Android AlertDialog使用

简介: 本示例功能为点击按钮,弹出对话框,自定义AlertDialog的布局View,获取输入框EditText中的内容,点击确定按钮以及空白处不关闭对话框。创建布局文件,设置按钮的点击事件。

本示例功能为点击按钮,弹出对话框,自定义AlertDialog的布局View,获取输入框EditText中的内容,点击确定按钮以及空白处不关闭对话框。

  1. 创建布局文件,设置按钮的点击事件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mazaiting.alertdialogtest.MainActivity"
    >

  <Button
      android:onClick="open"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="显示对话框"
      />
</RelativeLayout>

主页面Activity的初始配置代码

public class MainActivity extends AppCompatActivity {

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  /**
   * 按钮打开事件
   */
  public void open(View view){

  }
}
  1. 接下来的代码都写在open(View view)这个函数中。首先创建出AlertDialog的布局文件,如下:
<?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"
    android:layout_margin="50dp"
    android:padding="15dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_tip"
        android:gravity="center"
        android:text="请输入文字"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:textSize="16sp"
        android:layout_marginTop="20dp"
        android:id="@+id/et_number"
        android:hint="请输入文字"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
  1. 现在open(View view)方法中,加载出布局。
    // 加载布局
    View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input_id,null);
    EditText et_number = (EditText) dialogView.findViewById(R.id.et_number);
  1. 创建AlertDialog, 并实现其相应设置
  /**
   * 按钮打开事件
   */
  public void open(View view){
    // 加载布局
    View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_input_id,null);
    final EditText editText = (EditText) dialogView.findViewById(R.id.et_number);

    new AlertDialog.Builder(this) // 使用android.support.v7.app.AlertDialog
        .setView(dialogView) // 设置布局
        .setCancelable(false) // 设置点击空白处不关闭
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            String text = editText.getText().toString();
            if(TextUtils.isEmpty(text)){ // 判断输入的内容是否为空
              setDialogIsShowing(dialog, false); // 设置不关闭
              Toast.makeText(MainActivity.this, "内容不能为空", Toast.LENGTH_SHORT)
                  .show();
            }else{
              Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT)
                  .show();
            }
          }
        }) // 设置确定按钮,并设置监听事件})
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            setDialogIsShowing(dialog, true); // 设置关闭
          }
        }) // 设置取消按钮,并设置监听事件
        .create() // 创建对话框
        .show(); // 显示对话框
  }
  1. 设置是否关闭对话框,函数内容:
  /**
   * 设置对话框是否显示
   * @param dialog 对话框
   * @param isClose 是否显示. true为关闭,false为不关闭
   */
  private void setDialogIsShowing(DialogInterface dialog, boolean isClose) {
    try {
      // 获取到android.app.Dialog类
      Field mShowing = dialog.getClass().getSuperclass().getSuperclass().getDeclaredField("mShowing");
      mShowing.setAccessible(true); // 设置可访问
      mShowing.set(dialog,isClose); // 设置是否关闭
    } catch (NoSuchFieldException | IllegalAccessException e) {
      e.printStackTrace();
    }
  }
  1. 点击空白处不关闭对话框,应添加
    .setCancelable(false) // 设置点击空白处不关闭
    其中false为不关闭,true为关闭。
目录
相关文章
|
8月前
|
Android开发
Android 中AlertDialog警告对话框的使用
Android 中AlertDialog警告对话框的使用
72 0
|
9月前
|
XML Java Android开发
Android 对话框组件 AlertDialog 四种常用方法
Android 对话框组件 AlertDialog 四种常用方法
101 0
|
XML 存储 前端开发
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
225 0
Android MVVM框架搭建(七)Permission、AlertDialog、拍照和相册选取
|
XML Android开发 数据格式
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色
741 0
Android AlertDialog修改标题、内容、按钮的字体大小和字体颜色
|
Android开发
Android弹窗二则: PopupWindow和AlertDialog
前言 弹窗是图形界面必备的一个模块, 回忆一下windows那些恶心爆了的错误弹窗吧, 把弹窗制作的更高效友好一点是非常必要的. 这里说两个常用的弹窗类, PopupWindow和AlertDialog.
1121 0
|
Android开发 Windows 数据格式