Android开发之Dialog(AlertDialog,ProgressDialog)

当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用对话框来完成:

核心代码:


public class DemoActivity extends Activity implements OnClickListener {
	private Button bt1,bt2,bt3,bt4,bt5;
	AlertDialog dialog3 ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt1 = (Button) this.findViewById(R.id.button1);
        bt1.setOnClickListener(this);
        bt2 = (Button) this.findViewById(R.id.button2);
        bt2.setOnClickListener(this);
        bt3 = (Button) this.findViewById(R.id.button3);
        bt3.setOnClickListener(this);
        bt4 = (Button) this.findViewById(R.id.button4);
        bt4.setOnClickListener(this);
        bt5 = (Button) this.findViewById(R.id.button5);
        bt5.setOnClickListener(this);
    }
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1: // 确定取消的对话框 
			//1.获取一个对话框的创建器
			AlertDialog.Builder builder = new Builder(DemoActivity.this);
			//2.所有builder设置一些参数 
			builder.setTitle("对话框标题");
			builder.setMessage("确定退出?");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){

				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getApplicationContext(), "确定被点击", 0).show();
					
				}});
			
			builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getApplicationContext(), "取消被点击", 0).show();
					
				}
			});
			AlertDialog  dialog  = builder.create();
			dialog.show();//如果没有调用show方法,对话框将不会弹出。
			break;
		case R.id.button2:  //pick的对话框 
			//1.获取一个对话框的创建器
			AlertDialog.Builder builder2 = new Builder(DemoActivity.this);
			builder2.setTitle("pick 一个人");
			final String [] arr = new String[]{"张三","李四","王五"};
			builder2.setItems(arr, new DialogInterface.OnClickListener(){

				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getApplicationContext(), arr[which], 0).show();
					
				}
			});
			builder2.create().show();
			break;
		case R.id.button3:  //带选择 按钮的单选对话框 
			//1.获取一个对话框的创建器
			AlertDialog.Builder builder3 = new Builder(DemoActivity.this);
			builder3.setTitle("选择一个颜色");
			final String [] items = new String[]{"红色","蓝色","黄色"};
			//第二个参数是设置默认选项,选项索引从0开始,-1代表不选择任何选项。
			builder3.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener(){

				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(getApplicationContext(), items[which], 0).show();
					dialog3.dismiss();
				}});
			dialog3 = builder3.create();
			dialog3.show();
			break;
		case R.id.button4:  //带选择多个 按钮的多选对话框 
			//1.获取一个对话框的创建器
			AlertDialog.Builder builder4 = new Builder(DemoActivity.this);
			builder4.setTitle("选择若干个颜色");
			final String [] items4 = new String[]{"红色","蓝色","黄色"};	
			builder4.setMultiChoiceItems(items4, new boolean[]{false,false,false}, new DialogInterface.OnMultiChoiceClickListener(){

				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					Toast.makeText(getApplicationContext(), items4[which] + " 选择状态"+ isChecked, 0).show();
					
				}});
			builder4.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					
				}
			});
			dialog3 =  builder4.create();
			dialog3.show();
			break;
			
		case R.id.button5 : //进度条对话框 
			ProgressDialog pd = new ProgressDialog(this);
			pd.setTitle("提示");
			pd.setMessage("正在获取数据");
			pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为水平进度条
			//调用setProgressStyle()方法设置进度对话框风格。有两种风格:
            //ProgressDialog.STYLE_SPINNER 旋体进度条风格 (为默认风格)
            //ProgressDialog.STYLE_HORIZONTAL 横向进度条风格
			pd.setMax(100);
			pd.show();
			pd.setProgress(50);
			break;
		}
		
	}
	@Override
	protected void onPause() {
		System.out.println("onpause");
		super.onPause();
	}
	
	
}
 
XML布局文件:

    
    

    
    

    
     
     

    
     
     

    
     
     

    
     
     

    
     
     


    
    
 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值