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

学习记录 2020/11/22

Toast

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

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

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

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

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

  • 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来找到当前的布局文件中的构成组件post

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的目的。ui

而这个封装与以前的按钮的区别是:以前的按钮每按一次都会创造一个新的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相同

相关文章
相关标签/搜索