前言
本节给大家带来是显示提示信息的第三个控件AlertDialog(对话框),查看源码知道是Dialog的子类!ProgressDialog,TimePickerDialog父类等。另外,会发现构造方法是保护的,如果我们要创建AlertDialog的话,我们需要使用到该类中的一个静态内部类:public static class Builder,然后来调用AlertDialog里的相关方法,来对AlertDialog进行定制,最后调用show()方法来显示我们的AlertDialog对话框!好的,下面我们就来学习AlertDialog的基本用法,以及定制我们的AlertDialog!
地址
http://blog.csdn.net/xiangyong_1521/article/details/50528403
目录
- 实现效果
- 实现过程
- 主要代码
一.实现效果
二.实现过程
- 创建AlertDialog.Builder对象;
- 调用setIcon()来设置图标,的setTitle()或setCustomTitle()设置标题;
- 设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
- 调用setPositive / Negative / NeutralButton()设置:确定,取消,中立按钮;
- 调用创建()方法创建这个对象,再调用显示()方法将对话框显示出来;
三.主要代码
- MianActivity.java
package com.Evan.demo_alertdialog; import android.R.menu; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button dialog_1,dialog_2,dialog_3,dialog_4; private AlertDialog alert; private AlertDialog.Builder builder=null; private boolean[] checkItems; private final String[] Loves={ "benz","BMW","Lamborghini","Porsche","Audi" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SetView(); } private void SetView() { dialog_1=(Button) findViewById(R.id.button1); dialog_2=(Button) findViewById(R.id.button2); dialog_3=(Button) findViewById(R.id.button3); dialog_4=(Button) findViewById(R.id.button4); } public void onClick(View v){ switch (v.getId()) { /* * 普通选择对话框 */ case R.id.button1: alert=null; builder=new AlertDialog.Builder(MainActivity.this); alert=builder.setIcon(R.drawable.aa4)//图标 .setTitle("标题设置") .setMessage("弹出主题设置") .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您选择了:取消", 0).show(); } }).setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "您选择了:确认", 0).show(); } }).create();//创建 alert.show();//显示 break; /* * 普通单选对话框 */ case R.id.button2: alert=null; builder=new AlertDialog.Builder(MainActivity.this); alert=builder.setIcon(R.drawable.aa4) .setTitle("标题设置") .setItems(Loves, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了:" Loves[which], 0).show(); } }).create(); alert.show(); break; /* * 单项选择对话框 */ case R.id.button3: alert=null; builder=new AlertDialog.Builder(MainActivity.this); alert=builder.setIcon(R.drawable.aa4) .setTitle("标题设置") .setSingleChoiceItems(Loves, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你选择了" Loves[which], Toast.LENGTH_SHORT).show(); } }).create(); alert.show(); break; /* * 多项选择对话框 */ case R.id.button4: checkItems = new boolean[]{false, false, false, false, false}; alert=null; builder=new AlertDialog.Builder(MainActivity.this); alert=builder.setIcon(R.drawable.aa4) .setTitle("标题设置") .setMultiChoiceItems(Loves, checkItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkItems[which]=isChecked; } }).setPositiveButton("确实选择", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result=""; for(int i=0;i<checkItems.length;i ){ if(checkItems[i]) result =Loves[i] " "; } Toast.makeText(getApplicationContext(), "选择的是:" result, Toast.LENGTH_SHORT).show(); } }) .create(); alert.show(); break; } } }
- 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" tools:context=".MainActivity" > <Button android:id="@ id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="14dp" android:onClick="onClick" android:text="普通选择对话框" /> <Button android:id="@ id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@ id/button1" android:onClick="onClick" android:text="普通单选对话框" /> <Button android:id="@ id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@ id/button1" android:layout_below="@ id/button2" android:onClick="onClick" android:text="单项选择对话框" /> <Button android:id="@ id/button4" android:layout_width="match_parent" android:onClick="onClick" android:layout_height="wrap_content" android:layout_alignLeft="@ id/button2" android:layout_below="@ id/button3" android:text="多项选择对话框" /> </RelativeLayout>