实现方式
-
AlertDialog.Builder builder = new AlertDialog.Builder(context);构建Dialog的各种参数
-
Builder.setlcon(int iconld);添加ICON
-
Builder.setText(CharSrqrnce title);添加标题
-
Builder.setMessage(CharSequence message);添加消息
-
Builder.setView(View view);设置自定义布局
-
Builder.create();创建Dialog
-
Builder.show();显示对话框
-
setPositiveButton确定按钮
-
setNegativeButton取消按钮
-
setNeutralButton中间按钮
<?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="horizontal"
android:background="#FF00FF">
<ImageView
android:background="@drawable/icon_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="你好呀"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.example.project02;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick01(View view) {
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) {
Log.e("wu","点击了确定");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wu","点击了取消");
}
})
.setNeutralButton("中间", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wu","点击了中间");
}
})
.setView(R.layout.activity_alert_dialog)
.create()
.show();
}
}