如何使用微调器创建警告对话框

时间:2013-04-28 22:04:22

标签: android android-spinner android-alertdialog

我知道如何制作一个警告对话框但是我需要用一个微调器来制作一个警告对话框,这样当它弹出时,这个人就可以选择发生什么。有没有人有一个带微调器的警告对话框的代码或知道任何好的教程?

提前致谢

1 个答案:

答案 0 :(得分:17)

LayoutInflater li = LayoutInflater.from(context);

View promptsView = li.inflate(R.layout.my_dialog_layout, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

alertDialogBuilder.setView(promptsView);

// set dialog message

alertDialogBuilder.setTitle("My Dialog..");
alertDialogBuilder.setIcon(R.drawable.ic_launcher);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();

final Spinner mSpinner= (Spinner) promptsView
        .findViewById(R.id.mySpinner);
final Button mButton = (Button) promptsView
        .findViewById(R.id.myButton);

// reference UI elements from my_dialog_layout in similar fashion

mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());

// show it
alertDialog.show();
alertDialog.setCanceledOnTouchOutside(false);

,其中

my_dialog_layout 是您包含微调器的弹出式布局 mySpinner

编辑:

public class OnSpinnerItemClicked implements OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            Toast.makeText(parent.getContext(), "Clicked : " +
                    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();


        }

        @Override
        public void onNothingSelected(AdapterView parent) {
            // Do nothing.
        }
    }