0

OK I am trying to make a app which will only accept 1 word of any text.I am use the edittext to let the user put in the text and button for them to give them a toast with the words your incorrect or correct

The problem is when i use the if statement i cant get it to work can some one please identify the problem here is my code

public class IfActivity extends Activity {
Button GO;
EditText TEXT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GO = (Button) findViewById(R.id.go);
    TEXT = (EditText) findViewById(R.id.Textin);

    GO.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            if ("hello".equals(TEXT)) {
                Toast andEggs = Toast.makeText(IfActivity.this,
                        "PASSWORD IS CORRECT", Toast.LENGTH_SHORT);
                andEggs.show();
            } else {
                Toast andEggs = Toast.makeText(IfActivity.this,
                        "PASSWORD IS INCORRECT", Toast.LENGTH_SHORT);
                andEggs.show();
            }

        }
    });

}

}

1
  • 1
    What does "i cant get it to work" mean exactly? Commented Dec 26, 2011 at 16:36

3 Answers 3

3

TEXT is not a String. It is a reference variable of EditText.

if (TEXT.getText().toString().equals("hello"))
{

}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks dude I knew there was something I was missing just couldnt lay a finger on it spot on thanks again
1

Use this:

 public void onClick(View arg0) {             
 // TODO Auto-generated method stub              
 String tempTxt = ((EditText) findViewById(R.id.Textin)).getText().toString();
 if ("hello".equals(tempTxt )) {
     Toast andEggs = Toast.makeText(IfActivity.this,"PASSWORD IS CORRECT",Toast.LENGTH_SHORT);
     andEggs.show();
  } 
 else{
   Toast andEggs = Toast.makeText(IfActivity.this,"PASSWORD IS INCORRECT",Toast.LENGTH_SHORT);
   andEggs.show();
   }
 }

Comments

1

Use this :

if("hello".equals((findViewById(R.id.Textin)).getText().toString())) {  // show toast}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.