对话框
一.分类
警告对话框AlertDialog:①一般对话框②单选对话框③复选对话框④自定义对话框
进度对话框
日期对话框
时间对话框
二.警告对话框 AlertDialong
1.一般
不能直接实例化使用
使用内部构造器来生成对话框
new AlertDialog.Builder(context)实例化构造器:(1)setTitle(标题)(2)setMessage(消息)
(3)按钮:①确认按钮 setPositiveButton("内容",点击事件监听器)②否认按钮 setNegativeButton("文字",点击事件监听器)③中立按钮 setNeutralButton("文字",点击事件监听器)
(4)show( ) 创建后显示对话框并返回AlertDialog实例(5)creat( ) 生成对话框并返回
(6)setCancelable(true/false) :①设置是否能点击其他地方关闭对话框②类似于模态窗口
2.单选对话框
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="com.hanqi.testapp2.TestActivity5" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="一般对话框" android:onClick="bt1_OnClick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选对话框" android:onClick="bt2_OnClick"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="复选对话框" android:onClick="bt3_OnClick"/> </LinearLayout>
package com.hanqi.testapp2; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class TestActivity5 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test5); } //一般对话框 public void bt1_OnClick(View v) { //对话框不能直接实例化 //内部提供了构造器 //方法链调用 AlertDialog alertDialog=new AlertDialog.Builder(this) .setTitle("确认对话框") .setMessage("你确实要删除吗?") .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "执行删除,which="+which, Toast.LENGTH_SHORT).show(); } })//正面按钮 .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "取消删除,which="+which, Toast.LENGTH_SHORT).show(); } })//负面按钮 .setNeutralButton("中立", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "普通按钮,which"+which, Toast.LENGTH_SHORT).show(); } }).setCancelable(false) .show();//显示对话框 } //单选对话框 public void bt2_OnClick(View v) { //final 可以使这个常量的生命周期延长到整个对象 final String [] str={"男","女"}; new AlertDialog.Builder(this) .setTitle("单选对话框") .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(TestActivity5.this, "which="+which+",选中的是"+str[which], Toast.LENGTH_SHORT).show(); //关闭对话框 dialog.dismiss(); } }).setCancelable(false) .show(); } //复选对话框 public void bt3_OnClick(View v) { final String [] str={"宝马","奔驰","劳斯莱斯","宾利"}; final boolean[] ch={true,false,false,true}; new AlertDialog.Builder(this) .setTitle("复选对话框") .setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //改变对应数组项的选中状态 ch[which]=isChecked; } }) .setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int i=0; //获取最终的选中状态 for(boolean b:ch) { Toast.makeText(TestActivity5.this, str[i]+"选中状态="+(b?"选中":"未选中"), Toast.LENGTH_LONG).show(); i++; } } }) .setNegativeButton("取消", null) .setCancelable(false) .show(); } }