1. 程式人生 > Android入門教學 >吐司提示:Toast 的使用方法

吐司提示:Toast 的使用方法

在使用 Android 手機的時候,有沒有遇到過如圖中這種型別的訊息提示?

Toast in Android

這個在 Android 中被稱為 Toast,用來短暫的展示一些簡短的提示資訊。相比彈窗來講它對使用者的打擾更小,在提示一段時間之後會自動消失,通常用來提示當前的狀態或者一些不太重要的資訊。接下來我們先看看 Toast 的相關特性然後一起動手編寫一些與 Toasts 相關的示例程式碼。

1. Toast 的特性

其實大家在使用 Android 手機的時候大致能夠了解 Toast 的特徵,這裡簡單歸納一下:

  • 用來展示簡短訊息提示的控制元件
  • 會在短暫展示之後自動消失
  • Toast 不會阻塞 Activiity 或者 Fragment 的執行
  • 比較適合用來給使用者的某個操作做一個反饋

2. Toast 的使用方法

Toast 直接繼承自 Object,相當於是一個比較原始的控制元件,封裝的也比較友好,我們先來看看 Toast 可用的 API 及相關引數

2.1 Toast 常用 API

  • public static Toast makeText(Context context, CharSequence text, int duration):
    用於建立一個 Toast 並設定具體的提示文案及展示時長
  • public void show():
    顧名思義,觸發 Toast 的展示
  • public void setMargin(float horizontal, float vertical):

    用於設定 Toast 垂直方向和水平方向上的間距
  • public void setView(View view):
    自定義展示的佈局樣式

2.2 Toast 中的常量引數

  • public static final int LENGTH_LONG:
    長時間展示,大約維持 3.5 秒
  • public static final int LENGTH_SHORT:
    短時間展示,大約維持 2 秒

2.3 如何建立

  1. 呼叫 Toast 的靜態方法makeText:傳入Context、字串文案、顯示時長即可建立一個 Toast。為了避免記憶體洩露,這裡的 Context 建議使用getApplicationContext()
    來獲取 App 的 Context 傳入
    ,如下:
Toast toast = Toast.makeText(getApplicationContext(),"IMOOC Android Study",Toast.LENGTH_LONG);
  1. 呼叫 Toast 的show()方法
toast.show();

我們可以將以上兩句串聯起來,如下:

Toast.makeText(getApplicationContext(),"IMOOC Android Study",Toast.LENGTH_LONG).show();

3. 設定 Toast 在螢幕中的位置

按照第 2 小節的方式 show 出來的 Toast 預設會在螢幕的底部中間位置展示,如果想要不走尋常路,可以通過介面setGravity(int gravity, int x, int y)改變 Toast 展示的位置,引數如下:

  • int gravity:
    第一個引數表示重心,和 Layout 裡面的 gravity 類似,我們可以直接使用 Gravity.java 類裡面的常量來設定。有以下四種可選項:
    • Gravity.BOTTOM
    • Gravity.RIGHT
    • Gravity.LEFT
    • Gravity.TOP

當然我們也可以同時設定兩個值,中間用“|”隔開,如Gravity.TOP|Gravity.LEFT表示左上方向。

  • int x:
    設定水平方向上的距離,這個距離的參照是左邊還是右邊依賴第一個引數gravity
  • int y:
    設定垂直方向上的距離,這個距離的參照是頂部還是底部依賴於第一個引數gravity的設定

如果設定 gravity 為 Gravity.CENTER,x 為 100,y 為 200。那麼最後 show 出來的示意圖如下:

Toast 自定義位置

4. Toast 的使用示例

本節我們用 Toast 實現一個“農藥”提示,有兩個 Button,點選會觸發一個短暫的訊息提示。程式碼比較簡單,首先在佈局中加入兩個 Button:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/dont_wave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="猥瑣發育,別浪" />

    <Button
        android:id="@+id/hold_on_we_can_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="穩住,我們能贏" />
</LinearLayout>

接著在 Java 程式碼中註冊監聽器,在點選不同 Button 的時候彈出不同的 Toast 提示資訊:


package com.emercy.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.dont_wave).setOnClickListener(this);
        findViewById(R.id.hold_on_we_can_win).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        showToast(((TextView) v).getText().toString());
    }

    private void showToast(String text) {
        LinearLayout layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.toast, null);
        TextView textView = layout.findViewById(R.id.text);
        textView.setText(text);
        Toast toast = new Toast(this);
        toast.setView(layout);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 500);

        toast.show();
    }
}

可以看到在建立 Toast 之後,通過setView方法設定了一個 LinearLayout 型別的 View 物件。通過這種方式就可以自定義一個展示樣式,最後編寫 Toast 的佈局樣式程式碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="bottom"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/mc" />

    <TextView
        android:id="@+id/text"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" />
</LinearLayout>

XML 檔案中我們橫向放置了一個圖片和一個文字,對應的是遊戲人物和提示語,然後在點選的時候彈出此樣式的 Toast。最終編譯執行效果如下:

Toast 示例

5. 小結

本節我們學習了一種簡單的訊息提示方式,它適合於一些簡短且不是太重要的訊息,大多數情況下我們可以直接用靜態方法makeText來建立一個 Toast,但是在一些特定場景下我們也可以通過 API 設定它的邊界等樣式,或者直接自定義一個 xml 佈局讓它完全按自己的樣式去展示。