Comparing two editTexts in android

五迷三道 提交于 2019-11-28 12:14:26

问题


I am learning android I tried following codeline but it's giving me error please give me suggestions, that how can I compare two edittext's text.

if((edt1.getText().toString() && 
    edt4.getText().toString() && 
    edt7.getText().toString)=="X")

回答1:


Here's a solution that doesn't violate the DRY principle:

private static boolean allContain(final String value, 
                                  final EditText... editTexts)
{

    for (EditText editText : editTexts) {
        final String text = editText.getText().toString();
        if (!text.equals(value)) {
            return false;
        }
    }
    return true;
}

You can use it as follows:

if (allContain("X", edt1, edt2, edt3, edt4)) {
    // All EditTexts contain 'X'
}



回答2:


Please try this:

if((edt1.getText().toString.equalsIgnoreCase("X")) && 
   (edt4.getText().toString.equalsIgnoreCase("X")) && 
   (edt7.getText().toString.equalsIgnoreCase("X")))

If you have to compare strings then you need to call the equals or equalsIgnoreCase function of String.




回答3:


I have find the best solution..

if(Password.getText().toString().trim().matches(confirmPassword.getText().toString().trim()))
{
// then do your work
}
else
//otherwise show error message.

whereas

Password = (EditText)findViewById(R.id.pass);

confirmPassword = (EditText)findViewById(R.id.confirmpass);

are two editText.




回答4:


if( (edt1.getText().toString()=="X")&&(edt4.getText().toString()=="X")&&(edt7.getText().toString()=="X") )



回答5:


if you want to check edt1, edt4, edt7 have "X" value then try this..

if((edt1.getText().toString().equalsIgnoreCase("X")     
               &&edt4.getText().toString().equalsIgnoreCase("X") && 
              edt7.getText().toString.equalsIgnoreCase("X"))



回答6:


Make it simple:

if (!et1.toString().equals(et2.toString())) {
    MsgBox(this,"--Your Message--");
}


来源:https://stackoverflow.com/questions/6567527/comparing-two-edittexts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!