9、Android --AlertDialog基础学习

说明

AlertDialog优点想类似web的对话框/弹框。
设置主要是体现在java中。
需要通构建Dialog的各种参数 AlertDialog.Builder builder = new AlertDialog, Builder(context);
打开分类三类
1、普通提示框 – 类似web中的alert/confirm
2、列表提示框
3、单选提示框
4、多选提示框

普通提示框(通用属性)

  • builder.setlcon(int iconld); 添加图标Icon
  • builder.setTitle(CharSequence title); 添加标题
  • builder.setMessage(CharSequence message);添加提示框主体消息
  • builder.setCancelable(boolean cancelable); 点击提示框以外区域,提示框是否消失,默认true
  • builder.setPositiveButton(CharSequence text, OnClickListener listener); 确定按钮的描述(右)、以及监听事件
  • builder.setNegatfveButton(CharSequence text, OnClickListener listener); 确定按钮的描述(左)、以及监听事件
  • builder.setNeutraluttn(CharSequence text, OnClickListener listener); 确定按钮的描述(其他位置)、以及监听事件
  • builder.create();创建Dialog
  • builder.show);显示对话框 create必须先于show
  • alertDialog.setOnShowListener(OnShowListener listener); 监听对话框弹出。 位于create 之后,show之前(alertDialog=builder.create() ;alertDialog.create() ;)
  • alertDialog.setOnCancelListener(OnCancelListener listener); 监听对话框关闭。位于create 之后,show之前(alertDialog=builder.create() ;alertDialog.create() ;)

代码见示例中的com.pha.first/AlertDialogActivity.java 中的NormalAlertDialog方法

列表提示框

其他属性同普通提示框

  • setItems(CharSequence[] items, OnClickListener listener) ;对提示框列表赋值,以及监听点击事件
    代码见示例中的com.pha.first/AlertDialogActivity.java 中的ListAlertDialog方法

单选提示框

其他属性同普通提示框

  • setSingleChoiceItems(CharSequence[] items, int checkedItem, OnClickListener listener) ;对提示框单选列表赋值,以及监听点击事件
    代码见示例中的com.pha.first/AlertDialogActivity.java 中的ListAlertDialog方法

多选提示框

其他属性同普通提示框

  • setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, OnMultiChoiceClickListener listener) ;对提示框多选列表赋值,以及监听点击事件
    代码见示例中的com.pha.first/AlertDialogActivity.java 中的CheckAlertDialog方法

示例

1、res/layout/activity_alert_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".AlertDialogActivity">
    <Button
        android:id="@+id/btn_ad_one"
        android:text="Normal--AlertDialog"
        android:background="@color/yellow"
        android:layout_width="200dp"
        android:layout_height="100px"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/btn_ad_two"
        android:text="List--AlertDialog"
        android:background="@color/yellow"
        android:layout_width="200dp"
        android:layout_height="100px"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/btn_ad_three"
        android:text="single--select--AlertDialog"
        android:background="@color/yellow"
        android:layout_width="200dp"
        android:layout_height="100px"
        android:layout_margin="10dp"
        />
    <Button
        android:id="@+id/btn_ad_four"
        android:text="chck--select--AlertDialog"
        android:background="@color/yellow"
        android:layout_width="200dp"
        android:layout_height="100px"
        android:layout_margin="10dp"
        />
</LinearLayout>

2、com.pha.first/AlertDialogActivity.java

package com.pha.first;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.ArrayList;
import java.util.List;

public class AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "AlertDialog";
    private int checkItm = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        findViewById(R.id.btn_ad_one).setOnClickListener(this);
        findViewById(R.id.btn_ad_two).setOnClickListener(this);
        findViewById(R.id.btn_ad_three).setOnClickListener(this);
        findViewById(R.id.btn_ad_four).setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_ad_one:
                NormalAlertDialog();
                break;
            case R.id.btn_ad_two:
                ListAlertDialog();
                break;
            case R.id.btn_ad_three:
                SingleAlertDialog();
                break;
            case R.id.btn_ad_four:
                CheckAlertDialog();
                break;
            default:
                break;
        }
    }
    // 多选alertDialog
    private void CheckAlertDialog(){
        List<String> chkItms =new ArrayList<>();
        String like[] = {"战士","刺客","法师","辅助","射手"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_baseline_5g_24).setTitle("ListAlertDialog:你喜欢的角色:");
        builder.setMultiChoiceItems(like, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                if (b){
                    Log.e(TAG, "onClick(选择): Check - AlertDialog - setNegativeButton"+like[i]);
                    chkItms.add(like[i]);
                }else{
                    Log.e(TAG, "onClick(不选择): Check - AlertDialog - setNegativeButton"+like[i]);
                    chkItms.remove(like[i]);
                }
            }
        });
        //其他同普通提示框
        builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick(取消): Check - AlertDialog - setNegativeButton");
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick(确定): + Check - AlertDialog - setPositiveButton" );
                        dialogInterface.dismiss();
                    }
                })
                .create().show();

    }
    private void SingleAlertDialog() {

        String sex[] = {"男","女","保密"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_baseline_5g_24).setTitle("ListAlertDialog:你的性别是:");
        builder.setSingleChoiceItems(sex, checkItm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                checkItm = i;
                Log.e(TAG, "onClick(取消): Single - AlertDialog - setNegativeButton:"+sex[i]);
                dialogInterface.dismiss();
           }
        });
        //其他同普通提示框
        builder.create().show();
    }

    private void ListAlertDialog() {
        String level[] = {"青铜","铂金","黄金","星耀","王者"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_baseline_5g_24).setTitle("ListAlertDialog:你当前Level:");
        builder.setItems(level, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Log.e(TAG, "ListAlertDialog: "+level[i] );
                dialogInterface.dismiss();
            }
        });
        //其他同普通提示框
        builder.create().show();

    }

    // 普通提示框
    public void NormalAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_baseline_5g_24)
                .setTitle("信息提醒:")
                .setMessage("请问您是否需要保存界面数据?")
                .setCancelable(false)
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick(取消): Normal - AlertDialog - setNegativeButton");
                        dialogInterface.dismiss();
                    }
                })
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick(确定): + Normal - AlertDialog - setPositiveButton" );
                        dialogInterface.dismiss();
                    }
                })
                .setNeutralButton("帮助", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.e(TAG, "onClick(中间): + Normal - AlertDialog - setNeutralButton" );
                        dialogInterface.dismiss();
                    }
                });
        AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Log.e(TAG, "显示AlertDialog监听: + Normal - AlertDialog - setOnShowListener" );
            }
        });
        alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                Log.e(TAG, "消失AlertDialog监听: + Normal - AlertDialog - setOnShowListener" );
            }
        }) ;
        alertDialog.show();
    }
}

效果图

1、界面
在这里插入图片描述
2、普通提示框
普通提示框
3、列表对话框
列表对话框
4、单选提示框
单选提示框
5、多选提示框
多选提示框
6、输出记录日志
在这里插入图片描述

记忆知识点

提示框的各种属性,在此不单独重复了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值