Android APP开发08(AlertDialog)

AlertDialog

实现方式

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

方法

方法名解析
Builder.setIcon(int iconId)添加ICON
Builder.setTitle(CharSequence title)添加标题
Builder.setMessage(CharSequence msg)添加消息
Builder.setView(View view)设置自定义布局
Builder.create()创建Dialog
Builder.show()显示对话框
setPositiveButton确定按钮
setNegativeButton取消按钮
setNeutralButton中间按钮

实例

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" xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="显示对话框"
            android:onClick="showalert"
    />
</LinearLayout>

java

package com.example.myalertdialog;

import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void showalert(View view){
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.ic_launcher)
            .setTitle("我是对话框")
            .setMessage("CSDN博客:碰磕")
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Log.e("leo","点击了确定");
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Log.e("leo","点击了取消");
                }
            })
            .setNeutralButton("中间", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Log.e("leo","点击了中间");
                }
            })
            .create().show();
    }
}

效果

在这里插入图片描述
通过点击不同的选择,会做出相对于的操作在这里插入图片描述

还可以自定义布局
只需要创建一个xml
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:background="#ffff00"
        android:orientation="horizontal">
    <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
               android:src="@mipmap/ic_launcher"
    />
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
              android:text="碰磕"
    />
</LinearLayout>

在java中加入

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

最终效果:
在这里插入图片描述
dialog弹窗测试就完成了…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

碰磕

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

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

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

打赏作者

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

抵扣说明:

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

余额充值