欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android常用的AlertDialog对话框及自定义对话框

程序员文章站 2022-06-06 17:19:15
常用的dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框 alertdialog的常用方法 settitle:为对话框设置标...

常用的dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框

alertdialog的常用方法

settitle:为对话框设置标题

setmessage:为对话框设置内容

seticon:为对话框设置图标

setitems设置对话框要显示的list

setmultichoiceitems:一般用于复选框显示

setsinglechoiceitem:,设置单选按钮

setneutralbutton:普通按钮

setpositivebutton:添加确定按钮

setnegativebutton:添加取消按钮

setview:设置自定义样式

下面通过一个实例来了解这些方法

这是运行结果:

Android常用的AlertDialog对话框及自定义对话框

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> 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!