八、AlertDialog

AlertDialog.Builder builder = new AlertDialog.Builder(context);  //构建Dialog的各种参数

Builder.setlcon(int iconld);  //添加ICON

Builder.setTitle(CharSequence title);  //添加标题

Builder.setMessage(CharSequence message);  //添加消息

Builder.setView(View view);  //设置自定义布局

Builder.creat();  //创建

DialogBuilder.show();  //显示对话框

setPositiveButton   //确定按钮

setNegativeButton //取消按钮

setNeutralButton   //中间按钮 

源码设置

1.ui页面

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:text="显示对话框"
        android:textSize="30sp"
        android:onClick="BtnClink"
        android:layout_width="200dp"
        android:layout_height="150dp"/>

</LinearLayout>

 

  2.自定义布局页面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#ff00ff00"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:text="测试,天气很好"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

  3.后台代码

package com.example.alertdialog;

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 BtnClink(View view) {
        //加载布局并转换成View
        View view1 = getLayoutInflater().inflate(R.layout.dialog_view,null);

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher) //设置图标
                .setTitle("标题")  //设置标题
                .setMessage("文本提示信息")  //提示内容
                .setView(view1)   //设置自定义布局
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("leo", "onClick:点击了确认按钮 ");
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("leo", "onClick:点击了取消按钮 ");
                    }
                })
                .setNeutralButton("中间", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e("leo", "onClick:点击了中间按钮 ");
                    }
                })
                .create()   //创建
                .show();   //展示
    }
}

  4.效果图

 

posted @ 2022-03-15 11:21  搬砖工具人  阅读(46)  评论(0编辑  收藏  举报