[英]Android equalsIgnoreCase not working correctly
temp1 is "accountant"editText is "accountant" temp1是“会计” editText是“会计”
but i checked with String.length(); 但是我检查了String.length();
I tried trim() method, i got the same result again. 我尝试过trim()方法,再次得到了相同的结果。
Code sample: 代码示例:
if(temp1.equalsIgnoreCase(editText1.getText().toString().trim()))
{
//do someting
}
I solved. 我解决了
Since temp1
is a string and has 11 characters, you must have placed a whitespace after the word accountant. 由于temp1
是一个字符串,具有11个字符,因此您必须在会计一词后放置一个空格。 (Either that or you flat out misspelled the word.) Accountant is only 10 characters long, so there must be whitespace. (或者您拼写错误的单词。)会计只有10个字符长,因此必须有空格。 (Well, it's pretty guaranteed since you, yourself, created it.) (很好,因为您自己创建了它就可以保证。)
Either remove the whitespace from temp1
, or trim temp1
before you compare. 在比较之前,请从temp1
删除空白,或修剪temp1
。 Also, double-check temp1
to actually make sure you spelled the word correctly. 另外,请仔细检查temp1
以确保您正确拼写了该单词。
You may try this code : 您可以尝试以下代码:
//put this below public class activity
int index=0;
String [] answer ={"Accountant"};
and put this button code below setContentView 并将此按钮代码放在setContentView下面
buttonanswer = (Button)findViewById(R.id.yourbutton);
buttonanswer.setOnClickListener(this);
and then put this code 然后把这段代码
@Override
public void onClick(View v) {
edittextanswer=(EditText)findViewById(R.id.youreditText);
if(v==buttonanswer) {
String myanswer=edittextanswer.getText().toString();
if(myanswer.equalsIgnoreCase(answer[index]))
{
//do someting
}
else
{
//do someting
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.