如何在对话框模式下制作微调器?

时间:2013-05-08 17:09:12

标签: android dialog makefile spinner mode

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

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

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

2 个答案:

答案 0 :(得分:0)

这就是我的表现:

这里的问题是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();
    }

答案 1 :(得分:0)

我这样做:

    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();