八、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页面

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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.自定义布局页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.后台代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 @   搬砖工具人  阅读(49)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示