Android之AlertDialog.Builder详解

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main extends Activity {
    /** Called when the activity is first created. */
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button1=(Button)findViewById(R.id.btn1);
        button2=(Button)findViewById(R.id.btn2);
        button3=(Button)findViewById(R.id.btn3);
        button4=(Button)findViewById(R.id.btn4);
        button5=(Button)findViewById(R.id.btn5);
        
        button1.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(Main.this)
                .setTitle("标题")
                .setItems(R.array.items_dialog,
                        new DialogInterface.OnClickListener() {
                            
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                CharSequence strDialogBody=getString(R.string.hello);
                                String[] it = getResources().getStringArray(R.array.items_dialog);
                                new AlertDialog.Builder(Main.this)
                                .setMessage(strDialogBody + it[which])
                                .setNeutralButton(R.string.app_ok, new DialogInterface.OnClickListener() {
                                    
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        //在这里做要处理的事情
                                    }
                                }).show();
                            }
                        })
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                //在这里做要处理的事情
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // TODO Auto-generated method stub
                                dialog.dismiss();
                            }
                        }).show();
            }
            
        });
        button2.setOnClickListener(new Button.OnClickListener(){
            
            @Override
            public void onClick(View v) {
                
                new AlertDialog.Builder(Main.this)
                .setTitle("标题")
                .setMessage("消息框")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        //在这里做要处理的事情
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                }).show();
            }
            
        });
        button3.setOnClickListener(new Button.OnClickListener(){
            
            @Override
            public void onClick(View v) {
                
                new AlertDialog.Builder(Main.this)
                .setTitle("请输入")
//                .setIcon(R.drawable.app)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setView(new EditText(Main.this))
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        //在这里做要处理的事情
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                }).show();
            }
            
        });
        button4.setOnClickListener(new Button.OnClickListener(){
            
            @Override
            public void onClick(View v) {
                
                new AlertDialog.Builder(Main.this)
                .setTitle("请输入")
                .setIcon(android.R.drawable.ic_dialog_info)
//                .setView(new EditText(Main.this))
                .setSingleChoiceItems(new String[] {"choice 1","choice 2","choice 3","choice 4"}, 0, new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                }).show();
            }
            
        });
        button5.setOnClickListener(new Button.OnClickListener(){
            
            @Override
            public void onClick(View v) {
                
                new AlertDialog.Builder(Main.this)
                .setTitle("请输入")
                .setIcon(android.R.drawable.ic_dialog_info)
//                .setView(new EditText(Main.this))
                .setMultiChoiceItems(new String[] {"choice 1","choice 2","choice 3","choice 4"}, null,new DialogInterface.OnMultiChoiceClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        // TODO Auto-generated method stub
                        
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                }).show();
            }
            
        });
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, Main!</string>
    <string name="app_name">AlertDialogDemo</string>
    <string name="app_ok">Hello OK!</string>
    <array name="items_dialog">
        <item>你好</item>
        <item>你好!</item>
        <item>你好!!</item>
    </array>
</resources>

来自:http://blog.csdn.net/weiyidemaomao/article/details/7861723

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值