1

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

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

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

4

2 回答 2

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();
于 2013-05-08T18:18:09.110 回答
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();
    }
于 2013-05-08T17:15:30.997 回答