一.对话框的分类
1.警告对话框 AlertDialog
1>一般对话框
2>单选对话框
3>复选对话框
4>自定义对话框
2.进度对话框
3.日期对话框
4.时间对话框
二.警告对话框 AlertDialog
1.一般对话框
1>不能直接实例化使用
2>使用内部构造器来生成对话框
3>new AlertDialog.Builder(context) 实例化构造器
1-setTitle (标题)
2-setMessage (消息)
3-按钮
1°确认按钮 setPositiveButton(“文字”,点击事件监听器)
2°否认按钮 setNegativeButton(“文字”,点击事件监听器)
3°中立按钮 setNeutralButton(“文字”,点击事件监听器)
4-show() 创建后显示对话框,并返回AlertDialog实例
5-create() 生成对话框并返回
6-setCancelable(true/false)
1°设置是否能点击其他地方关闭对话框
2°类似与模态窗口

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.example.wang.testapp2.TestActivity5" 11 android:orientation="vertical"> 12 13 14 <Button 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="一般对话框" 18 android:onClick="bt1_OnClick" 19 /> 20 21 </LinearLayout>

1 package com.example.wang.testapp2; 2 3 import android.app.AlertDialog; 4 import android.content.DialogInterface; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Toast; 9 10 public class TestActivity5 extends AppCompatActivity { 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.activity_test5); 16 17 } 18 19 //一般对话框 20 public void bt1_OnClick(View v) 21 { 22 //对话框不能直接实例化 23 //内部提供了构造器 24 //方法链调用 25 AlertDialog alertDialog=new AlertDialog.Builder(this) 26 .setTitle("确认对话框") 27 .setMessage("你确实要删除吗?") 28 .setPositiveButton("确认", new DialogInterface.OnClickListener() { 29 @Override 30 public void onClick(DialogInterface dialog, int which) { 31 32 Toast.makeText(TestActivity5.this, "执行删除,which="+which, Toast.LENGTH_SHORT).show(); 33 34 } 35 })//正面按钮 36 .setNegativeButton("取消", new DialogInterface.OnClickListener() { 37 @Override 38 public void onClick(DialogInterface dialog, int which) { 39 40 Toast.makeText(TestActivity5.this, "取消删除,which="+which, Toast.LENGTH_SHORT).show(); 41 42 } 43 })//负面按钮 44 .setNeutralButton("中立", new DialogInterface.OnClickListener() { 45 @Override 46 public void onClick(DialogInterface dialog, int which) { 47 48 Toast.makeText(TestActivity5.this, "普通按钮,which="+which, Toast.LENGTH_SHORT).show(); 49 50 } 51 }) 52 .setCancelable(false) 53 .show();//显示对话框 54 } 55 56 }
小结: