-3

I want my app to be such that when the editText1 has value (2) without brackets in it, the button press (with onClick="k") would navigate to MainActivity2. And if the text is (3), the activity MainActivity3 would open. I guess it'll use If function. How can I just implement this in my app? Sorry for silly questions, I'm just a newbie. ;-;

Iamat8
  • 3,888
  • 9
  • 25
  • 35
  • (with onClick="k") -- what is k?.. button??.. or character in keyboard?.. – uday Aug 10 '15 at 12:07
  • @uday k is the android:onClick of my button. – Dhairyadev Arora Aug 10 '15 at 12:09
  • can you post any code? – Iamat8 Aug 10 '15 at 12:14
  • String string = editText1.getText().toString();{ switch(string){ case "2": public void kden(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity2.class); startActivity(i); } break; case "3": public void kden(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity3.class); startActivity(i); } @Mohit here's my code till now. It isn't working. It's showing errors. – Dhairyadev Arora Aug 10 '15 at 12:17

6 Answers6

0

Within onClickListener, get the data from edittext as string and compare it using switch-case :

String string = editText1.getText().toString();
switch(string){

case "2":
//Start MainActivity2
break;

case "3":
//Start MainActivity3
break;
}
Zds
  • 4,311
  • 2
  • 24
  • 28
Srijith
  • 1,695
  • 17
  • 29
0

I'm not sure why exactly you're doing this, but here's what you can do:

  1. Set the EditText property to accept only digits/numbers

  2. onClick() of the button, access the number entered in EditText by

    editText1.getText().toString()

  3. After you've done that, use switch-case statements to launch the desired activity.

Joel Fernandes
  • 4,776
  • 2
  • 26
  • 48
  • Could you please elaborate it a bit more? I'm a beginner and can't really understand that. ._. i've anyway typed this: String string = editText1.getText().toString();{ switch(string){ case "2": public void k(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity2.class); startActivity(i); } break; case "3": public void k(View v) { // TODO Auto-generated method stub Intent i = new Intent(getApplicationContext(),MainActivity3.class); startActivity(i); } – Dhairyadev Arora Aug 10 '15 at 12:15
0
Try this:-

String string = editText1.getText().toString();

       if(string.equals("2"))
       {
        Intent i = new  Intent(getApplicationContext(),MainActivity2.class);
        startActivity(i); 
       }
       else if(equals("3"))
       {
        Intent i = new   Intent(getApplicationContext(),MainActivity3.class); 
        startActivity(i);
       }
0

Remove the methods inside the switch-case

    case "2": 
    public void kden(View v) { 
         // TODO Auto-generated method stub 
         Intent i = new Intent(getApplicationContext(),MainActivity2.class);
         startActivity(i); 
    } 
    break; 

and try this

 case "2":    
    Intent i = new Intent(getApplicationContext(),MainActivity2.class);
    startActivity(i); 
    break;
uday
  • 1,348
  • 12
  • 27
0

First, retrieve the value from the EditText. If you only want digits/numbers then you can set this in the properties for theEditText. For now, we'll leave it as accepting string values. Assuming the identifier for your EditText is 'editText1':

String targetActivity = editText1.getText().toString();

Secondly, you need to use conditional statements to evaluate which Activity to launch. @Sree used switch-case in his answer, here I will use if statements as they are much easier to read and understand.

Intent i;

if(targetActivity.equals("k")){
   i = new Intent(YourCurrentActivity.this, StartSomeActivity.class);
   startActivity(i);
}

else if(targetActivity.equals("3")){
    i = new Intent(YourCurrentActivity.this, StartAnotherActivity.class);
    startActivity(i);
}

Obviously you can change what values to compare against, I used k and 3 as those were the examples you gave in your question. Also be sure to swap out 'YourCurrentActivity' for the current Activity name and 'StartSomeActivity' and 'StartAnotherActivity' with the names of the desires activites you want to launch.

Scott
  • 1,863
  • 2
  • 24
  • 43
0

At least post the code properly in comment...

And with value you can use this on your button click event

int key = Integer.parseInt(edit.getText().toString());
Intent i;
switch (key) {
  case 1:
      i = new Intent(MainActivity.this,MainActivity2.class); 
    break;
  case 2:
      i = new Intent(MainActivity.this,MainActivity3.class); 
    break;
}
startActivity(i);
Community
  • 1
  • 1
Iamat8
  • 3,888
  • 9
  • 25
  • 35