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();
        	}
        	
        });
    }
}

res/values/strings.xml




    Hello World, Main!
    AlertDialogDemo
    Hello OK!
	
	    你好
	    你好!
	    你好!!
	


你可能感兴趣的:(Android)