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() {@OverrIDepublic 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("取消",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,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","PHP","C++" };builder.setMultiChoiceItems(items,new boolean[] { true,false,true },new OnMultiChoiceClickListener() {@OverrIDepublic voID onClick(DialogInterface dialog,int which,boolean isChecked) {Toast.makeText(MainActivity.this,items[which] + isChecked,0).show();}});builder.setNegativebutton("取消",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 常见的四种对话框实例讲解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 常见的四种对话框实例讲解全部内容,希望文章能够帮你解决Android 常见的四种对话框实例讲解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)