android 定位util,Android 简单的ToastUtil(位置,布局,时长)

复制以下文件可直接使用:

public class ToastUtil {

/**

* show short toast

*

* @param msg

*/

public static void showShortToast(String msg) {

Toast.makeText(MyApplication.getInstance(), msg, Toast.LENGTH_SHORT).show();

}

/**

* show long toast

*

* @param msg

*/

public static void showLongToast(String msg) {

Toast.makeText(MyApplication.getInstance(), msg, Toast.LENGTH_LONG).show();

}

/**

* 居中显示 short toast

*

* @param msg

*/

public static void showCenterShortToast(String msg) {

Toast toast = Toast.makeText(MyApplication.getInstance(), msg, Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

}

/**

* 居中显示 long toast

*

* @param msg

*/

public static void showCenterLongToast(String msg) {

Toast toast = Toast.makeText(MyApplication.getInstance(), msg, Toast.LENGTH_LONG);

//Gravity.CENTER Gravity.LEFT Gravity.RIGHT Gravity.TOP

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

}

/**

* 自定义toast布局,并居中显示

*

* @param msg

*/

public static void showCustomShortToast(String msg) {

View view = LayoutInflater.from(MyApplication.getInstance()).inflate(R.layout.toast_layout, null);

TextView textView = view.findViewById(R.id.id_content);

textView.setText(msg);

Toast toast = new Toast(MyApplication.getInstance());

toast.setGravity(Gravity.CENTER, 0, 0);

toast.setView(view);

toast.show();

}

/**

* 自定义toast 显示的时长

*

* @param msg

*/

public static void showCustomTimeShortToast(String msg, int time) {

final Toast toast = Toast.makeText(MyApplication.getInstance(), msg, Toast.LENGTH_LONG);

final Timer timer = new Timer();

timer.schedule(new TimerTask() {

@Override

public void run() {

toast.show();

}

}, 0, 3500);//0秒后执行,LENGTH_LONG时间为3.5s,所以设置3500毫秒后再次执行

new Timer().schedule(new TimerTask() {

@Override

public void run() {

toast.cancel();

timer.cancel();

}

}, time);//time时间后执行取消taost,取消timer.

}

}

ToastUtil文件中使用到的MyApplication文件如下:

public class MyApplication extends Application {

public static MyApplication instance;

public static MyApplication getInstance() {

return instance;

}

@Override

public void onCreate() {

super.onCreate();

instance = this;

}

}

自定义Toast布局文件如下:

效果如下:

(录屏工具推荐:GifCam 下载地址:http://www.bahraniapps.com/apps/gifcam/gifcam.php)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值