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(); }
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(); }
以上所述是小编给大家介绍的Android 常见的四种对话框实例讲解,希望对大家有所帮助,如果大家有任何疑问请给我们留言,小编会及时回复大家的。在此也非常感谢大家对查字典教程网的支持!