How do I create an Android Spinner as a popup?

后端 未结 12 1754
梦如初夏
梦如初夏 2020-12-02 09:20

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 dir

相关标签:
12条回答
  • 2020-12-02 09:32

    Try this:

    Spinner popupSpinner = new Spinner(context, Spinner.MODE_DIALOG);
    

    See this link for more details.

    0 讨论(0)
  • 2020-12-02 09:34

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

    0 讨论(0)
  • 2020-12-02 09:36

    In xml there is option

    android:spinnerMode="dialog"
    

    use this for Dialog mode

    0 讨论(0)
  • 2020-12-02 09:37

    Here is an Spinner subclass which overrides performClick() to show a dialog instead of a dropdown. No XML required. Give it a try, let me know if it works for you.

    public class DialogSpinner extends Spinner {
        public DialogSpinner(Context context) {
            super(context);
        }
    
        @Override 
        public boolean performClick() {
            new AlertDialog.Builder(getContext()).setAdapter((ListAdapter) getAdapter(), 
                new DialogInterface.OnClickListener() {
                    @Override public void onClick(DialogInterface dialog, int which) {
                        setSelection(which);
                        dialog.dismiss();
                    }
                }).create().show();
            return true;
        }
    }
    

    For more information read this article: How To Make Android Spinner Options Popup In A Dialog

    0 讨论(0)
  • 2020-12-02 09:38

    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!

    0 讨论(0)
  • 2020-12-02 09:42

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

    0 讨论(0)
提交回复
热议问题