.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();
}
}