• 中级实训Android学习记录——Toast、AlertDialog、ProgressBar


    学习记录 2020/11/22

    Toast

    • Toast
      • Toast是一个消息提示组件
      • 我们可以设置其显示的位置
      • 自定义其显示的内容
      • 对Toast的简单封装可以达到不同的目的
    • Toast的默认用法
    Toast.makeText(getApplicationContext(), "Toast", Toast.LENGTH_LONG).show()
    

    其中makeText相当于Toast类中的一个构造函数,他会根据你输入的参数来构造一个新的Toast返回给你。

    第一个参数是一个context,可以选择直接调用getApplicationContext函数来输入,也可以输入当前的Activity的名字来输入,如:ToastActivity.this

    第二个参数是一个String,用来表示Toast的输出内容

    第三个参数是Toast的显示时间,LENGTH_LONG表示显示2s

    • Toast改变位置用法
    Toast toastCenter = Toast.makeText(getApplicationContext(), "居中Toast", Toast.LENGTH_LONG);
    toastCenter.setGravity(Gravity.CENTER, 0, 0);
    toastCenter.show();
    

    调用makeText生成一个Toast,并调用setGravity将其位置设置为居中即可

    • Toast的自定义显示内容的用法,如增加一个图片
    // 首先需要一个Toast自定义显示的布局layout_toast.xml
    // 自定义为一个LinearLayout,里面包括了一个ImageView(id=iv_toast)和一个TextView(id=tv_toast)
    Toast toastCustom = new Toast(getApplicationContext());
    LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
    View view = inflater.inflate(R.layout.layout_toast, null);
    ImageView imageView = (ImageView) view.findViewById(R.id.iv_toast);
    TextView textView = (textView) view.findViewById(R.id.tv_toast);
    imageVIew.setImageResource(R.drawable.icon_simle); // 用drawable/icon_simle.png图片作为layout_toast布局文件中的imageView的图片
    textView.setText("自定义Toast");
    toastCustom.setView(view);
    toastCustom.show();
    

    需要使用一个布局文件时,通过LayoutInflater来构造一个基于现在的activity的inflater,通过inflater来帮我们找到我们需要的布局文件。

    LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);
    View view = inflater.inflate(R.layout.layout_toast, null);
    

    最后通过view以及方法findViewById来找到当前的布局文件中的构成组件

    ImageView imageView = (ImageView) view.findViewById(R.id.iv_toast);
    TextView textView = (textView) view.findViewById(R.id.tv_toast);
    

    然后通过组件各自的方法来实现我们需要显示的内容

    imageVIew.setImageResource(R.drawable.icon_simle); // 用drawable/icon_simle.png图片作为layout_toast布局文件中的imageView的图片
    textView.setText("自定义Toast");
    

    最后把我们需要显示的自定义toast通过setView方法自定义我们的toast

    toastCustom.setView(view);
    
    • 多次点击显示Toast的按钮,Toast会排队,等待上一个Toast显示完之后再开始显示,不符合需求,所以我们需要对Toast进行简单封装
    public class ToastUtil {
        public static Toast mToast;
        public static void showMsg(Context context, String msg) {
            if (mToast == null) {
                mToast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
            }
            else {
                mToast.setText(msg);
            }
            mToast.show();
        }
    }
    

    运用以上简单的封装,我们可以达到多次点击生成Toast的按钮,只会按照最后一次点击来显示Toast的目的。

    而这个封装与之前的按钮的区别是:之前的按钮每按一次都会创造一个新的Toast,而这次只会在第一次创造新的Toast,他调用的是同一个Toast的show方法。

    AlertDialog

    • AlertDialog
      • 默认样式
      • 单选样式
      • 多选样式
      • 自定义
    • 默认用法
    AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
    builder.setTitle("请回答").setMessage("你觉得课程如何")
        .setIcon(R.drawable.icon_smile)
        .setPositiveButton("棒", new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, "你很诚实");
            }
        }).setNeutralButton("还行", new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, "你再瞅瞅");
            }
        }).setPositiveButton("不好", new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, "瞎说");
            }
        }).show();
    

    通过AlertDialog的生产者模式Builder来生成AlertDialog

    由于builder的大部分方法返回的都是builder本身,所以我们可以通过一系列的.来调用形成调用串

    记得最后要调用show来显示AlertDialog

    • 单选样式的AlertDialog
    //没有radioButton的单选AlertDialog
    AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
    String[] array = new String[]{"男", "女"};
    builder1.setTitle("选择性别").setItems(array, new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, array[which]);
            }
        }).show();
    
    //有radioButton的单选AlertDialog
    AlertDialog.Builder builder2 = new AlertDialog.Builder(DialogActivity.this);
    String[] array = new String[]{"男", "女"};
    builder2.setTitle("选择性别").setSingleChoiceItems(array, 0, new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, array[which]);
            }
        }).show();
    

    有radioButton的单选AlertDialog中setSingleChoiceItems中

    第一个参数是一个String的数组,表示显示的列表

    第二个参数是选定的数组的下表,在例子中,0则表示选中男,1则表示选中女

    第三个是一个ClickListener

    此时我们可以通过点击非AlertDialog的区域来取消AlertDialog的显示,如果我们希望用户一定要选择一个按钮才能取消显示,我们需要

    • 让builder产生的AlertDialog不支持自己取消:setCancelable(false)

    • 在onClick函数中调用dismiss方法

    new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, array[which]);
                dialog.dismiss();
            }
        }
    
    • 多选样式的AlertDialog
    AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
    String[] array = new String[]{"唱歌", "跳舞", "写代码"};
    boolean[] isSelected = new Boolean[]{false, falst, true};
    builder3.setTitle("选择兴趣").setMultipleChoiceItems(array, isSelected, new DialogInterface.OnClickListener() {
            @override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.showMsg(DialogActivity.this, array[which]+":"+isSelected[which]);
            }
        }).show();
    

    多选样式即从setSingleChoiceItems变成setMultipleChoiceItems,

    第二个参数从选定的数组下标(int)改成是否选定的列表(boolean[])

    • 自定义样式的AlertDialog
    // 自定义一个布局layout_dialog.xml
    // 例子中包含一个EditText(id=et_username)表示输入用户名,一个EditText(id=et_password)表示输入密码,一个登陆按钮(id=btn_login)
    AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
    LayoutInflater inflater = LayoutInflater.fron(DialogActivity.this);
    View view = inflater.inflate(R.layout.layout_dialog);
    EditText eUserName = (EditText) view.findViewById(R.id.et_username);
    EditText ePassWord = (EditText) view.findViewById(R.id.et_password);
    Button btnLogin = (Button) view.findViewById(R.id.btn_login);
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @override
        public void onClick(View v) {
            //
        }
    })
    builder4.setTitle("请登录").setView(view).show();
    

    其中我们直接用setView设置自定义样式

    ProgressBar & ProgressDialog

    • ProgressBar
      • 默认样式
    • 默认

    可以直接在xml声明,可以自定义style,

    style="???.Horizontal"时为水平的ProgressBar,

    可以声明progress="10"说明此时的进度条进度

    声明secondaryProgress说明进度条的二级进度(视频缓存之类的)

    声明max说明进度条最大进度是多少

    // 可以在Activity中直接更改进度条的进度
    mpb = (ProgressBar) findViewById(R.id.pb);
    mBtnStart = (Button) findViewById(R.id.btn_start);
    mBtnStart.setOnClickListener(new View.onClickListener {
        @override
        public void onClick() {
            handler.sendEmptyMessage(0);
        }
    })
    mpb.setProgress(30);
    
    Handler handler = new Handler() {
        @override
        public void handleMessage(Message msg) {
    		super.handleMessage(msg);
            if (mpb.getProgress < 100) {
                handler.postDelayed(runnable, 500); //延迟500ms发送一个信息给runnable
            } else {
                ToastUtil.showMsg(ProgressActivity.this, "加载完成");
            }
        }
    }
    
    Runnable runnable = new Runnable() {
    	@override
        public void run() {
    		mpb.setProgress(mpb.getProgress() + 5);
            handler.sendEmptyMessage(0);
        }
    }
    

    此处,我们声明一个handler接受消息,一个runnable发送消息,这个过程其实是通过点击按钮,我们模拟一个ProgressBar加载的过程,每过500ms发送一次信息给runnable让他给progress加5进度直到进度为100.

    • 自定义ProgressBar
    // 通过自定义一个xml文件来生成旋转动画,具体表现为<animated-rotate/>
    // 然后使用ProgressBar时直接更改其style为自定义的动画即可
    
    • ProgressDialog

    使用方法基本与AlertDialog相同

  • 相关阅读:
    Kivy Minimum required opengl version (2.0) not found
    Rust 在Jebrains 家族工具里报 No Cargo projects found!
    Rust Cargo 下载慢的解决办法
    Python面试题库学习1-5
    Kivy Pyinstaller打包报: No module named pkg_resources.py2_warn
    Kettle 部署和远程调用(spoon 8.3)
    spring boot 发布的jar 二 发布到 windows
    spring boot 发布的jar 一 发布到 linux centos7
    springBoot项目打jar包后,修改配置文件中的配置项
    mysql sqldump 备份
  • 原文地址:https://www.cnblogs.com/lwfing/p/14053960.html
Copyright © 2020-2023  润新知