編輯:關於 Android 程式設計
Toast 以一個小彈窗的形式向使用者傳達簡單的操作回饋。它只佔用顯示訊息所需的最小空間,同時目前的 Activity 依然可見、可互動。舉例來說,當你在還沒寄出郵件前就離開,系統會跳出「草稿已保存」的 Toast,提醒你之後可以繼續編輯。Toast 會在一段時間後自動消失。

Toast 的常用成員如下:

各成員的用途:
| 成員 | 說明 |
|---|---|
| Toast(Context) | 建構子 |
| show() | 顯示 Toast |
| cancel() | 取消 Toast |
| setMargin(float, float) | 設定水平與垂直方向的 margin |
| setGravity(int, int, int) | 設定位置重心與相對的 XOffset、YOffset |
| makeText(Context, CharSequence, int) | 以字串建立 Toast |
| makeText(Context, int, int) | 以字串資源建立 Toast |
| setView(View) | 設定自訂 Toast 的 View |
| setDuration(int) | 設定顯示時間 |
| setText | 設定訊息文字 |
註:部分 getter 與 setter 成對出現,此處未全部列出。
<LinearLayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin">
<Button android:background="@color/colorAccent" android:id="@+id/bt_simple" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Simple Toast"></Button>
<Button android:background="@color/colorPrimary" android:id="@+id/bt_custom" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_width="match_parent" android:text="Custom Toast"></Button>
</LinearLayout> <LinearLayout android:background="@drawable/shape_bg" android:layout_height="40dp" android:layout_width="match_parent" android:orientation="horizontal">
<ImageView android:layout_height="40dp" android:layout_width="40dp" android:src="@drawable/ic_account_circle_black_24dp">
<TextView android:gravity="center" android:layout_height="40dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:text="This is a Custom Toast !">
</LinearLayout> package mraz.com.toastdemo;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Context mContext;
Button btSimple, btCustom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
btSimple = (Button) findViewById(R.id.bt_simple);
btCustom = (Button) findViewById(R.id.bt_custom);
btSimple.setOnClickListener(this);
btCustom.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_simple:
Toast.makeText(mContext, " This is a simple toast", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_custom:
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View custToast = layoutInflater.inflate(R.layout.custom_toast, null);
Toast toast = new Toast(mContext);
toast.setView(custToast);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
break;
}
}
}
Toast 的內容看似簡單,卻是開發過程中經常派上用場的小工具。偶爾會遇到它遲遲不顯示的狀況,最常見的原因是忘了呼叫 show() 方法;當然,也可能有其他因素導致無法顯示,值得逐一排查。
OpenGL ES 繪製圓柱體:光照與紋理
OpenGL ES 繪製圓柱體:光照與紋理 本文以 OpenGL ES 繪製圓柱體為例,說明圓面、側面、法向量與紋理坐標的處理方式。圓柱體看似簡單,但涉及幾何頂點、三角形
Android 自訂 SeekBar 同步控制硬體音量並與音量鍵互動
播放器裡的滑桿音量控制,要做到「拖滑桿改音量、按音量鍵滑桿也跟著動」的雙向同步,需要同時用到 AudioManager、ContentObserver 與音量變化的系統廣
在 App 內自訂鈴聲:挑選、播放與預設勾選
有個需求是在自己的 App 裡加入鈴聲並播放,但網上查到的資料大多在教怎麼改系統來電或鬧鐘鈴聲,跟我要做的事不太一樣,於是就自己整理成這篇記錄。 一、本文涵蓋的四件事 1
Android 學習筆記(三):Intent 意圖完整解析
前面的文章我們介紹了 Activity,不過相信不少朋友還覺得意猶未盡。所以這次我們來認識 Android 的「信使」——Intent,它能夠在不同元件之間傳遞訊息、彼此