Android 可检查菜单可在选择时打开一个新类

发布于 2024-12-26 12:59:26 字数 1451 浏览 2 评论 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拿走:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷血 2025-01-02 12:59:26

试试吧 瑞奇:

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

祝你好运。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文