android对话框介绍

一点需要注意:对话框接收上下文不能用getaplicationcontext,因为它要挂在到activity上,
用getaplicationcontext它不知道挂在到哪个activity上。

默认对话框 

/**
	 * 默认对话框 
	 * 
	 */
	
	public void click1(View v){
		
		//1 创建对话框 
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle("警告");
		builder.setMessage("您的网络有异常");
		builder.setPositiveButton("确定", new OnClickListener() {
			
			//点击 确定按钮之后  该方法执行 
			@Override
			public void onClick(DialogInterface dialog, int which) {


				System.out.println("点击了确定");
				
				
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		//一定不要忘记 show 
		builder.show();
	}

单选 对话框 

	/**
	 * 单选 对话框 
	 * 
	 */
	
	public void click2(View v){
		
		//1 创建对话框 
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle("请选课");
		
		final String items[] = {"android","ios","C++","C"};
		
		builder.setSingleChoiceItems(items, -1, new OnClickListener() {
			//选中了具体某一个
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				Toast.makeText(MainActivity.this, items[which], 1).show();
				dialog.dismiss(); //取消对话框
				
			}
		});
		//一定不要忘记 show 
//		builder.show();
		
		dialog = builder.create();
		dialog.show();
	}

多选 对话框 

	/**
	 * 多选 对话框 
	 * 
	 */
	
	public void click3(View v){
		
		//1 创建对话框 
		AlertDialog.Builder builder = new Builder(MainActivity.this);
		builder.setTitle("请选择您喜欢的水果");
		
		final String items[] = {"橘子","苹果","梨","榴莲","橙子","猕猴桃","黄瓜","火龙果"};
		final boolean[] checkedItems = {true,false,false,false,false,false,true,false};
		builder.setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				//
				checkedItems[which] = isChecked;
				
			}
		});
		builder.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				//我要却出我选中的
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < checkedItems.length; i++) {
					if (checkedItems[i]) {
						
						String  fruit = items[i]; //取出选中水果
						sb.append(fruit+ "  ");
						
					}
					
				}
				
				Toast.makeText(getApplicationContext(), sb.toString(), 1).show();
				
			}
		});
		
		
		//一定不要忘记 show 
//		builder.show();
		
		dialog = builder.create();
		dialog.show();
		
	}

进度对话框 

	/**
	 * 进度对话框 
	 * progressDialog
	 */
	
	public void click4(View v){
		final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
		progressDialog.setTitle("天天酷跑");
		progressDialog.setMessage("正在玩命加载ing");
		//设置进度条的样式 
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		
		progressDialog.show();
		progressDialog.setMax(100);
		new Thread(){public void run() {
			
			try {
				
				for (int i = 0; i <= 100; i++) {
					Thread.sleep(50);
					progressDialog.setProgress(i);
					
				}
				progressDialog.dismiss();
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
//			progressDialog.dismiss();
			
			
		};}.start();
		
		
	}



你可能感兴趣的:(android对话框)