layout.xml
<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:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_alertDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="普通对话框" android:onClick="onShowDialog" android:textSize="20sp"/>" <Button android:id="@+id/btn_itemsDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="列表对话框" android:onClick="onShowDialog" android:textSize="20sp"/> <Button android:id="@+id/btn_singleChoiceDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选对话框" android:onClick="onShowDialog" android:textSize="20sp"/> <Button android:id="@+id/btn_multiChoiceDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选对话框" android:onClick="onShowDialog" android:textSize="20sp"/> <Button android:id="@+id/btn_adapterDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="适配器对话框" android:onClick="onShowDialog" android:textSize="20sp"/> <EditText android:id="@+id/btn_dataPickerDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2016-2-9" android:focusable="false" android:onClick="onShowDialog" android:textSize="20sp"/> <!-- android:focusable="false" 设置文本不聚焦不可以编辑--> <Button android:id="@+id/btn_timePickerDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时间选择对话框" android:onClick="onShowDialog" android:textSize="20sp"/> <Button android:id="@+id/btn_userDefinedDialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户自定义对话框" android:onClick="onShowDialog" android:textSize="20sp"/> </LinearLayout>
face_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="horizontal" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> </LinearLayout>
layout_customer.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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="20sp"/> <EditText android:id="@+id/et_user" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="请输入用户名:" android:inputType="text" android:textSize="15sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textSize="20sp"/> <EditText android:id="@+id/et_psd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="numberPassword" android:hint="请输入密码:" android:textSize="15sp"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:textSize="20sp" android:onClick="onClick" android:layout_marginLeft="10dp" /> <Button android:id="@+id/cansel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:textSize="20sp" android:onClick="onClick" android:layout_gravity="right" android:layout_marginRight="10dp" /> </LinearLayout> </LinearLayout>
main.java
package com.example.day10_dialog; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.SimpleAdapter; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private Context context; private EditText et_date; private AlertDialog alertDialog; private Button btn_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.context = MainActivity.this; et_date = (EditText) findViewById(R.id.btn_dataPickerDialog); } /** * * @param v */ public void onShowDialog(View v){ switch (v.getId()) { case R.id.btn_alertDialog://普通对话框 showalertDialog(); break; case R.id.btn_itemsDialog://列表对话框 showItemDialog(); break; case R.id.btn_singleChoiceDialog://单选对话框 showSingleChoiceDialog(); break; case R.id.btn_multiChoiceDialog://多选对话框 showMultiChioceDialog(); break; case R.id.btn_adapterDialog://适配器对话框 showAdapterDialog(); break; case R.id.btn_dataPickerDialog://日期选择对话框 showDataPickerDialog(); break; case R.id.btn_timePickerDialog://时间选择对话框 showTimePickerDialog(); break; case R.id.btn_userDefinedDialog://用户自定义对话框 showUserDefinedDialog(); break; } } /** * 用户自定义对话框 */ private void showUserDefinedDialog() { // AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("自定义对话框");//对第二种方法无效,被窗体覆盖 //把布局文件转化成view--第一种 /* View view = getLayoutInflater().inflate(R.layout.layout_customer, null); //设置显示的视图 builder.setView(view); //展示视图 builder.show();*/ AlertDialog alertDialog = builder.create(); alertDialog.show(); //第二种 //该方法要在show()之后, //获得dialog的window对象 Window window = alertDialog.getWindow(); //为window对象设置布局 window.setContentView(R.layout.layout_customer); //在window中找到相应的控件 btn_login = (Button) window.findViewById(R.id.login); //为控件设置监听事件 btn_login.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, btn_login.getText().toString(), 1).show(); } }); } /** * 时间选择对话框 */ private void showTimePickerDialog() { //创建时间选择对话框,第二个参数是设置点击事件,第三个和第四个参数是设置默认的时与分,第五个参数是是否为24小时制 TimePickerDialog timePickerDialog = new TimePickerDialog(context, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } }, 12, 23, true); //设置标题 timePickerDialog.setTitle("设置闹钟"); //展示对话框 timePickerDialog.show(); } /** * 日期选择对话框 */ private void showDataPickerDialog() { //创建日期选择对话框 后面3个参数分别表示默认的年 月 日 DatePickerDialog datePickerDialog = new DatePickerDialog(context, new OnDateSetListener() { //当日期选择好点击确定调用,会将设定的新的日期传过来 @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { et_date.setText(year+"-"+(monthOfYear+1)+"-"+dayOfMonth); } }, 2001, 4, 4);//注意月份是从零开始,所以默认值是2001/5/4 //展示对话框 datePickerDialog.show(); } /** * 适配器对话框 */ private void showAdapterDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("请选择你最喜欢的表情"); String[] text = {"调皮","无语","偷笑","再见","敲打","流汗"}; final int[] image = {R.drawable.f001,R.drawable.f002,R.drawable.f003,R.drawable.f004,R.drawable.f005,R.drawable.f006}; List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object>map; for (int i = 0; i < image.length; i++) { map = new HashMap<String,Object>(); map.put("text", text[i]); map.put("image", image[i]); list.add(map); } SimpleAdapter adapter = new SimpleAdapter(context, list, R.layout.face_item, new String[]{"text","image"}, new int[]{R.id.text,R.id.image}); //为对话框设置适配器 builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你喜欢的表情是"+image[which], 0).show(); } }); builder.show(); } /** * 多选对话框 */ private void showMultiChioceDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("你的爱好是:"); final String items[] = {"吃饭","睡觉","打豆豆","玩游戏"}; final boolean checkedItems[] = {false,false,false,true}; /** * items 设置多选对话框的条目 * checkedItems 设置默认选择的项,必须与第一个参数中的值一一对应并且该数组中的值会随着对话框中条目的选中与否自动改变 * listener */ final List<String>list = new ArrayList<String>(); builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //方法一:保存选择的爱好,需设置checkedItems为null if(isChecked){ list.add(items[which]); }else{ list.remove(items[which]); } } }); //点击确定按钮弹出toast显示爱好 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //方法二:利用checkedItems String result = ""; for (int i = 0; i < checkedItems.length; i++) { if(checkedItems[i]){ result += items[i]; } } Toast.makeText(context, "你的爱好是:"+result, 0).show(); } }); builder.show(); } /** * 展示单选对话框 */ private void showSingleChoiceDialog() { //创建构造器对象 final AlertDialog.Builder builder = new AlertDialog.Builder(context); //设置标题 builder.setTitle("请选择你的性别"); //设置图标 builder.setIcon(R.drawable.ic_launcher); final String[] items = {"男","女","不详"}; //设置监听事件 /** *items 参数一 数据源 *checkedItem 参数二默认选择的item的索引 *listener 参数三 监听事件 * */ builder.setSingleChoiceItems(items,1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你的性别为:"+items[which], 0).show(); alertDialog.dismiss();//点击单选按钮后关闭对话框 } }); //先创建再显示,一般直接写为builder.show() alertDialog = builder.create(); alertDialog.show(); } /** * 展示列表对话框 */ private void showItemDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("请选择一种水果:"); final String[] data = {"苹果","香蕉","梨"}; //点击后会自动关闭对话框 builder.setItems(data, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "你选择了"+data[which], 0).show(); } }); builder.show(); } /** * 普通对话框 */ private void showalertDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("你确定要退出吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "确定退出", 0).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, "取消退出", 0).show(); } }); builder.show(); } }
对话框效果图:
普通对话框:
列表对话框
单选对话框
多选对话框
适配器对话框
日期选择对话框
时间选择对话框
自定义对话框