[英]comparing two strings of EditTexts in android
我很抱歉,如果這是一個愚蠢的問題,但我試圖找到答案,但無法得到。 請幫幫我..我是android studio的新手,但有基本的java知識。 實際上,問題是我試圖像這樣比較TextView
和EditText
值;
TextView a = (TextView)findViewById(R.id.t1);
EditText b = (EditText)findViewById(R.id.t2);
String x = a.getText().toString();
String y = b.getText().toString();
if(x==y)
{
//----stuff--;
}
不顯示任何錯誤,但“if”下的代碼不執行。
根據區分大小寫,使用equals()
或equalsIgnoreCase()
。
試試這樣:
if(x.equalsIgnoreCase(y))
{
//----stuff--;
}
點擊此鏈接了解更多信息: http : //www.leepoint.net/data/expressions/22compareobjects.html
您不能將兩個字符串與等於運算符進行比較。 使用equals或equalsIgnoreCase,如下面的代碼。
if(x.equalsIgnoreCase(y)){
//return value true if both the strings are same.
}
String x = a.getText().toString();
String y = b.getText().toString();
boolean correct = x.equals(y);
if(correct)
{
//do stuff
}
用這個
==運算符測試兩個變量是否具有相同的引用(標識)和內存地址 。
而equals()方法測試兩個變量是否引用具有相同狀態(值)的對象 。
我建議你使用equals()方法,如果匹配大小寫不需要匹配大小寫,而不是使用equalsIgnoreCase()
喜歡
if(x.equalsIgnoreCase(y)){
//both string are equals
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.