toast java_Toast工具类

/**

* toast工具类 防止重复弹出toast

*/

public class ToastUtils {

private Context context;

private Toast toast = null;

public ToastUtils(Context context) {

this.context = context;

}

public void showToast(int resID) {

showToast(context, Toast.LENGTH_SHORT, resID);

}

public void showToast(String text) {

showToast(context, Toast.LENGTH_SHORT, text);

}

public void showToast(Context ctx, int resID) {

showToast(ctx, Toast.LENGTH_SHORT, resID);

}

public void showToast(Context ctx, String text) {

showToast(ctx, Toast.LENGTH_SHORT, text);

}

public void showLongToast(Context ctx, int resID) {

showToast(ctx, Toast.LENGTH_LONG, resID);

}

public void showLongToast(int resID) {

showToast(context, Toast.LENGTH_LONG, resID);

}

public void showLongToast(Context ctx, String text) {

showToast(ctx, Toast.LENGTH_LONG, text);

}

public void showLongToast(String text) {

showToast(context, Toast.LENGTH_LONG, text);

}

public void showToast(Context ctx, int duration, int resID) {

showToast(ctx, duration, ctx.getString(resID));

}

/** Toast一个图片 */

public Toast showToastImage(Context ctx, int resID) {

toast = Toast.makeText(ctx, "", Toast.LENGTH_SHORT);

View mNextView = toast.getView();

if (mNextView != null)

mNextView.setBackgroundResource(resID);

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

toast.show();

return toast;

}

public void showToast(final Context ctx, final int duration,

final String text) {

if (toast == null) {

toast = Toast.makeText(ctx, text, duration);

if(MyApp.isPad(context)) {

//LinearLayout layout = (LinearLayout) toast.getView();

//layout.setBackgroundColor(Color.parseColor("#FFFFFF"));

TextView v = (TextView) toast.getView().findViewById(android.R.id.message);

//v.setTextColor(Color.BLACK);

v.setTextSize(30);

}

} else {

toast.setText(text);

}

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

toast.show();

}

/** 在UI线程运行弹出 */

public void showToastOnUiThread(final Activity ctx, final String text) {

if (ctx != null) {

ctx.runOnUiThread(new Runnable() {

public void run() {

showToast(ctx, text);

}

});

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值