Android Studio基础对话框AlertDialog最基本的使用

Android Studio基础对话框AlertDialog最基本的使用

1、对按钮添加一个点击事件,需要配合Activity.java进行实现

第一步:在该布局XML中对按钮增加ID值,进行唯一性参照物

 

<?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">

    <Button
        android:id="@+id/btn_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示对话框"
        />

</LinearLayout>

第二步:到LayoutActivity.java配置,首先绑定布局xml文件

1)通过接口方式进行实现

在public class LayoutActivity extends AppCompatActivity后面导入 implements View.OnClickListener

2)实现该接口View.OnClickListener自动生成此接口抽象方法,代码内容如下

3)使用多态性btn_alert.setOnClickListener(this);

4)编写该接口的抽象方法

package com.xwb.btn1;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class LayoutActivity extends AppCompatActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);//设置的布局界面
        Button btn_alert = findViewById(R.id.btn_alert);//实现变量值与布局进行关联
        btn_alert.setOnClickListener(this);//多态性,是指View.OnClickListener
    }

    @Override
    public void onClick(View view) {
        //1、创造一个builder的构造器
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //2、设置标题、设置图片、设置消息内容、设置setPositiveButton确定按钮(当点击OK后,new是要去做啥事情)
        builder.setTitle("对话框").setIcon(R.mipmap.ic_launcher).setMessage("今晚一起喝酒?").setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //3、设置需要弹出对话框信息
                Toast.makeText(LayoutActivity.this,"好的,你请客就行。",Toast.LENGTH_SHORT).show();
            }
        })
         //4、设置取消按钮,new为不同意后做的事情
        .setNegativeButton("不好", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //5、设置需要弹出对话框信息
                Toast.makeText(LayoutActivity.this,"不行,今晚要回家",Toast.LENGTH_SHORT).show();
            }
        });
        //6、AlertDialog方法创建对话框,并通过.show()方法显示对话框
        AlertDialog ad = builder.create();
        ad.show();
    }
}

第三步:运行APP效果

此类对话框场景是做“删除”操作可以使用。

参考:

https://blog.csdn.net/weixin_33735077/article/details/85784360

https://blog.csdn.net/lpCrazyBoy/article/details/80538257

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Android Studio使用 AlertDialog 类可以很容易地创建对话框。下面是一个简单的示例,演示如何在 Android Studio 中编写对话框代码: 1. 首先,在 XML 布局文件中定义一个按钮,用于触发对话框的显示: ```xml <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Dialog" /> ``` 2. 在 Java 代码中,添加以下代码: ```java Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("Dialog Title"); builder.setMessage("Dialog Message"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 确定按钮被点击后执行的操作 } }); builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消按钮被点击后执行的操作 } }); AlertDialog dialog = builder.create(); dialog.show(); } }); ``` 这段代码添加了一个点击监听器,当按钮被点击时,创建一个 AlertDialog.Builder 对象,并设置标题、消息以及确定和取消按钮的回调函数。然后创建一个 AlertDialog 对象并显示出来。 这就是如何在 Android Studio 中编写对话框代码的基本方法。您可以根据需要自定义对话框的外观和行为。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐为波

看着给就好了,学习写作有点累!

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

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

打赏作者

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

抵扣说明:

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

余额充值