android开发Dialog常见用法

项目中使用Dialog的地方还是非常多的。Dialog一般浮在Activity上边,使得Activity失去焦点,从而使用Dialog进行交互、提示等操作。我计划写两篇博文记录Dialog用法。

第一篇Dialog常见用法。第二篇讲解自定义Dialog、Dialog和Activity之间的关系,什么时机使用Dialog代替Activity,有何好处。


言归正传吧,先说说Dialog常见用法。

1. 确定取消对话框
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("提示");
builder.setMessage("你确定要离开吗?");
builder.setCancelable(false);
 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			//这里添加点击确定后的逻辑
			System.out.println("确定");
		}
	});
	builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			//这里添加点击确定后的逻辑
		   System.out.println("取消");
		}
	});

builder.show();
System.out.println("show after");

可以设置图标、标题、正文。通过测试可以发现,show()之后能打印"show after"说明,android系统默认的对话框是非模态对话框。通过setCancelable设置返回键是否
生效,true能够正常返回,false按下返回键不返回。
 

2.多个按钮对话框
Builder builder2 = new AlertDialog.Builder(this);
builder2.setIcon(R.drawable.ic_launcher);
builder2.setTitle("投票");
builder2.setMessage("您认为什么样的内容能吸引您?");
builder2.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		   System.out.println("有趣味的");
		}
	});
builder2.setNeutralButton("有思想的", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			System.out.println("有思想的");                    
		}
	});
builder2.setNegativeButton("主题强的", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			System.out.println("主题强的");
		}
	});
builder2.show();

3.列表对话框

Builder builder3 = new Builder(this);
builder3.setTitle("列表选择框");
builder3.setItems(mItems, new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			//点击后弹出窗口选择了第几项
			System.out.println("你选择的id:" + which);
		}
	});

4.进度条对话框

mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIcon(R.drawable.ic_launcher);
mProgressDialog.setTitle("进度条窗口");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(MAX_PROGRESS);
mProgressDialog.setButton("确定",
		new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
					int whichButton) {
				// 这里添加点击后的逻辑
			}
		});
mProgressDialog.setButton2("取消",
		new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
					int whichButton) {
				// 这里添加点击后的逻辑
			}
		});
mProgressDialog.show();
new Thread(this).start();
			

在子线程中更新进度
 public void run() {
		    int Progress = 0;
		    while(Progress < MAX_PROGRESS) {
			try {
			    Thread.sleep(100);
			    Progress++;  
			    mProgressDialog.incrementProgressBy(1);
			   
			} catch (InterruptedException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			}
			 
		    }
		
	    }

5.单项列表对话框
Builder builder5 = new Builder(this);
mSingleChoiceID = -1;
builder5.setIcon(R.drawable.ic_launcher);
builder5.setTitle("单项选择");
builder5.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {
		 public void onClick(DialogInterface dialog, int whichButton) {
				 mSingleChoiceID = whichButton;
				System.out.println("你选择的id为" + whichButton + " , " + mItems[whichButton]);
		 }
	 });
builder5.setPositiveButton("确定", new DialogInterface.OnClickListener() {
		 public void onClick(DialogInterface dialog, int whichButton) {
			 if(mSingleChoiceID > 0) {
			 System.out.println("你选择的是" + mSingleChoiceID);
			 }
		 }
	 });
builder5.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		 public void onClick(DialogInterface dialog, int whichButton) {

		 }
	 });
builder5.show();


6.多项选择列表框
MultiChoiceID.clear();
Builder builder6 = new Builder(this);
builder6.setIcon(R.drawable.ic_launcher);
builder6.setTitle("多项选择");
builder6.setMultiChoiceItems(mItems, new boolean[] { false, false,
		false, false, false, false, false },
		new DialogInterface.OnMultiChoiceClickListener() {
			public void onClick(DialogInterface dialog,
					int whichButton, boolean isChecked) {
				if (isChecked) {
					MultiChoiceID.add(whichButton);
					System.out.println("你选择的id为" + whichButton
							+ " , " + mItems[whichButton]);
				} else {
					MultiChoiceID.remove(whichButton);
				}

			}
		});
builder6.setPositiveButton("确定",
		new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
					int whichButton) {
				String str = "";
				int size = MultiChoiceID.size();
				for (int i = 0; i < size; i++) {
					str += mItems[MultiChoiceID.get(i)] + ", ";
				}
				System.out.println("你选择的是" + str);
			}
		});
builder6.setNegativeButton("取消",
		new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog,
					int whichButton) {

			}
		});
builder6.show();

7.自定义布局
这种方式的对话框还是比较常见的。比较系统的对话框不够美观。
Builder builder7 = new Builder(this);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.test, null);
builder7.setIcon(R.drawable.ic_launcher);
builder7.setTitle("自定义输入框");
builder7.setView(textEntryView);
builder7.setPositiveButton("确定", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
		
		EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
		EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
		System.out.println("姓名 :"  + userName.getText().toString()  + "密码:" + password.getText().toString() );
		}
	});
builder7.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {

		}
	});
builder7.show();

8.读取进度对话框
这种类型的对话框用于,进度的不确定性。只是提示用户目前正在读取,还未完毕而已。
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setTitle("读取ing");
mProgressDialog.setMessage("正在读取中请稍候");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setCancelable(true);
mProgressDialog.show();

以上的知识都比较简单,我相信做过android开发的都是知道的。我只是想将自己的基础更加的扎实。将这些Dialog用法记录下来,方便以后查阅。

代码下载地址:点击打开链接













评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值