How do I create an Android Spinner as a popup?

2019-01-13 03:07发布

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

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             

10条回答
走好不送
2楼-- · 2019-01-13 03:52

MODE_DIALOG and MODE_DROPDOWN are defined in API 11 (Honeycomb). MODE_DIALOG describes the usual behavior in previous platform versions.

查看更多
Luminary・发光体
3楼-- · 2019-01-13 03:54

Adding a small attribute as android:spinnerMode="dialog" would show the spinner contents in a pop-up.

查看更多
小情绪 Triste *
4楼-- · 2019-01-13 04:00

You can use an alert dialog

    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!

查看更多
看我几分像从前
5楼-- · 2019-01-13 04:00

In xml there is option

android:spinnerMode="dialog"

use this for Dialog mode

查看更多
登录 后发表回答