控件AlertDialog

这篇博客详细介绍了如何在Android中创建和使用AlertDialog,包括设置ICON、标题、消息、自定义布局以及添加不同操作按钮。通过示例代码展示了如何利用Builder进行链式调用来构建和显示对话框,并监听按钮点击事件。

常用属性

AlertDialog.Builder builder = new AlertDialog.Builder(context); 构建Dialog的各种参数
builder.setIcon(int iconld); 添加ICON
builder.setTitle(CharSequence title); 添加标题
builder.setMessage(CharSequence message); 添加消息
builder.setView(View view); 设置自定义布局
builder.create(); 创建Dialog
builder.show(); 显示对话框
setPositiveButton 确定按钮
setNegativeButton 取消按钮
setNeutralButton 中间按钮

注意:
除了create和show的返回值都是Builder,可以使用链式结构
create返回值是AlertDialog
show是Dialog.class中的
所以show在create后面,两个是放到最后面的

案例

代码

MainActivity.java

package com.example.alertdialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Finny";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void adClick(View view) {

        View dialogview = getLayoutInflater().inflate(R.layout.dialog_view,null);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher)
                .setTitle("对话框")
                .setMessage("对话框内容")
                .setView(dialogview)
                .setPositiveButton("确定按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick: 点击了确定按钮" );
                    }
                })
                .setNegativeButton("取消按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick: 点击了取消按钮" );
                    }
                })
                .setNeutralButton("中间按钮", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick: 点击了中间按钮" );
                    }
                })
                .create()
                .show();
    }
}

activity_main.xml

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

    <Button
        android:text="显示对话框"
        android:onClick="adClick"
        android:background="#00f7a9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

dialog_view.xml

<?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="#FF00FF00">

    <ImageView
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="这是一个Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只小阿大:)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值