Such a problem: I want to enter text into EditText, and depending on it, I will open different Activities.
For example: I enter the word machine in EditText, and I have an Activity with a picture of a machine, or I enter the word plane in EditText, and I have an Activity with the word plane. Help me please!
- Provide your code and we will help you - danilshik
|
2 answers
Use TextWatcher :
edittext.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { if(s.toString().equals("машинка")) { Intent i = new Intent(YourActivity.this,SecondActivity.class); startActivity(i); } } }); |
public class MainActivity extends AppCompatActivity { Button button; EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.btn); editText = (EditText)findViewById(R.id.edText); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = editText.getText().toString(); Intent intent; switch (text){ case "машина": intent = new Intent(MainActivity.this, ActivityA.class); startActivity(intent); break; case "самолет": intent = new Intent(MainActivity.this, ActivityB.class); startActivity(intent); break; default: Toast toast = Toast.makeText(getApplicationContext(),"не было введено машина или самолет", Toast.LENGTH_SHORT); toast.show(); } } }); } } |