ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android APP开发08(AlertDialog)

2022-01-30 13:30:14  阅读:157  来源: 互联网

标签:Builder DialogInterface AlertDialog void new import Android APP


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弹窗测试就完成了…

标签:Builder,DialogInterface,AlertDialog,void,new,import,Android,APP
来源: https://blog.csdn.net/m_xiaozhilei/article/details/122751636

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有