android AlertDialog多种使用方法详解
程序员文章站
2024-02-22 11:55:22
当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用alertdialog来完成。下面来介绍常用的四种alertdialog。
1、普通对话框...
当你的应用需要显示一个进度条或需要用户对信息进行确认时,可以使用alertdialog来完成。下面来介绍常用的四种alertdialog。
1、普通对话框
package com.example.yk.dialogtest; import android.content.dialoginterface; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.toast; /** * alertdialog普通对话框 */ public class generaldialogactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_general_dialog); alertdialog.builder alertdialog = new alertdialog.builder(this) .settitle("操作title")//设置title .setmessage("操作message")//设置要显示的message .setcancelable(false)//表示点击dialog其它部分不能取消(除了“取消”,“确定”按钮) .setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { toast.maketext(generaldialogactivity.this, "点击了确定", toast.length_short).show(); } }).setnegativebutton("取消", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { // dialoginterface.dismiss(); } }); alertdialog.show();//别忘了show } }
2、单选对话框
package com.example.yk.dialogtest; import android.content.dialoginterface; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.toast; /** * 单选对话框 */ public class singledialogactivity extends appcompatactivity { private string[] items={"java","php","c"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_single_dialog); alertdialog.builder alertdialog = new alertdialog.builder(this) .settitle("提示title") // .setmessage("提示message")//在需要设置单选对话框的情况下是不能设置message的,否则单选对话框内容会失效 .setsinglechoiceitems(items, -1, new dialoginterface.onclicklistener() {//checkeditem=-1表示默认不选中 @override public void onclick(dialoginterface dialoginterface, int i) { toast.maketext(singledialogactivity.this, "选中了"+items[i], toast.length_short).show(); } }).setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { } }); alertdialog.show(); } }
3、多选对话框
package com.example.yk.dialogtest; import android.content.dialoginterface; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.widget.toast; /** * 多选对话框 */ public class multichoicedialogactivity extends appcompatactivity { private string[] items={"java","php","c"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_multi_choice_dialog); alertdialog.builder alertdialog = new alertdialog.builder(this) .settitle("提示title") .setcancelable(false) .setmultichoiceitems(items, new boolean[]{false, false, false}, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i, boolean b) { if(b){ toast.maketext(multichoicedialogactivity.this, "选中了"+items[i], toast.length_short) .show(); } } }) .setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialoginterface, int i) { } }); alertdialog.show(); } }
4、进度条对话框
package com.example.yk.dialogtest; import android.app.progressdialog; import android.os.handler; import android.os.message; import android.support.v7.app.appcompatactivity; import android.os.bundle; /** * 进度条对话框 */ public class progressdialogactivity extends appcompatactivity { private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_progress_dialog); progressdialog = new progressdialog(this); progressdialog.settitle("提示title"); progressdialog.setcancelable(true); // progressdialog.setprogressstyle(progressdialog.style_spinner);//旋转进度条,默认风格 progressdialog.setprogressstyle(progressdialog.style_horizontal);//横向进度条 progressdialog.show(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: python:socket传输大文件示例