Android AlertDialog的几种用法详解
程序员文章站
2022-03-30 12:53:38
alertdialog的几种用法xml代码:
alertdialog的几种用法
xml代码:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.lesson7_3_id19_alertdialog.mainactivity"> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="简单的dialog" android:onclick="dialog_1"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表的dialog" android:onclick="dialog_2"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选的dialog" android:onclick="dialog_3"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选的dialog" android:onclick="dialog_4"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定义view的dialog" android:onclick="dialog_5"/> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用adapter的dialog" android:onclick="dialog_6"/> </linearlayout>
java代码:
package com.example.lesson7_3_id19_alertdialog; import android.app.alertdialog; import android.content.dialoginterface; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.toast; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } public void dialog_1(view v){ alertdialog.builder builder = new alertdialog.builder(this); builder.seticon(r.mipmap.ic_launcher_round); builder.settitle("标题栏"); builder.setmessage("正文部分,简单的文本"); builder.setpositivebutton("确定",new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "点击了确定", toast.length_short).show(); } }); builder.setnegativebutton("取消",null); builder.setneutralbutton("中立",null); alertdialog alertdialog = builder.create(); alertdialog.show(); } private string [] item = {"游戏","运动","电影","旅游","看书"}; public void dialog_2(view v){ alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("请选择"); builder.setitems(item, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "选择了"+item[which], toast.length_short).show(); } }); // 取消可以不添加 //builder.setnegativebutton("取消",null); alertdialog alertdialog = builder.create(); alertdialog.show(); } int index; public void dialog_3(view v){ alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("请选择"); builder.setsinglechoiceitems(item, index, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { index = which; } }); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "选择了"+item[index], toast.length_short).show(); } }); builder.setnegativebutton("取消",null); alertdialog alertdialog = builder.create(); alertdialog.show(); } // 设置boolean数组所有的选项设置默认没选 boolean[] bools = {false,false,false,false,false}; public void dialog_4(view v){ alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("请选择"); builder.setmultichoiceitems(item, bools, new dialoginterface.onmultichoiceclicklistener() { @override public void onclick(dialoginterface dialog, int which, boolean ischecked) { bools[which] = ischecked; } }); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { stringbuffer sb = new stringbuffer(); for (int i = 0; i < item.length; i++) { if (bools[i]) { sb.append(item[i] + " "); } } toast.maketext(mainactivity.this, "选择了" + sb.tostring(), toast.length_short).show(); } }); builder.setnegativebutton("取消",null); alertdialog alertdialog = builder.create(); alertdialog.show(); } public void dialog_5(view v){ alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("连接wifi"); final edittext et = new edittext(this); et.sethint("请输入密码"); et.setsingleline(true); builder.setview(et); builder.setnegativebutton("取消",null); builder.setpositivebutton("确定", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { string password = et.gettext().tostring(); if (password.equals("123456")) { toast.maketext(mainactivity.this, "连接成功", toast.length_short).show(); }else{ toast.maketext(mainactivity.this, "密码错误", toast.length_short).show(); } } }); alertdialog alertdialog = builder.create(); alertdialog.show(); } public void dialog_6(view v){ arrayadapter<string> adapter = new arrayadapter<string>(this,android.r.layout.simple_list_item_1,item); alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("使用适配器"); builder.setadapter(adapter, new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { toast.maketext(mainactivity.this, "选择了"+item[which], toast.length_short).show(); } }); alertdialog alertdialog = builder.create(); alertdialog.show(); } }
到此这篇关于android alertdialog的几种用法详解的文章就介绍到这了,更多相关android alertdialog方法内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!