Android 自定义带图标Toast,工具方法

简易Toast工具方法

 

private void displayToastTip(final String tip) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(SfzShiBieActivity.this, tip, Toast.LENGTH_SHORT).show();
        }
    });
}
 

private void toast(final String text) {
    handler.post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(RegActivity.this, text, Toast.LENGTH_LONG).show();
        }
    });
}

private Handler handler = new Handler(Looper.getMainLooper());
 

 

背景资源

1、#999999

2、




    
    

    
    
    
    
    
    

3、layout.xml布局




    
    
        
        

        
    

4、工具类

/**
 * 作者:created by meixi
 * 邮箱:[email protected]
 * 日期:2018/8/28 08
 */

public class ToastUtils {

    private static Context mContext ;

    public static void showToast(String toast,Context context) {
        mContext = context;
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }

    /**
     *  带图片的吐司提示
     * @param text
     */
    public static void showCustomImgToast(String text ,Context context) {
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setBackgroundResource(R.mipmap.em_dx_checkbox_on);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    /**
     *  带图片的吐司提示
     *  通过参数传递,可是设置吐司的图片和文字内容
     * @param text
     */
    public static void showCustomImgToast(String text,int imgResId) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setBackgroundResource(R.mipmap.em_dx_checkbox_on);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    /**
     *  不带图片的吐司提示
     * @param text
     */
    public static void showCustomToast(String text) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setVisibility(View.GONE);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

    /**
     * 带图片的吐司,设置吐司弹出的位置为屏幕中心
     * @param text
     */
    public static void showCustomToastCenter(String text ,Context context) {
        showCustomToastCenter(text, R.mipmap.em_dx_checkbox_on,context);
    }

    /**
     * 带图片的吐司,设置吐司弹出的位置为屏幕中心
     * 通过参数传递,可是设置吐司的图片和文字内容
     * @param text
     */
    public static void showCustomToastCenter(String text, int imgResId,Context context) {
        mContext = context;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.toast_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.toast_image);
        imageView.setBackgroundResource(imgResId);
        TextView t = (TextView) view.findViewById(R.id.toast_text);
        t.setText(text);
        Toast toast = null;
        if (toast != null) {
            toast.cancel();
        }
        toast = new Toast(mContext);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.setGravity(Gravity.BOTTOM, 0, 50);
        toast.show();
    }
}

 

你可能感兴趣的:(移动开发)