一.单选对话框
1.常规用法同一般对话框
2.Builder方法:设置单选选项setSingleChoiceItems(选项数组,默认被选中的选项索引,点击监听器DialogInterface.OnClickListener()):
onClick(DialogInterface dialog, int which):①which表示哪一个选项被选中②关闭对话框dialog.dismiss();
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 <Button 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="单选对话框" 16 android:onClick="bt2_OnClick"/> 17 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.TimePickerDialog; 6 import android.content.DialogInterface; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.DatePicker; 11 import android.widget.EditText; 12 import android.widget.ImageView; 13 import android.widget.TimePicker; 14 import android.widget.Toast; 15 16 import java.util.Calendar; 17 18 public class TestActivity5 extends AppCompatActivity { 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test5); 24 } 25 //单选对话框 26 public void bt2_OnClick(View v) 27 { 28 //final 可以使这个常量的生命周期延长到整个对象 29 final String [] str={"男","女"}; 30 new AlertDialog.Builder(this) 31 .setTitle("单选对话框") 32 .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() { 33 @Override 34 public void onClick(DialogInterface dialog, int which) { 35 36 Toast.makeText(TestActivity5.this, "which="+which+",选中的是"+str[which], Toast.LENGTH_SHORT).show(); 37 38 //关闭对话框 39 dialog.dismiss(); 40 } 41 }).setCancelable(false) 42 .show(); 43 } 44 }
二.复选对话框
1.常规用法同一般对话框
2.Builder方法:设置复选选项 setMultiChoiceItems(复选选项数组,每个选项是否选中的数组,点击事件监听器OnMultiChoiceClickListener()):
void onClick(DialogInterface dialog, int which, boolean isChecked):①which 表示哪一个选项被点击②isChecked 表示是否被选中③改变每个选项是否选中数组内对应的值
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 <Button 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="复选对话框" 16 android:onClick="bt3_OnClick"/> 17 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.TimePickerDialog; 6 import android.content.DialogInterface; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.DatePicker; 11 import android.widget.EditText; 12 import android.widget.ImageView; 13 import android.widget.TimePicker; 14 import android.widget.Toast; 15 16 import java.util.Calendar; 17 18 public class TestActivity5 extends AppCompatActivity { 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test5); 24 } 25 //复选对话框 26 public void bt3_OnClick(View v) 27 { 28 final String [] str={"宝马","奔驰","劳斯莱斯","宾利"}; 29 final boolean[] ch={true,false,false,true}; 30 31 new AlertDialog.Builder(this) 32 .setTitle("复选对话框") 33 .setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() { 34 @Override 35 public void onClick(DialogInterface dialog, int which, boolean isChecked) { 36 37 //改变对应数组项的选中状态 38 ch[which]=isChecked; 39 } 40 }) 41 .setPositiveButton("确认", new DialogInterface.OnClickListener() { 42 @Override 43 public void onClick(DialogInterface dialog, int which) { 44 45 int i = 0; 46 //获取最终的选中状态 47 for (boolean b : ch) { 48 Toast.makeText(TestActivity5.this, str[i] + "选中状态=" + (b ? "选中" : "未选中"), Toast.LENGTH_LONG).show(); 49 50 i++; 51 } 52 53 54 } 55 }) 56 .setNegativeButton("取消", null) 57 .setCancelable(false) 58 .show(); 59 } 60 }
三.自定义对话框
1.常规用法同一般对话框
2.Builder方法:setView(View):①用加载器加载layout文件:第一种方式:LayoutInflater layoutInflater=getLayoutInflater( );:layoutInflater.inflate( )
第二种方式:View.inflate(context,Layout文件id, ViewGroup);
②纯java代码定义View
3.获得Layout中的组件:调用容器的findViewById(id):①加载了Layout文件的View②当前自定义对话框实例:在按钮的OnClick回调方法里,AlertDialog a1=(AlertDialog)dialog;
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 <Button 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="自定义对话框" 16 android:onClick="bt4_OnClick"/> 17 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.TimePickerDialog; 6 import android.content.DialogInterface; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.DatePicker; 11 import android.widget.EditText; 12 import android.widget.ImageView; 13 import android.widget.TimePicker; 14 import android.widget.Toast; 15 16 import java.util.Calendar; 17 18 public class TestActivity5 extends AppCompatActivity { 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test5); 24 } 25 //自定义对话框 26 public void bt4_OnClick(View v) 27 { 28 //1.layout文件加载器 29 //LayoutInflater layoutInflater=getLayoutInflater(); 30 31 //View view=layoutInflater.inflate(R.layout.dialoglayout, null); 32 33 //2.view的静态方法 34 //View view=View.inflate(this, R.layout.dialoglayout, null); 35 36 //3.java代码 37 ImageView view=new ImageView(this); 38 39 view.setImageResource(R.drawable.xiao1); 40 41 new AlertDialog.Builder(this) 42 .setTitle("自定义对话框") 43 // .setView(R.layout.dialoglayout) 44 .setView(view) 45 46 .setPositiveButton("确定", null) 47 48 .show(); 49 } 50 }
四.方法
关闭 dismiss( );
补充登陆、日期、时间对话框
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.hanqi.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 <Button 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content" 15 android:text="登陆对话框" 16 android:onClick="bt5_OnClick"/> 17 18 <Button 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:text="日期对话框" 22 android:onClick="bt6_OnClick"/> 23 24 <Button 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:text="时间对话框" 28 android:onClick="bt7_OnClick"/> 29 30 </LinearLayout>
1 package com.hanqi.testapp2; 2 3 import android.app.AlertDialog; 4 import android.app.DatePickerDialog; 5 import android.app.TimePickerDialog; 6 import android.content.DialogInterface; 7 import android.os.Bundle; 8 import android.support.v7.app.AppCompatActivity; 9 import android.view.View; 10 import android.widget.DatePicker; 11 import android.widget.EditText; 12 import android.widget.ImageView; 13 import android.widget.TimePicker; 14 import android.widget.Toast; 15 16 import java.util.Calendar; 17 18 public class TestActivity5 extends AppCompatActivity { 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_test5); 24 } 25 //登录对话框 26 public void bt5_OnClick(View v) 27 { 28 //加载layout文件 29 final View view=View.inflate(this,R.layout.dialog_login,null); 30 31 AlertDialog a1=new AlertDialog.Builder(this) 32 .setTitle("登录对话框") 33 .setView(view) 34 .setPositiveButton("登陆", new DialogInterface.OnClickListener() { 35 @Override 36 public void onClick(DialogInterface dialog, int which) { 37 38 //转换 39 AlertDialog a1=(AlertDialog)dialog; 40 41 //获取输入信息 42 EditText et_1=(EditText)view.findViewById(R.id.et_1); 43 44 Toast.makeText(TestActivity5.this, "用户名="+et_1.getText(), Toast.LENGTH_SHORT).show(); 45 46 //验证并提示 47 } 48 }).setNegativeButton("取消",null) 49 .show(); 50 } 51 52 53 //当前日期 日历 54 55 //单例模式 56 Calendar c=Calendar.getInstance(); 57 58 59 //日期 60 public void bt6_OnClick(View v) 61 { 62 63 64 65 66 DatePickerDialog dp=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() { 67 @Override 68 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 69 70 Toast.makeText(TestActivity5.this, year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); 71 } 72 }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); 73 74 dp.setCancelable(false); 75 76 dp.show(); 77 } 78 79 //时间 80 public void bt7_OnClick(View v) 81 { 82 TimePickerDialog tp=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { 83 @Override 84 public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 85 86 Toast.makeText(TestActivity5.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); 87 } 88 }, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true); 89 90 tp.setCanceledOnTouchOutside(false); 91 92 tp.show(); 93 } 94 95 }