Android的 AlertDialog自定义布局与常用布局用法(弹窗)

文章展示了如何在Android应用中创建和使用AlertDialog,包括标准对话框和自定义布局的对话框。通过示例代码,解释了如何设置对话框的内容、按钮以及响应按钮点击事件的方法。
摘要由CSDN通过智能技术生成

1.直接上效果图,看看是不是你们想要的效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.主活动MainActivity2的代码如下


import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;


public class MainActivity2 extends AppCompatActivity {

    private Button button = null;
    private Button button2 = null;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                dialogShow1();
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                dialogShow2();
            }
        });

    }

    private void dialogShow1() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
        builder.setTitle("温馨提示");
        builder.setIcon(R.drawable.ic_launcher_background);
        builder.setMessage("原理是基本");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(MainActivity2.this, "no", Toast.LENGTH_LONG).show();
            }
        });
        builder.setPositiveButton("立即更新",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity2.this, "ok", Toast.LENGTH_LONG).show();
                    }
                });
        Dialog dialog = builder.create();
        dialog.show();
    }

    /**
     * 自定义布局
     * setView()只会覆盖AlertDialog的Title与Button之间的那部分,而setContentView()则会覆盖全部,
     * setContentView()必须放在show()的后面
     */
    private void dialogShow2() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity2.this);
        LayoutInflater inflater = LayoutInflater.from(MainActivity2.this);
        View v = inflater.inflate(R.layout.update_manage_dialog, null);
        TextView content = (TextView) v.findViewById(R.id.dialog_content);
        Button btn_sure = (Button) v.findViewById(R.id.dialog_btn_sure);
        Button btn_cancel = (Button) v.findViewById(R.id.dialog_btn_cancel);
        //builer.setView(v);//这里如果使用builer.setView(v),自定义布局只会覆盖title和button之间的那部分
        final Dialog dialog = builder.create();
        dialog.show();
        dialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面
        //dialog.getWindow().setGravity(Gravity.CENTER);//可以设置显示的位置
        content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity2.this, "用户点击的是:"+content.getText(), Toast.LENGTH_LONG).show();

            }
        });
        btn_sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Toast.makeText(MainActivity2.this, "ok", Toast.LENGTH_LONG).show();
            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
                Toast.makeText(MainActivity2.this, "no", Toast.LENGTH_LONG).show();
            }
        });
    }


}

3.activity_main2的布局文件内容如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myapplication001.MainActivity2">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#03A9F4"
            android:text="弹出dialog"
            android:textColor="#F3EEEE" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="#03A9F4"
            android:text="弹出自定义布局dialog"
            android:textColor="#F4F2F2" />
    </LinearLayout>

</RelativeLayout>

4.自定义弹出窗的布局:update_manage_dialog布局文件内容如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FFFFFF" >

    <RelativeLayout
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_centerInParent="true"
        android:background="@drawable/update_bg" >

        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:text="温馨提示"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/dialog_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/dialog_title"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:text="原理是基本\n实践出真知"
            android:textSize="14sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/dialog_btn_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:text="取消"
                android:textColor="#AAAAAA"
                android:textSize="14sp" />

            <Button
                android:id="@+id/dialog_btn_sure"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:text="立即更新"
                android:textSize="14sp" />
        </LinearLayout>
    </RelativeLayout>

</RelativeLayout>

5.update_bg放在drawable里面,代码如下

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- android:radius 弧形的半径 -->
    <corners android:radius="30dp" />

    <!-- 填充的颜色 -->
    <solid android:color="@android:color/white" />

</shape>

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义 AlertDialog布局,你可以按照以下步骤进行操作: 1. 首先,创建一个 XML 布局文件来定义你想要在 AlertDialog 中显示的内容。例如,创建一个名为 `custom_dialog.xml` 的文件。 2. 在这个布局文件中,你可以使用任何你需要的视图和样式来构建对话框的外观。例如,可以添加文本框、按钮、图像等。确保布局中的根视图是一个容器,比如 LinearLayout 或 RelativeLayout。 3. 在代码中创建一个 AlertDialog.Builder 对象,并通过调用 `setView()` 方法来设置自定义布局。例如: ```java AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.custom_dialog, null); builder.setView(dialogView); ``` 4. 可选:如果需要在对话框中添加按钮并处理点击事件,你可以使用 `setPositiveButton()`、`setNegativeButton()` 或 `setNeutralButton()` 方法来添加按钮,并传入相应的点击事件监听器。 5. 最后,调用 `create()` 方法创建 AlertDialog 对象,并通过 `show()` 方法将其显示出来。完整的示例代码如下所示: ```java AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.custom_dialog, null); builder.setView(dialogView) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 处理确定按钮点击事件 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // 处理取消按钮点击事件 } }); AlertDialog dialog = builder.create(); dialog.show(); ``` 通过上述步骤,你可以自定义 AlertDialog布局并添加相应的按钮和事件处理逻辑。希望对你有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值