选择项目时无法取消AlertDialog

时间:2011-05-12 19:50:26

标签: android alertdialog

嘿伙计们,当我选择一个项目时,我无法将警报对话框解除。我使用AlertDialog构建器来显示它,我相信这是我的问题。我不能调用dismiss因为它们不是builder.dismiss()方法。有没有一种简单的方法来解决这个问题?这是我的代码。感谢。

  AlertDialog.Builder builder = new AlertDialog.Builder(this);

 Context mContext = this;

 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.numplayersdialog,
                                (ViewGroup) findViewById(R.id.layout_root));

 final Spinner spinner = (Spinner) layout.findViewById(R.id.spinner);
 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
         this, R.array.num_players_array, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spinner.setAdapter(adapter);

 builder.setTitle("Select Number of Players"); 

 spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            int item = spinner.getSelectedItemPosition();
            commandWriter(item); 
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });


 builder.setView(layout);
 builder.show();

1 个答案:

答案 0 :(得分:2)

builder.show()会返回AlertDialog个对象。您可以使用它来关闭对话框。