0

我正在 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.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);
                }

有一个更好的方法吗?

4

1 回答 1

0

试试吧里奇:

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();

祝你好运。

于 2012-01-13T13:07:57.293 回答