第3章 Android常见界面控件
3.2 AlertDialog对话框的使用
3.2.1 AlertDialog对话框概述
- AlertDialog对话框用于提示一些重要信息或者显示一些需要用户额外交互的内容。它一般以小窗口的形式展示在界面上。使用AlertDialog创建的对话框一般包含标题、内容和按钮三个区域。
- 结构图
- 创建AlertDialog对话框步骤
- 调用AlertDialog的静态内部类Builder创建AlertDialog.Builder的对象。
- 调用AlertDialog.Builder的setTitle()和setIcon()方法分别设置AlertDialog对话框的标题名称和图标。
- 调用AlertDialog.Builder的setMessage()、setSingleChoiceItems()或者setMultiChoiceItems()方法设置AlertDialog对话框的内容为简单文本、单选列表或者多选列表。
- 调用AlertDialog.Builder的setPositiveButton()和setNegativeButton()方法设置AlertDialog对话框的确定和取消按钮。
- 调用AlertDialog.Builder的create()方法创建AlertDialog对象。
- 调用AlertDialog对象的show()方法显示该对话框。
- 调用AlertDialog对象的dismiss()方法取消该对话框。
3.2.2 普通对话框
- 普通对话框的内容区域一般显示简单的文本信息。通过setMessage()方法设置的。
package com.ywp.commondialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("普通对话框")
.setMessage("是否确定退出应用?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
MainActivity.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
3.2.3 单选对话框
- 单选对话框的内容区域显示为单选列表。单选列表通过AlertDialog.Builder对象调用setSingleChoiceItems()方法设置的。
<?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">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="单选对话框"
android:textColor="#FFFDB371"
android:textSize="20sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="设置字体大小" />
</LinearLayout>
package com.ywp.singlechoicedialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private int textSize = 1;
private int[] textSizeArr = {10, 20, 25, 30, 40};
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("单选对话框")
.setSingleChoiceItems(new String[]{"小号", "默认", "中号", "大号", "超大"}, textSize, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
textSize = i;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
int size = textSizeArr[textSize];
textView.setTextSize(size);
dialogInterface.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
3.2.4 多选对话框
- 多选对话框的内容区域显示为多选对话框。多选列表通过AlertDialog.Builder对象调用setMultiChoiceItems()方法设置的。
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启兴趣爱好小调查" />
</LinearLayout>
package com.ywp.multichoicedialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CharSequence[] items = new CharSequence[]{"游泳", "美食", "看定影", "运动"};
private boolean[] checkedItems = new boolean[]{false, false, false, false};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("请添加兴趣爱好!")
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
checkedItems[i] = b;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
StringBuffer stringBuffer = new StringBuffer();
for (int j = 0; j <= checkedItems.length - 1; j++) {
if (checkedItems[j]) {
stringBuffer.append(items[j]).append(" ");
}
}
if (stringBuffer != null) {
Toast.makeText(MainActivity.this, " " + stringBuffer,
Toast.LENGTH_SHORT).show();
}
dialogInterface.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
3.2.5 自定义对话框
- 在Android程序中由于界面风格的不同,一般不直接使用系统提供的对话框,而是根据项目需求定义相应的对话框样式。
<?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:gravity="center">
<Button
android:id="@+id/btn_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="弹出自定义对话框"/>
</LinearLayout>
custom_dialog
<?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:paddingTop="16dp"
android:orientation="vertical">
<TextView android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="visible"
android:textColor="#333333"
android:textSize="18sp"
android:layout_marginBottom="16dp"/>
<TextView android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:textSize="14sp"
android:textColor="#999999" />
<View android:layout_width="match_parent"
android:layout_height="2px"
android:layout_marginTop="16dp"
android:background="#E8E8E8" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:id="@+id/negtive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_weight="1"
android:background="@null"
android:gravity="center"
android:singleLine="true"
android:textColor="#999999"
android:textSize="16sp" />
<View android:id="@+id/column_line"
android:layout_width="2px"
android:layout_height="match_parent"
android:background="#E8E8E8" />
<Button
android:id="@+id/positive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:background="@null"
android:gravity="center"
android:textColor="#38ADFF"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
package com.ywp.customdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_dialog).setOnClickListener(this);
}
@Override
public void onClick(View view) {
final CommonDialog commonDialog = new CommonDialog(MainActivity.this);
commonDialog.setTitle("提示")
.setMessage("您确定要删除信息?")
.setNegtive("取消")
.setPositive("确定")
.setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
@Override
public void onPositiveClick() {
commonDialog.dismiss();
}
@Override
public void onNegtiveClick() {
commonDialog.dismiss();
}
})
.show();
}
}
CommonDialog.java
package com.ywp.customdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CommonDialog extends AlertDialog {
private TextView titleTv ; //显示的标题
private TextView messageTv ; //显示的消息
private Button negtiveBn ,positiveBn; //确认和取消按钮
public CommonDialog(Context context) {
super(context);
}
private String message;
private String title;
private String positive,negtive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog);
initView(); //初始化界面控件
initEvent(); //初始化界面控件的点击事件
}
//初始化界面控件
private void initView() {
negtiveBn = (Button) findViewById(R.id.negtive);
positiveBn = (Button) findViewById(R.id.positive);
titleTv = (TextView) findViewById(R.id.title);
messageTv = (TextView) findViewById(R.id.message);
}
//初始化界面控件的显示数据
private void refreshView() {
//如果自定义了title和message会 显示自定义的信息,否则不显示title和message的信息
if (!TextUtils.isEmpty(title)) {
titleTv.setText(title); //设置标题控件的文本为自定义的title
titleTv.setVisibility(View.VISIBLE); //标题控件设置为显示状态
}else {
titleTv.setVisibility(View.GONE); //标题控件设置为隐藏状态
}
if (!TextUtils.isEmpty(message)) {
messageTv.setText(message); //设置消息控件的文本为自定义的message信息
}
//如果自定义了按钮的文本,则按钮显示自定义的文本,否则,按钮显示“确定”或“取消”文本
if (!TextUtils.isEmpty(positive)) {
positiveBn.setText(positive); //设置按钮的文本为自定义的文本信息
}else {
positiveBn.setText("确定"); //设置按钮文本为“确定”
}
if (!TextUtils.isEmpty(negtive)) {
negtiveBn.setText(negtive);
}else {
negtiveBn.setText("取消");
}
}
//初始化界面的确定和取消监听器
private void initEvent() {
//设置确定按钮的点击事件的监听器
positiveBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onClickBottomListener!= null) {
onClickBottomListener.onPositiveClick();
}
}
});
//设置取消按钮的点击事件的监听器
negtiveBn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ( onClickBottomListener!= null) {
onClickBottomListener.onNegtiveClick();
}
}
});
}
@Override
public void show() {
super.show();
refreshView();
}
public interface OnClickBottomListener{
void onPositiveClick();//实现确定按钮点击事件的方法
void onNegtiveClick(); //实现取消按钮点击事件的方法
}
//设置确定取消按钮的回调
public OnClickBottomListener onClickBottomListener;
public CommonDialog setOnClickBottomListener(OnClickBottomListener
onClickBottomListener){
this.onClickBottomListener = onClickBottomListener;
return this;
}
public CommonDialog setMessage(String message) {
this.message = message;
return this ;
}
public CommonDialog setTitle(String title) {
this.title = title;
return this ;
}
public CommonDialog setPositive(String positive) {
this.positive = positive;
return this ;
}
public CommonDialog setNegtive(String negtive) {
this.negtive = negtive;
return this ;
}
}