Android4.0-AlertDialog 自身的问题

问题描述:当弹出对话框时,点击对话框外的其他的地方。就会关闭对话框。

解决方案:添加AlertDialog.Builder .setCancelable(false);

 

Android-AlertDialog(警告窗口)的一个例子
php1DUUrR

new AlertDialog.Builder(TestActivity.this)//Context 
.setTitle("标题啊") 
.setIcon(android.R.drawable.ic_dialog_alert)//图标 
.setMessage("内容")
 .setNegativeButton("取消",new DialogInterface.OnClickListener() {//按钮1 
public
 void onClick(DialogInterface arg0, int arg1) { } })
 .setPositiveButton("确定"new DialogInterface.OnClickListener() {//按钮2 
public void onClick(DialogInterface arg0, int arg1) { finish(); } })
.show();//显示






import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;


public class testAlertDialog extends Activity {
    Button btnShowDialog;
    Button btnShowDialog_Layout;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //定义按钮
        btnShowDialog=(Button)this.findViewById(R.id.Button01);
        btnShowDialog.setOnClickListener(new ClickEvent());
        btnShowDialog_Layout=(Button)this.findViewById(R.id.Button02);
        btnShowDialog_Layout.setOnClickListener(new ClickEvent());
    }
    
    
    //统一处理按键事件
    class ClickEvent implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v==btnShowDialog)
                showDialog(testAlertDialog.this);
                
            else if(v==btnShowDialog_Layout)
                showDialog_Layout(testAlertDialog.this);
            
        }

    }

    //显示基本的AlertDialog
    private void showDialog(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.icon);
        builder.setTitle("Title");
        builder.setMessage("Message");
        builder.setPositiveButton("Button1",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle("点击了对话框上的Button1");
                    }
                });
        builder.setNeutralButton("Button2",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle("点击了对话框上的Button2");
                    }
                });
        builder.setNegativeButton("Button3",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle("点击了对话框上的Button3");
                    }
                });
        builder.show();
    }


    //显示基于Layout的AlertDialog
    private void showDialog_Layout(Context context) {
        LayoutInflater inflater = LayoutInflater.from(this);
        final View textEntryView = inflater.inflate(
                R.layout.dialoglayout, null);
        final EditText edtInput=(EditText)textEntryView.findViewById(R.id.edtInput);
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setIcon(R.drawable.icon);
        builder.setTitle("Title");
        builder.setView(textEntryView);
        builder.setPositiveButton("确认",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle(edtInput.getText());
                    }
                });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle("");

 

posted @ 2012-06-27 10:00  wanqi  阅读(3513)  评论(0编辑  收藏  举报