【问题标题】:How to make spinner in dialog mode?如何在对话模式下制作微调器?
【发布时间】:2013-05-03 00:32:15
【问题描述】:

我的应用程序中有按钮,当我单击此按钮时,它会打开一个微调器,但微调器处于下拉模式,我需要将其设为对话模式。对于 API 11 及更高版本,有一个简单的代码可以解决问题:

Spinner s1 = new Spinner(this, Spinner.MODE_DIALOG);

但我需要使用一些代码,这些代码也适用于 7 及更高版本的 API。有人可以帮帮我吗?

【问题讨论】:

    标签: android dialog makefile spinner mode


    【解决方案1】:

    我就是这样做的:

    这里,问题是String[];

    DialogInterface.OnClickListener questionDialogListener = new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
    
    // implement the coding for getting the selected item.
    
    
    
                    arg0.dismiss();
                }
            };
    
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Select Question:");
            builder.setItems(questions, questionDialogListener);
    
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    

    【讨论】:

    【解决方案2】:

    我是这样做的:

        LayoutInflater inflater = context.getLayoutInflater();
        final View dlg =inflater.inflate(R.layout.dialog,null);             
    
        final AlertDialog d = new AlertDialog.Builder(context)
            .setView(dlg)
            .setPositiveButton("SAVE",
                    new Dialog.OnClickListener() {
                        public void onClick(DialogInterface d, int which) {
                            //Do nothing here. We override the onclick
                        }
                    })
            .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                      //This will close dialog
                   }
               })
            .create();
    
            d.setOnShowListener(new DialogInterface.OnShowListener() {              
                @Override
                public void onShow(DialogInterface dialog) {    
                    final Spinner mySpinner = (Spinner)dlg.findViewById(R.id.spinner);
                    ArrayAdapter<CharSequence> adapter =  new ArrayAdapter<CharSequence>(context,android.R.layout.simple_spinner_item );
                //ADD VALUES TO adapter
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                 
                    mySpinner.setAdapter(adapter);
                    Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
                    b.setOnClickListener(new View.OnClickListener() {               
                        @Override
                        public void onClick(View view) {
                            d.dismiss();
                            //DO SOMETHING
                        }
                    });
                }
            });             
            d.show();
    

    【讨论】:

    • 这不是我想要的,我需要在对话框模式下显示 Spinner 而不是在对话框内制作 Spinner。
    • 您希望微调器作为带有下拉菜单的对话框还是列表?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多