Android checkable menu to open up a new class on selection
-
28-10-2019 - |
سؤال
I am creating a menu in Android and I would like this menu to open a new class depending on what the user has selected.
The Menu I have created is from this link: http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
And is the code for the adding check boxes and radio buttons
I have this code:
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
final AlertDialog alert = builder.create();
But I would like to take the Toast away:
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
So when the user clicks on the specified colour in the array list show a new class, which I am not sure how to do.
I am trying to make an if statement that looks like this:
if(items.equals("Red")){
Intent red = new Intent(Menu.this,Red.class);
startActivity(red);
}
But this doesn't work.
Edit
No worries I have just done this by doing:
if(items[item].equals("Red")){
Intent red = new Intent(Menu.this,Red.class);
startActivity(red);
}
Is there a better way to do this?
المحلول
try it Ricky:
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Intent color;
switch(item){
case 0:
color = new Intent(Menu.this,Red.class);
break;
case 1:
color = new Intent(Menu.this,Green.class);
break;
case 2:
color = new Intent(Menu.this,Blue.class);
break;
default:
color = null;
break;
}
if(color!=null)startActivity(color);
}
});
final AlertDialog alert = builder.create();
good luck.
لا تنتمي إلى StackOverflow