How to accept user input in android

I have an EditText in android in which I want the user to enter text and check the "BYE" condition

Code example:

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 get the user to enter text? The user interface should wait for text input (something like InputStreamReader in java applications).

+6
android
source share
3 answers

I do not think you need a loop for this. From your comment, it looks like you also have an β€œEnter” button or something that you press to perform a check. Just install onclicklistener and onclick, you can make edittext invisible (or not editable), check that edittext is "BYE", and then 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); } }); 
+5
source share

Very simple: -

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

now in str u will get a string that is entered in EditText

+3
source share

Instead of running this check in an infinite loop, only run it on every onKeyUp in EditText. In any case, you know that the condition will be satisfied only when the user really enters something.

+2
source share

All Articles