一、前言:
本节继续给大家带来是显示提示信息的第三个控件AlertDialog(对话框),同时它也是其他
Dialog的的父类!比如ProgressDialog,TimePickerDialog等,而AlertDialog的父类是:Dialog!
另外,不像前面学习的Toast和Notification,AlertDialog并不能直接new出来,如果你打开
AlertDialog的源码,会发现构造方法是protected的,如果我们要创建AlertDialog的话,我们
需要使用到该类中的一个静态内部类:public static class Builder,然后来调用AlertDialog
里的相关方法,来对AlertDialog进行定制,最后调用show()方法来显示我们的AlertDialog对话框!
好的,下面我们就来学习AlertDialog的基本用法,以及定制我们的AlertDialog!
官方文档:AlertDialog
二、方法:
- Step 1:创建AlertDialog.Builder对象;
- Step 2:调用setIcon()设置图标,setTitle()或setCustomTitle()设置标题;
- Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;
- Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;
- Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;
四、贴代码:MainActivity.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;
}
}
}
布局:
<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>