如何将 Android Spinner 创建为弹出窗口?

How do I create an Android Spinner as a popup?(如何将 Android Spinner 创建为弹出窗口?)
本文介绍了如何将 Android Spinner 创建为弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击菜单项以允许用户选择一项时调出一个微调对话框.

I want to bring up a spinner dialog when the user taps a menu item to allow the user to select an item.

我需要一个单独的对话框,还是可以直接使用 Spinner?我看到 这个链接, 提到了一个 MODE_DIALOG 选项,但它似乎不再被定义了.AlertDialog 可能没问题,但所有选项都说单击列表中的项目不会关闭对话框",这是我想要的.有什么建议吗?

Do I need a separate dialog for this or can I use Spinner directly? I see this link, mentions a MODE_DIALOG option but it doesn't seem to be defined anymore. AlertDialog may be OK but all the options say "clicking on an item in the list will not dismiss the dialog" which is what I want. Any suggestion?

理想情况下,代码类似于屏幕上显示微调器的情况:

Ideally, the code would be similar to the case where the spinner is shown on the screen:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
     android.R.layout.simple_spinner_item, items);              
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
myspinner.setAdapter(adapter);  
// myspinner.showAsDialog() <-- what i want             

推荐答案

你可以使用警告对话框

    AlertDialog.Builder b = new Builder(this);
    b.setTitle("Example");
    String[] types = {"By Zip", "By Category"};
    b.setItems(types, new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            dialog.dismiss();
            switch(which){
            case 0:
                onZipRequested();
                break;
            case 1:
                onCategoryRequested();
                break;
            }
        }

    });

    b.show();

当您想要按下其中一个时,这将关闭对话框.希望这会有所帮助!

This will close the dialog when one of them is pressed like you are wanting. Hope this helps!

这篇关于如何将 Android Spinner 创建为弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)