亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

全部開發者教程

Android 入門教程

菜單類控件
菜單:Menu
并發編程
多線程
首頁 慕課教程 Android 入門教程 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 布局讓它完全按自己的樣式去展示。