Android 常见对话框
1、对话框通知(Dialog Notification)
当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用对话框来完成。
下面代码将打开一个如图所示的对话框:
public void click1(View view) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("工学1号馆"); builder.setIcon(R.drawable.ic_launcher); builder.setMessage("浏览wuyudong的博客?"); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Uri uri = Uri.parse("http://www.wuyudong.com/");// 打开链接 Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog dialog = builder.create(); dialog.show(); }
本文地址:http://www.cnblogs.com/wuyudong/p/5854896.html,转载请注明源地址。
2、创建带单选项列表的对话框
public void click2(View view) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("单选对话框"); final String[] items = new String[] { "java", ".net", "php" }; builder.setSingleChoiceItems(items, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, items[which] + "被点击了", 0) .show(); } }); builder.show(); }
3、创建带多选项列表的对话框
public void click3(View view) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("多选对话框"); final String[] items = new String[] { "java", ".net", "php", "C++" }; builder.setMultiChoiceItems(items, new boolean[] { true, false, false, true }, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this, items[which] + isChecked, 0) .show(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); }
4、进度对话框(ProgressDialog)
使用代码ProgressDialog.show(ProgressDialogActivity.this, "请稍等", "数据正在加载中...", true);创建并显示一个进度对话框。
调用setProgressStyle()方法设置进度对话框风格。有两种风格:
ProgressDialog.STYLE_SPINNER 旋体进度条风格 (为默认风格)
ProgressDialog.STYLE_HORIZONTAL 横向进度条风格
public void click4(View view) { ProgressDialog pd = new ProgressDialog(this); pd.setTitle("提醒"); pd.setMessage("正在加载中......"); pd.show(); }
下面的代码实现横向进度条风格:
public void click5(View view) { final ProgressDialog pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMax(100); pd.show(); new Thread() { public void run() { for (int i = 0; i < 100; i++) { pd.setProgress(i); try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } pd.dismiss(); }; }.start(); }
作者:wuyudong
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
如果觉得本文对您有帮助,可以对作者进行小额【赞助】