0

I have implemented a dialog box, I also get the text field as a string. Now the problem I have is that I want to compare the text input to a String and exit the app if the text on the dialog box matches either a string under (R.strings.stringname) or either a private String variable.

I have implemented this code but it does not seem to work.

public void onBackPressed(){
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setIcon(R.drawable.ic_about_logo);
    alert.setTitle("Phoebus Club");
    alert.setMessage("Please Insert Security Key");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        securityKey = input.getText().toString();
        if(securityKey == "oneplc"){
            System.exit(0);
        }
      }
    });

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

    alert.show();
}
AndroidKrayze
  • 349
  • 2
  • 5
  • 21

2 Answers2

2

Have a look at this question

The == operator doesn't really compare values, rather reference equality. You should use the equals() method of the String object instead:

if(securityKey.equels("oneplc")){
     System.exit(0);
}
Community
  • 1
  • 1
Daniel Setréus
  • 552
  • 4
  • 16
2

if static string then instead of

securityKey == "oneplc"

do

securityKey.equals("oneplc");

if from strings.xml then

securityKey.equals(getResources().getString(R.string.stringname));
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41