Android常用的AlertDialog对话框及自定义对话框
程序员文章站
2022-06-06 17:19:15
常用的dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框
alertdialog的常用方法
settitle:为对话框设置标...
常用的dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框
alertdialog的常用方法
settitle:为对话框设置标题
setmessage:为对话框设置内容
seticon:为对话框设置图标
setitems设置对话框要显示的list
setmultichoiceitems:一般用于复选框显示
setsinglechoiceitem:,设置单选按钮
setneutralbutton:普通按钮
setpositivebutton:添加确定按钮
setnegativebutton:添加取消按钮
setview:设置自定义样式
下面通过一个实例来了解这些方法
这是运行结果:
mainactivity.class
package com.example.alertdialog; import android.r.bool; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.app.alertdialog.builder; import android.app.dialog; import android.content.dialoginterface; import android.content.pm.labeledintent; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.toast; public class mainactivity extends activity implements onclicklistener { private button button1; private button button2; private button button3; private button button4; private button button5; private int mark=0; private string item[] = { "学生", "工人", "教师", "农民" }; private string multchoice[]={"旅游","电影","运动","读书"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button1 = (button) findviewbyid(r.id.btn_button1); button2 = (button) findviewbyid(r.id.btn_button2); button3 = (button) findviewbyid(r.id.btn_button3); button4 = (button) findviewbyid(r.id.btn_button4); button5 = (button) findviewbyid(r.id.btn_button5); button1.setonclicklistener(this); button2.setonclicklistener(this); button3.setonclicklistener(this); button4.setonclicklistener(this); button5.setonclicklistener(this); } @override public void onclick(view v) { // todo auto-generated method stub switch (v.getid()) { //确认对话框 case r.id.btn_button1: { alertdialog.builder builder = new builder(this); builder.seticon(r.drawable.ic_launcher); builder.settitle("确认对话框"); builder.setmessage("确认退出?"); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub toast.maketext(mainactivity.this, "你单击了确定按钮", toast.length_short).show(); } }); builder.setnegativebutton("取消", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub toast.maketext(mainactivity.this, "你单击了取消按钮", toast.length_short).show(); } }); builder.create(); builder.show(); break; } //列表对话框 case r.id.btn_button2: { alertdialog.builder builder = new builder(this); builder.seticon(r.drawable.ic_launcher); builder.settitle("职业"); builder.setitems(item, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub toast.maketext(mainactivity.this, "你的职业是" + item[which], toast.length_short).show(); } }); builder.create(); builder.show(); break; } //多选对话框 case r.id.btn_button3: { final boolean choose[]=new boolean[multchoice.length]; alertdialog.builder builder = new builder(this); builder.seticon(r.drawable.ic_launcher); builder.settitle("爱好"); builder.setmultichoiceitems(multchoice, null, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int which, boolean ischecked) { // todo auto-generated method stub choose[which]=ischecked; } }); builder.setpositivebutton("确认", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub string result=""; for(int i=0;i<multchoice.length;i++){ if(choose[i]){ result+=multchoice[i]+" "; } } toast.maketext(mainactivity.this, "你的爱好["+result+"]", toast.length_short).show(); } }); builder.create(); builder.show(); break; } //单选对话框 case r.id.btn_button4: { mark=0; alertdialog.builder builder = new builder(this); builder.seticon(r.drawable.ic_launcher); builder.settitle("职业"); builder.setsinglechoiceitems(item, 0, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub mark=which; } }); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub toast.maketext(mainactivity.this, "你的职业:"+item[mark], toast.length_short).show(); } }); builder.create(); builder.show(); break; } //自定义对话框 case r.id.btn_button5: { layoutinflater inflater=layoutinflater.from(this); view view=inflater.inflate(r.layout.item, null); alertdialog.builder builder = new builder(this); builder.seticon(r.drawable.ic_launcher); builder.settitle("自定义对话框"); builder.setview(view); builder.setneutralbutton("普通按钮", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub toast.maketext(mainactivity.this,"我是自定义的对话框哦",toast.length_short).show(); } }); builder.create(); builder.show(); break; } } } }
布局文件
activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <button android:id="@+id/btn_button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确认对话框" /> <button android:id="@+id/btn_button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="列表对话框" /> <button android:id="@+id/btn_button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多选对话框" /> <button android:id="@+id/btn_button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单选对话框" /> <button android:id="@+id/btn_button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义对话框" /> </linearlayout> </relativelayout>
自定义的对话框布局文件
item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:scaletype="fitcenter" android:src="@drawable/icon" /> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="我是自定义的对话框" /> </linearlayout>
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
上一篇: 求大牛指导 框架经验
下一篇: php常用函数-字符串
推荐阅读
-
Android自定义控件实现万能的对话框
-
Android使用AlertDialog实现的信息列表单选、多选对话框功能
-
Android编程自定义对话框(Dialog)位置及大小的方法
-
Android中自定义对话框(Dialog)的实例代码
-
Android修改源码解决Alertdialog触摸对话框边缘消失的问题
-
Android自定义控件实现万能的对话框
-
Android中使用AlertDialog实现几种不同的对话框
-
Android中AlertDialog四种对话框的最科学编写用法(实例代码)
-
Android常用的AlertDialog对话框及自定义对话框
-
Android使用AlertDialog实现的信息列表单选、多选对话框功能