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

三步驟打造自訂 Toast:BToast 完整教學(附範例)

編輯:關於 Android 程式設計

這裡介紹一套名為 BToast 的自訂提示元件。它的設計目標有兩個:使用方式要夠簡單,以及外觀要能自訂。BToast 內建兩種樣式——純文字,以及帶圖示(提供「√」與「×」兩種)。

它還有一個與原生 Toast 明顯不同的行為:系統 Toast 採用佇列機制,前一個消失後下一個才會出現;BToast 則會直接把目前的提示頂掉,永遠只顯示最新那一則。對使用者來說,反饋會即時很多。

步驟一、準備版面資源

除了版面檔,還需要三個素材:背景、√ 圖示、× 圖示。背景可以直接用 XML 畫出來,不必準備圖片。

toast_back.xml

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="https://schemas.android.com/apk/res/android" >
 <corners android:radius="12dp" />
 <solid android:color="#CC000000"/>
 </shape>

√ 與 × 建議使用圖片素材,這裡不再贅述。接著完成提示本體的版面:

toast_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/toast_back"
 android:gravity="center_vertical"
 android:padding="13dp"
 android:orientation="vertical" >
 <ImageView
 android:id="@+id/toast_img"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:background="@drawable/toast_y"
 android:layout_gravity="center_horizontal"
 android:layout_marginBottom="5dp" />
 <TextView
 android:id="@+id/toast_text"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginLeft="10dp"
 android:layout_marginRight="10dp"
 android:textColor="#FFFFFF"
 android:gravity="center"
 android:textSize="17sp" />
 </LinearLayout>

步驟二、Java 實作

先建立 BToast 類別,繼承 Toast 並以靜態成員保存唯一實例:

 public class BToast extends Toast {
 /** Toast 單例 */
 private static BToast toast;

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

為了做到「新提示頂掉舊提示」,需要覆寫幾個方法,並把例外吞掉以免造成閃退:

 /** 隱藏目前的 Toast */
 public static void cancelToast() {
 if (toast != null) {
 toast.cancel();
 }
 }

 public void cancel() {
 try {
 super.cancel();
 } catch (Exception e) { }
 }

 @Override
 public void show() {
 try {
 super.show();
 } catch (Exception e) { }
 }

接著把自訂版面灌進 Toast:

 private static void initToast(Context context, CharSequence text) {
 try {
 cancelToast();
 toast = new BToast(context);
 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.toast_layout, null);
 toast_img = (ImageView) layout.findViewById(R.id.toast_img);
 TextView toast_text = (TextView) layout.findViewById(R.id.toast_text);
 toast_text.setText(text);
 toast.setView(layout);
 toast.setGravity(Gravity.CENTER, 0, 70);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }

核心的顯示邏輯如下,這裡用三個常數描述圖示狀態,並在顯示圖示時加上 Y 軸旋轉動畫:

 /** 不顯示圖示 */
 private static final int TYPE_HIDE = -1;
 /** 顯示 √ */
 private static final int TYPE_TRUE = 0;
 /** 顯示 × */
 private static final int TYPE_FALSE = 1;

 private static void showToast(Context context, CharSequence text, int time, int imgType) {
 initToast(context, text);

 if (time == Toast.LENGTH_LONG) {
 toast.setDuration(Toast.LENGTH_LONG);
 } else {
 toast.setDuration(Toast.LENGTH_SHORT);
 }

 if (imgType == TYPE_HIDE) {
 toast_img.setVisibility(View.GONE);
 } else {
 if (imgType == TYPE_TRUE) {
 toast_img.setBackgroundResource(R.drawable.toast_y);
 } else {
 toast_img.setBackgroundResource(R.drawable.toast_n);
 }
 toast_img.setVisibility(View.VISIBLE);
 ObjectAnimator.ofFloat(toast_img, "rotationY", 0, 360).setDuration(1700).start();
 }

 toast.show();
 }

你可能會注意到這個方法是 private。若直接改成 public 當然也能用,但呼叫端仍得記住一長串參數,跟原生 Toast 比並沒有省事多少。

步驟三、讓介面更貼心

既然「自訂」的意思就是規則由我們自己定,那不如把介面收斂成幾個好讀的重載方法:

 /** 純文字提示 */
 public static void showText(Context context, CharSequence text) {
 showToast(context, text, Toast.LENGTH_SHORT, TYPE_HIDE);
 }

 /** 帶圖示的提示,isSucceed 決定顯示對號或叉號 */
 public static void showText(Context context, CharSequence text, boolean isSucceed) {
 showToast(context, text, Toast.LENGTH_SHORT, isSucceed ? TYPE_TRUE : TYPE_FALSE);
 }

 /** 純文字提示,可指定持續時間 */
 public static void showText(Context context, CharSequence text, int time) {
 showToast(context, text, time, TYPE_HIDE);
 }

 /** 帶圖示的提示,可指定持續時間 */
 public static void showText(Context context, CharSequence text, int time, boolean isSucceed) {
 showToast(context, text, time, isSucceed ? TYPE_TRUE : TYPE_FALSE);
 }

呼叫端因此變得非常直覺:

 public void click(View view) {
 switch (view.getId()) {
 case R.id.btn_text:
 BToast.showText(this, "簡單提示");
 break;
 case R.id.btn_text_true:
 BToast.showText(this, "簡單提示 正確圖標", true);
 break;
 case R.id.btn_text_false:
 BToast.showText(this, "簡單提示 錯誤圖標", false);
 break;
 case R.id.btn_text_long:
 BToast.showText(this, "簡單提示 長~", Toast.LENGTH_LONG);
 break;
 case R.id.btn_text_true_long:
 BToast.showText(this, "簡單提示 正確圖標 長~", Toast.LENGTH_LONG, true);
 break;
 case R.id.btn_text_false_long:
 BToast.showText(this, "簡單提示 錯誤圖標 長~", Toast.LENGTH_LONG, false);
 break;
 }
 }

比原生 Toast 好用不少吧?成果如下:

BamboyToast 效果圖

原始碼:BamboyToast_jb51.rar

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