1. Are you ready for the Galaxy S20? Here is everything we know so far!

If statement Problem

Discussion in 'Android Development' started by mizzeeboy, Dec 26, 2011.

  1. mizzeeboy

    mizzeeboy Lurker
    Thread Starter

    Ok so im trying to make a app that opens a toast saying you are incorrect or correct although im having some trouble with my if statements can someone please tell me what the problem is






    Code (Text):
    1.  
    2. public class IfActivity extends Activity {
    3.     Button GO;
    4.     EditText TEXT;
    5.  
    6.     /** Called when the activity is first created. */
    7.     @Override
    8.     public void onCreate(Bundle savedInstanceState) {
    9.         super.onCreate(savedInstanceState);
    10.         setContentView(R.layout.main);
    11.  
    12.         GO = (Button) findViewById(R.id.go);
    13.         TEXT = (EditText) findViewById(R.id.Textin);
    14.  
    15.         GO.setOnClickListener(new View.OnClickListener() {
    16.  
    17.             public void onClick(View arg0) {
    18.                 // TODO Auto-generated method stub
    19.                
    20.  
    21.                 if ("hello".equals(TEXT)) {
    22.                     Toast andEggs = Toast.makeText(IfActivity.this,
    23.                             "PASSWORD IS CORRECT", Toast.LENGTH_SHORT);
    24.                     andEggs.show();
    25.                 } else {
    26.                     Toast andEggs = Toast.makeText(IfActivity.this,
    27.                             "PASSWORD IS INCORRECT", Toast.LENGTH_SHORT);
    28.                     andEggs.show();
    29.                 }
    30.  
    31.             }
    32.         });
    33.  
    34.     }
    35. }
    36.  
    37.  
    38.  
     



    1. Download the Forums for Android™ app!


      Download

       
  2. miXer

    miXer Android Enthusiast

    Your if's are built up correctly, but your comparison is incorrect. You are trying to compare the text "hello" with the EditText control TEXT. Instead you must compare the text "hello" with the text inside the EditText like this:
    Code (Text):
    1. if ("hello".equals(TEXT.getText().toString())
     
Loading...

Share This Page

Loading...