Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Toast工具類:一鍵搞定各種提示彈窗

編輯:關於 Android 程式設計

簡述

在日常開發中,Toast是最常用的輕量提示手段。為了避免到處重複編寫Toast.makeText的樣板代碼,我把常見的各種形式封裝成了一個工具類,在此分享給大家,希望能帶來一些啟發。

工具類實現

這個工具類集合了短提示、長提示、自定義位置、帶圖片以及完全自定義布局等多種形式,並通過一個開關控制是否顯示:

public class TinyjoyToastUtil extends Toast {
    private static boolean isShowFlag = true; // 全局顯示開關
    private static Toast toast;

    public TinyjoyToastUtil(Context context) { super(context); }

    // 短顯示,消息為字符串
    public static void showShort(Context context, CharSequence message) {
        if (isShowFlag) Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
    // 短顯示,消息為資源ID
    public static void showShort(Context context, int message) {
        if (isShowFlag) Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
    }
    // 長顯示,消息為字符串
    public static void showLong(Context context, CharSequence message) {
        if (isShowFlag) Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }
    // 長顯示,消息為資源ID
    public static void showLong(Context context, int message) {
        if (isShowFlag) Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }

    // 自定義顯示位置(短):gravity指定位置,xOffset/yOffset為相對偏移量
    public static void showCustomLocationShort(Context context, int messageId, int gravity, int xOffset, int yOffset) {
        if (isShowFlag) {
            Toast toast = Toast.makeText(context, messageId, Toast.LENGTH_SHORT);
            toast.setGravity(gravity, xOffset, yOffset);
            toast.show();
        }
    }
    // 自定義顯示位置(長)及其他重載方法省略,思路相同

    // 帶圖片的短提示,顯示在屏幕正中
    public static void showCustomToastWithImageShort(Context context, CharSequence message, int resId) {
        if (isShowFlag) {
            toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            LinearLayout toastContentView = (LinearLayout) toast.getView();
            ImageView img = new ImageView(context);
            img.setImageResource(resId);
            toastContentView.addView(img);
            toast.show();
        }
    }

    // 完全自定義的短提示:使用自定義XML布局
    public static void showCompletedCustomToastShort(Context context, int layoutResource, CharSequence message, Bitmap bitmap) {
        if (isShowFlag) {
            toast = new Toast(context);
            LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflate.inflate(layoutResource, null);
            TextView mTextView = (TextView) view.findViewById(R.id.toast_text_content);
            ImageView mImageView = (ImageView) view.findViewById(R.id.toast_image_content);
            mTextView.setText(message);
            mImageView.setImageBitmap(bitmap);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setView(view);
            toast.setDuration(TinyjoyToastUtil.LENGTH_SHORT);
            toast.show();
        }
    }
    // 其餘帶圖片ID、長顯示等重載方法思路一致,此處從略
}

小結

這個工具類的設計要點有三:一是用static方法統一封裝,調用時一行搞定;二是通過isShowFlag開關便於全局控制提示的開關,比如在測試環境關閉所有提示;三是支持從簡單文本到完全自定義布局的多層次擴展,滿足從基礎到複雜的各種需求。實際項目中,你還可以進一步擴充單例模式來避免內存洩露、加入隊列來防止提示被頻繁刷新覆蓋,讓它變得更健壯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved