如何通过点击android中的警告框外限制用户?

时间:2011-10-14 10:20:22

标签: android

我有一个自定义警报视图,弹出按钮点击事件。所有事情都很顺利。但问题是: 如果用户点击外部警报对话框,它就会消失。我想限制用户点击一边。我给他选择取消/交叉按钮来关闭警报对话框。 那么如何限制用户在警报框外点击? 代码:

onCreate for for按钮中的代码单击我正在调用show dialog:

final Button cdButton = (Button) findViewById(R.id.denonCdImage);
    cdButton.setOnClickListener(new Button.OnClickListener(){   
        public void onClick(View v) 
        {
            showDialog(CD_CATG_ID);
        }
    });



protected Dialog onCreateDialog(int id) {   

    AlertDialog.Builder builder;   
    Context mContext = this;   
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);   
    View layout = inflater.inflate(R.layout.categorydialog,(ViewGroup) findViewById(R.id.layout_root));   
    GridView gridview = (GridView)layout.findViewById(R.id.gridview);   
    gridview.setAdapter(new ImageAdapter(this)); 
    /** Check the id for the device type for image tobe change */
    switch(id) {   
    case 1 :   // for the cd image 
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,final int position, long id) {   
                Toast.makeText(view.getContext(), "Image selected for CD", 3000).show();
                cdImageId = getImageId(position);
                int elementId = getApplicationContext().getResources().getIdentifier(cdImageId, "drawable", getPackageName());
                cdImageView.setImageResource(elementId);

                Log.d("CdImageid", ""+cdImageId);
                closeDialog(view);
            }   
        });
        builder = new AlertDialog.Builder(mContext);   
        builder.setView(layout);   
        dialog = builder.create();   
        break;  
default:   
        dialog = null;   
    }

    /** onclick listner for the close button */
    ImageView close = (ImageView) layout.findViewById(R.id.close);
    close.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v){
            dialog.dismiss();
        }
    });

    return dialog;   
}  

有什么建议吗? 谢谢!

1 个答案:

答案 0 :(得分:14)

有两种与此行为相关的方法:setCancelable()setCanceledOnTouchOutside(),您可以在reference中看到。