onCreate 中显示的 Android 上下文菜单

发布于 2024-10-30 22:30:35 字数 174 浏览 1 评论 0原文

我正在尝试添加以下功能:如果用户单击 mainActivity 中的按钮,它会启动另一个 Activity,并且必须在屏幕上弹出一个包含两种可能性的菜单。当用户选择任一可能性时,来自网络的不同 XML 将加载到我的布局中。

我不知道是否可以在 onCreate 方法中访问上下文菜单,或者是否有其他方法可以做到这一点?

I'm trying to add the functionality that if the user clicks a button in my mainActivity, it starts another Activity and it has to pop up a menu with two possibilities on the screen. When the users picks either possibility, a different XML from the net will be loaded in my lay-out.

I don't know if this is possible to access a context menu in the onCreate method or if there's another way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

流年已逝 2024-11-06 22:30:35

弹出一个 AlertDialog,其中包含您要显示的项目列表。当您选择的项目被选中时,触发 startActivity(intent)。

示例代码在这里:
http://developer.android.com/guide/topics/ui/dialogs .html#AddingAList

Pop open an AlertDialog with a list of items you want to show. Fire a startActivity(intent) when your choice of item is selected.

Sample code here:
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList

桃气十足 2024-11-06 22:30:35

为什么不在加载新活动之前显示一个包含两个选项的对话框?然后,根据用户的选择显示相应的xml?

我会这样做:

  1. 你的 mainActivity 正在运行
  2. 当用户单击你的按钮时,你会显示一个 AlertDialog,其中“正”按钮代表第一个 xml 文件,“负”按钮代表你的第二个 xml。
  3. 单击任何按钮后,您可以通过使用不同的 Intent 操作并检查第二个 Activity 的 onCreate 方法中使用了哪个 Intent 来显示您的第二个 Activity。

对于您的按钮:

Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent act2 = null;
                    switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        // layout 1
                        act2 = new Intent("package.example.act2_layout1");
                        startActivity(act2);
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        act2 = new Intent("package.example.act2_layout2");
                        startActivity(act2);
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity.this);
            builder.setTitle("Make your choice")
                .setPositiveButton("Layout 1", listener)
                .setNegativeButton("Layout 2", listener);
        }
    });

然后在 onCreate 方法中:

Intent starter = getIntent();
    if (starter.getAction().equals("package.example.act2_layout1")) {
        // use layout 1
    } else if (starter.getAction().equals("package.example.act2_layout2")) {
        // use layout 2
    }

Why don't you display a dialog with the two options before loading your new Activity? And then, according the user's selection display the corresponding xml?

I would do it this way:

  1. You have your mainActivity running
  2. When the user clicks your button, you show an AlertDialog with the "positive" button representing the first xml file and the "negative" button representing your second xml.
  3. Upon click on any of the buttons, you show your second Activity by using different Intent-actions and checking which intent was used in the onCreate method of your second Activity.

For your button:

Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent act2 = null;
                    switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        // layout 1
                        act2 = new Intent("package.example.act2_layout1");
                        startActivity(act2);
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        act2 = new Intent("package.example.act2_layout2");
                        startActivity(act2);
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity.this);
            builder.setTitle("Make your choice")
                .setPositiveButton("Layout 1", listener)
                .setNegativeButton("Layout 2", listener);
        }
    });

And then in the onCreate method:

Intent starter = getIntent();
    if (starter.getAction().equals("package.example.act2_layout1")) {
        // use layout 1
    } else if (starter.getAction().equals("package.example.act2_layout2")) {
        // use layout 2
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文