Toast的工具类和自定义工具类

Toast在实际Android开发中使用率是非常高的。默认的写法就不在此处赘述了。

下面是Toast的简单使用工具类

public class ToastUtils {
    private ToastUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static boolean isShow = true;

    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, CharSequence message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }



    /**
     * 短时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showShort(Context context, int message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, CharSequence message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /**
     * 长时间显示Toast
     *
     * @param context
     * @param message
     */
    public static void showLong(Context context, int message) {
        if (isShow)
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, CharSequence message, int duration) {
        if (isShow)
            Toast.makeText(context, message, duration).show();
    }

    /**
     * 自定义显示Toast时间
     *
     * @param context
     * @param message
     * @param duration
     */
    public static void show(Context context, int message, int duration) {
        if (isShow)
            Toast.makeText(context, message, duration).show();
    }
}


下面是Toast的自定义工具类,首先自定义他的方法接口,包括Toast的位置、Toast的图片类型自定义

public interface ToastFunctions {

	/**
	 * 直接显示Toast
	 *
	 * @param context
	 *            上下文
	 * @param text
	 *            文本信息
	 * @param duration
	 *            显示时长
	 */
	public void immediateToast(Context context, String text, int duration);

	/**
	 * 自定义位置的Toast
	 *
	 * @param context
	 *            上下文
	 * @param text
	 *            文本信息
	 * @param duration
	 *            显示时长
	 * @param gravity
	 *            位置
	 */
	public void ToastLocation(Context context, String text, int duration,
							  int gravity);

	/**
	 * 带图片的Toast
	 *
	 * @param context
	 *            上下文
	 * @param text
	 *            文本信息
	 * @param duration
	 *            显示时长
	 * @param resId
	 *            图片资源id
	 */
	public void ToastWithPic(Context context, String text, int duration,
							 int resId);

	/**
	 * 自定义的Toast
	 *
	 * @param context
	 *            上下文
	 * @param text
	 *            文本信息
	 * @param duration
	 *            显示时长
	 * @param view
	 *            布局文件xml文件的view对象 <br><br>
	 *
	 *            View的创建如下: <br>
	 *            //自定义Toast主要是利用LayoutInflater来实现<br>
	 *            LayoutInflater layoutInflater = getLayoutInflater(); <br>
	 *            //加载一个布局文件,自定义的布局文件 View<br>
	 *            view = layoutInflater.inflate(layoutId,null);<br>
	 */
	public void ToastBySelf(Context context, String text, int duration,
							View view);
}

public class ToastUtil implements ToastFunctions {
	private Toast toast = null;

	@Override
	public void immediateToast(Context context, String text, int duration) {
		toast = Toast.makeText(context, text, duration);
		toast.show();
	}

	@Override
	public void ToastLocation(Context context, String text, int duration,
							  int gravity) {
		Toast toast = Toast.makeText(context, text, duration);
		toast.setGravity(gravity, 0, 0);
		toast.show();
	}

	@Override
	public void ToastWithPic(Context context, String text, int duration,
							 int resId) {
		toast = Toast.makeText(context, text, duration);
		// 通过Toast创建一个LinearLayout
		LinearLayout layout = (LinearLayout) toast.getView();
		// 创建一个ImageView对象,并添加到LinearLayout中
		ImageView imageView = new ImageView(context);
		imageView.setImageResource(resId);
		// 添加
		layout.addView(imageView);
		// 显示Toast
		toast.show();
	}

	@Override
	public void ToastBySelf(Context context, String text, int duration, View view) {
		// Toast
		toast = new Toast(context);
		// 设置Toast显示时长
		toast.setDuration(duration);
		// 设置Toast的View对象
		toast.setView(view);
		// 显示Toast
		toast.show();
	}

}

另外,其他线程通过Handler的调用

//调用方法1 
    //Thread th=new Thread(this); 
    //th.start(); 
    //调用方法2 
    handler.post(new Runnable() { 
        @Override 
        public void run() { 
            showToast(); 
        } 
    }); 
 
Java代码
public void showToast(){ 
        Toast toast=Toast.makeText(getApplicationContext(), "Toast在其他线程中调用显示", Toast.LENGTH_SHORT); 
        toast.show(); 
    } 
 
Java代码
Handler handler=new Handler(){ 
        @Override 
        public void handleMessage(Message msg) { 
            int what=msg.what; 
            switch (what) { 
            case 1: 
                showToast(); 
                break; 
            default: 
                break; 
            } 
             
            super.handleMessage(msg); 
        } 
    }; 
 
Java代码
@Override 
    public void run() { 
        handler.sendEmptyMessage(1); 
    } 
复制代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值