tags:

views:

64

answers:

2

hi, i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"

Code sample:

EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString(); 

while( !(abc).equals("bye")){

   abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text

   //do some operation with abc

 }

How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).

+1  A: 

Instead of running this check in an infinite loop, only run it on every onKeyUp of the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.

David Hedlund
but i want to enter from the keyboard.its something like this:i enter some text and press enter then while loop should check the if the string entered is "BYE" then terminate or else go inside loop and ask me to enter again till i enter "BYE"
SPB
A: 

I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:

final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    ET.setVisibility(View.INVISIBLE);
                    if(ET.getText.toString() == "BYE")
                    {
                        //do something if it is "BYE"
                    } else {
                        Context context = getApplicationContext();
                    CharSequence text = "Please enter BYE";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show(); 
                    }
                    ET.setVisibility(View.VISIBLE);
                } });
CornCat