AlertDialog.Builder builder = new AlertDialog.Builder(this) :构建Dialog的各种参数
builder.setTitle(“对话框标题”) :添加对话框标题
builder.setMessage(“内容”) :添加对话框内容
builder.setView(inflate) :设置自定义布局
builder.setPositiveButton(“确定”,new DialogInterface.OnClickListener() :确定按钮
buildersetNegativeButton(“取消”, new DialogInterface.OnClickListener() :取消按钮
builder.setNeutralButton(“中间”, new DialogInterface.OnClickListener() :中间按钮
builder.create() :创建Dialog
builder.show(); :显示对话框
前端示例activity_main.xml
<?xml version=”1.0” encoding=”utf-8”?>
android:layout_height=”match_parent”
xmlns:app=”http://schemas.android.com/apk/res-auto“
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android">
<Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="设置"<br /> android:onClick="liu"<br /> /><br /></LinearLayout>
前端示例alert_dia_log_main.xml
<?xml version=”1.0” encoding=”utf-8”?>
android:layout_height=”match_parent”
xmlns:app=”http://schemas.android.com/apk/res-auto“
android:orientation=”vertical”
android:background=”@color/purple_200”
xmlns:android=”http://schemas.android.com/apk/res/android">
<ImageView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:src="@mipmap/ic_launcher"<br /> /><br /> <TextView<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="背景"
/><br /></LinearLayout>
后端示例
package com.example.myAlertDialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout._activity_main_);<br /> }
public void liu(View view) {<br /> View inflate = getLayoutInflater().inflate(R.layout._alert_dia_log_main_, null);<br /> AlertDialog.Builder builder = new AlertDialog.Builder(this);<br /> builder.setTitle("对话框标题")<br /> .setMessage("内容")<br /> .setView(inflate)<br /> .setPositiveButton("确定",new DialogInterface.OnClickListener(){<br /> @Override<br /> public void onClick(DialogInterface dialogInterface, int i) {<br /> Log._e_("beleth","点击了 确定 Onclick");<br /> }<br /> }).setNegativeButton("取消", new DialogInterface.OnClickListener() {<br /> @Override<br /> public void onClick(DialogInterface dialogInterface, int i) {<br /> Log._e_("beleth","点击了 取消 Onclick");<br /> }<br /> }).setNeutralButton("中间", new DialogInterface.OnClickListener() {<br /> @Override<br /> public void onClick(DialogInterface dialogInterface, int i) {<br /> Log._e_("beleth","点击了 中间 Onclick");<br /> }<br /> }).create().show();<br /> }<br />}