创建AlertDialog的步骤:
1、创建AlertDialog.Builder对象
2、调用Builder对象的setTitle方法设置标题,setIcon方法设置图标
3、调用Builder相关方法如setMessage方法、setItems方法、setSingleChoiceItems方法、setMultiChoiceItems方法、setAdapter方法、setView方法设置不同类型的对话框内容。
4、调用setPositiveButton、setNegativeButton、setNeutralButton设置多个按钮
5、调用Builder对象的create()方法创建AlertDialog对象
6、调用AlertDialog对象的show()方法将对话框显示出来
下面来看例子:
创建一个DialogActivity。
在activity_dialog.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">
<Button
android:id="@+id/btn_dialog_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="style1"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_dialog_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="style2"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_dialog_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="style3"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_dialog_d"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="style4"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btn_dialog_e"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="style5"
android:textAllCaps="false"
/>
</LinearLayout>
在DialogActivity中修改代码:
package net.nyist.lenovo.mytest;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import net.nyist.lenovo.mytest.util.ToastUtil;
public class DialogActivity extends AppCompatActivity {
private Button mBtnDialog1,mBtnDialog2,mBtnDialog3,mBtnDialog4,mBtnDialog5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
mBtnDialog1 = (Button) findViewById(R.id.btn_dialog_a);
mBtnDialog2 = (Button) findViewById(R.id.btn_dialog_b);
mBtnDialog3 = (Button) findViewById(R.id.btn_dialog_c);
mBtnDialog4 = (Button) findViewById(R.id.btn_dialog_d);
mBtnDialog5 = (Button) findViewById(R.id.btn_dialog_e);
OnClick onClick = new OnClick();
mBtnDialog1.setOnClickListener(onClick);
mBtnDialog2.setOnClickListener(onClick);
mBtnDialog3.setOnClickListener(onClick);
mBtnDialog4.setOnClickListener(onClick);
mBtnDialog5.setOnClickListener(onClick);
}
class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_dialog_a:
//创建AlertDialog
AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
//设置标题
builder.setTitle("请回答").setMessage("你是猪吗?")
//设置图标
.setIcon(R.drawable.pig)
//设置积极按钮
.setPositiveButton("是啊", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你很诚实", Toast.LENGTH_SHORT).show();
//中性按钮
}
}).setNeutralButton("必须的", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你很聪明", Toast.LENGTH_SHORT).show();
//消极按钮
}
}).setNegativeButton("你才是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你很坏", Toast.LENGTH_SHORT).show();
}
}).show();
break;
case R.id.btn_dialog_b:
//首先提供数组用来选择
final String[] array = new String[]{"男","女"};
AlertDialog.Builder builder_b = new AlertDialog.Builder(DialogActivity.this);
//使用setItems()增加一个可选列表,该列表接受一个选项名称的列表
// 和一个DialogInterface.OnClickListener, 后者定义了选项对应的响应。
builder_b.setTitle("选择性别").setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(DialogActivity.this,array[which]);
}
}).show();
break;
case R.id.btn_dialog_c:
final String[] array_c = new String[]{"男","女"};
AlertDialog.Builder builder_c = new AlertDialog.Builder(DialogActivity.this);
//setSingleChoiceItems()设置对话框内容为单选列表项,第二个参数为默认选项,这里选择为男
builder_c.setTitle("选择性别").setSingleChoiceItems(array_c, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ToastUtil.showMsg(DialogActivity.this,array_c[which]);
dialog.dismiss();
}
//默认点击对话框外部或者点击后退键使对话框消失,但是我们不想要这样怎么办?
//.setCancelable(false)
//直接设置视图不能取消。
}).setCancelable(false).show();
break;
case R.id.btn_dialog_d:
final String[] array_d = new String[]{"操作系统","数据结构","操作系统"};
final boolean[] isSelected = new boolean[]{false,false,false};
AlertDialog.Builder builder_d = new AlertDialog.Builder(DialogActivity.this);
// setMultiChoiceItems()设置对话框内容为多选项列表
builder_d.setTitle("选择课程").setMultiChoiceItems(array_d, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
ToastUtil.showMsg(DialogActivity.this,array_d[which]+isChecked);
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
break;
case R.id.btn_dialog_e:
AlertDialog.Builder builder_e = new AlertDialog.Builder(DialogActivity.this);
View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog,null);
EditText etUserName = (EditText) view.findViewById(R.id.et_login);
EditText etPassWord = (EditText) view.findViewById(R.id.et_password);
Button btnLogin = (Button) view.findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(DialogActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
}
});
builder_e.setTitle("请先登录").setView(view).show();
break;
}
}
}
}
在 case R.id.btn_dialog_e中我们自定义了一个布局,登陆的界面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<EditText
android:id="@+id/et_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="username"
android:maxLines="1"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="password"
android:inputType="textPassword"
android:maxLines="1"
android:layout_marginTop="10dp"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="login"
android:textAllCaps="false"
/>
</LinearLayout>
运行效果图如下:
setIcon
setItems
setSingleChoiceItems
setMultiChoiceItems
setView
学习内容来自@天哥在奔跑。