Android 基础开发组件AlertDialog

.setIcon

设置图标

builder.setIcon(R.drawable.ic_android_black_24dp)

#.setTitle
设置标题

.setTitle("警告")

#.setMessage
设置内容

.setMessage("输入错误")

#.ssetPositiveButton
确定按钮

.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialogInterface, int i) {
    Log.e("june", "onClick: 点击了确定");
  }
})

#.setNegativeButton
取消按钮

.setNegativeButton("取消", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialogInterface, int i) {
    Log.e("june", "onClick: 点击了取消");
  }
})

#.ssetNeutralButton
中间按钮

.setNeutralButton("中间", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialogInterface, int i) {
    Log.e("june", "onClick: 点击了中间");
  }
})

#.setView
设置自定义布局

.setView(dialogView)

#.create()
创建Dialog

.create()

#.show()
显示对话框

.show();

dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".Dialog">

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
      android:text="显示对话框"
      android:onClick="buttonClick"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/button"/>
  </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

dialog_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <LinearLayout
    android:orientation="horizontal"
    android:background="#1A000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:srcCompat="@drawable/user" android:id="@+id/imageView"/>
    <TextView
      android:text="用户至上"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:textSize="75sp"/>
  </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Dialog.java

package com.weijun901.app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class Dialog extends AppCompatActivity {

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

  public void buttonClick(View view) {

    //拿到dialog_view布局文件
    View dialogView = getLayoutInflater().inflate(R.layout.dialog_view, null);

    //builder构造器创建AlterDialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setIcon(R.drawable.ic_android_black_24dp)
          .setTitle("警告")
          .setMessage("输入错误")
          .setView(dialogView)
          .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
              Log.e("june", "onClick: 点击了确定");
            }
          })
          .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
              Log.e("june", "onClick: 点击了取消");
            }
          })
          .setNeutralButton("中间", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
              Log.e("june", "onClick: 点击了中间");
            }
          })
          .create()
          .show();
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值