Android 可检查菜单可在选择时打开一个新类
我正在 Android 中创建一个菜单,我希望这个菜单根据用户选择的内容打开一个新类。
我创建的菜单来自此链接:http://developer. android.com/guide/topics/ui/dialogs.html#AlertDialog
这是添加复选框和单选按钮的代码
我有这个代码:
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();
但我想把Toast拿走:
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
所以当用户点击指定的数组列表中的颜色显示一个新类,我不知道该怎么做。
我试图做出一个如下所示的 if 语句:
if(items.equals("Red")){
Intent red = new Intent(Menu.this,Red.class);
startActivity(red);
}
但这不起作用。
编辑
不用担心,我刚刚通过以下操作完成了此操作:
if(items[item].equals("Red")){
Intent red = new Intent(Menu.this,Red.class);
startActivity(red);
}
有更好的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试吧 瑞奇:
祝你好运。
try it Ricky:
good luck.