Swapnil
Swapnil

Reputation: 1

Comparing two editTexts in android

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")

Upvotes: 0

Views: 10737

Answers (6)

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

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.

Upvotes: 1

raghav chopra
raghav chopra

Reputation: 837

Make it simple:

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

Upvotes: -1

hoipolloi
hoipolloi

Reputation: 8044

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'
}

Upvotes: 6

Chirag
Chirag

Reputation: 56925

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.

Upvotes: 1

Niranj Patel
Niranj Patel

Reputation: 33238

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"))

Upvotes: 0

ArK
ArK

Reputation: 21058

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

Upvotes: 0

Related Questions