android tost封装,Android对Toast简单封装之ToastMaker

今天再来记录一个工具类,Toast,每个项目都会用到的这个东东,短暂出现的通知,它们只显示几秒钟就会消失。Toast 不会获取焦点,并且是非模态的,所以它们不会打断当前活动的应用程序,很友好的东东,现在来对它进行一个简单的封装喽~

先放个图,好喜欢今朝和祈,相依为命的生活。O(∩_∩)O哈哈哈~

a1bb5fffcde9

截图取自网络

我这里对它里面的ToastMaker稍作点修改,当我们点击按钮触发Toast时,若前一个Toast提示还没消失,又触发了新的Toast提示了,我这边是直接修改提示文字的,感觉用户体验会更好,不然连续点击N次触发Toast,那提示得持续多久哇!

/**

* 对app中所有的toast进行管理

* Created by pengyn on 2016/1/5.

*/

public class ToastMaker {

private static Toast sToast;

private static TextView sContentTv;

public static void showShortToast(String msg) {

showCustomToast(BaseApplication.getInstance(), msg, Toast.LENGTH_SHORT);

}

public static void showShortToast(int msgId) {

showCustomToast(BaseApplication.getInstance(), msgId, Toast.LENGTH_SHORT);

}

public static void showLongToast(String msg) {

showCustomToast(BaseApplication.getInstance(), msg, Toast.LENGTH_LONG);

}

public static void showLongToast(int msgId) {

showCustomToast(BaseApplication.getInstance(), msgId, Toast.LENGTH_LONG);

}

/**

* 创建运行在UI线程中的Toast

*

* @param activity

* @param msg

*/

public static void showToastInUiThread(final Activity activity, final String msg) {

if (activity != null) {

activity.runOnUiThread(new Runnable() {

public void run() {

showCustomToast(activity, msg);

}

});

}

}

public static void showToastInUiThread(final Activity activity, final int stringId) {

if (activity != null) {

activity.runOnUiThread(new Runnable() {

public void run() {

showCustomToast(activity, stringId);

}

});

}

}

private static void showCustomToast(Context context, int msgId) {

final String msg = context.getResources().getString(msgId);

showCustomToast(context, msg);

}

private static void showCustomToast(Context context, String msg) {

showCustomToast(context, msg, Toast.LENGTH_SHORT);

}

private static void showCustomToast(Context context, int msgId, int duration) {

final String msg = context.getResources().getString(msgId);

showCustomToast(context, msg, duration);

}

private static void showCustomToast(final Context context, final String msg, final int duration) {

if (context == null) {

return;

}

if (Looper.myLooper() == Looper.getMainLooper()) {

showToast(context, msg, duration);

} else {

new Handler(Looper.getMainLooper()).post(new Runnable() {

@Override

public void run() {

showToast(context, msg, duration);

}

});

}

}

private static void showToast(Context context, String msg, int duration) {

if (null != context) {

if (sToast == null) {

sToast = new Toast(context);

LayoutInflater inflater = LayoutInflater.from(context);

View layout = inflater.inflate(R.layout.toast, null);

sContentTv = (TextView) layout.findViewById(R.id.tv_toast_content);

sContentTv.setText(msg);

sToast.setGravity(Gravity.CENTER, 0, BaseApplication.getInstance().screenH / 4);

sToast.setDuration(duration);

sToast.setView(layout);

}else {

sContentTv.setText(msg);

}

sToast.show();

}

}

}

代码比较容易看懂,然后有需要什么带图片的Toast什么的,就自己定义根据需求修改啦,我反正感觉这样子用挺好用的。

2017.1.20 附上一个不错常用的 Toast 封装

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值