简体   繁体   中英

Unable to compare values from alert dialog edit text and my string

regsCode1=objCommonServices.fetchRegisterationCode(etMobileNo.getText().toString().trim(),etPassword.getText().toString().trim());
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Recharhge Confirmation");
alert.setMessage("Message");
// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Editable value = input.getText();
            System.out.println("Hello"+regsCode1 +" Value is "+value.toString());
            if(value.toString().equals(regsCode1)){
                System.out.println("Hello"+regsCode1+" "+value);
            }
             else{
                System.out.println("B");
            }
        }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
           // Canceled.
      }
});
alert.show();

I get the result B. Since System.out print both values correctly but it doesn't compare two strings I have even changed Editable value from String value and used toString fn of object class. but then also i can't compare two values

Compare like this

public void onClick(DialogInterface dialog, int whichButton) {
   String value = input.getText().toString().trim(); /// use this line 
   System.out.println("Hello"+regsCode1 +" Value is "+value);
      if(value.equals(regsCode1)){
         System.out.println("Hello"+regsCode1+" "+value);
      }
      else{
         System.out.println("B");
       }
    }
});

尝试if(value.toString().trim().equalsIgnoreCase(regsCode1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM