?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
Toast
翻譯時間:2010年11月2日
版本:Android 2.2 r1
?
類結(jié)構(gòu)
???????? public class Toast extends Object
?
java.lang.Object
android.widget.Toast
?
概述
????????
Toast是一種提供給用戶簡潔信息的視圖。Toast類幫助你創(chuàng)建和顯示該信息。
???????? 該視圖已浮于應(yīng)用程序之上的形式呈現(xiàn)給用戶。因為它并不獲得焦點,即使用戶正在輸入什么也不會受到影響。它的目標(biāo)是盡可能已不顯眼的方式,使用戶看到你提供的信息。有兩個例子就是音量控制和設(shè)置信息保存成功。
使用該類最簡單的方法就是調(diào)用一個靜態(tài)方法,讓他來構(gòu)造你需要的一切并返回一個新的 Toast 對象。
?
常量
???????? int ? LENGTH_LONG ?????
持續(xù)顯示視圖或文本提示較長時間。該時間長度可定制。
???????? 參見
?????????????????? setDuration(int)
?
int ? LENGTH_SHORT
持續(xù)顯示視圖或文本提示較短時間。該時間長度可定制。該值為默認(rèn)值。
???????? 參見
?????????????????? setDuration(int)
?
構(gòu)造函數(shù)
???????? public Toast (Context context)
???????? 構(gòu)造一個空的 Toast 對象。在調(diào)用 show() 之前,必須先調(diào)用 setView(View)。
(譯者注:只有使用setView(View)的時候,才使用new Toast(Content content)來得到Toast對象,否則必須用makeText()方法來創(chuàng)建toast對象,并且這種方式獲得Toast對象不能使用setText()方法。)
???????? 參數(shù)
?????????????????? context??? 使用的上下文。通常是你的 Application 或 Activity 對象。
?
公共方法
public int cancel ()
???????? 如果視圖已經(jīng)顯示則將其關(guān)閉,還沒有顯示則不再顯示。一般不需要調(diào)用該方法。正常情況下,視圖會在超過存續(xù)期間后消失。
?
public int getDuration ()
返回存續(xù)期間
?????? 請參閱
????????????? setDuration(int)
public int getGravity ()
???????? 取得提示信息在屏幕上顯示的位置。
?????????????????? 請參閱
??????????????????????????? Gravity
?????????? setGravity()
?
public float getHorizontalMargin ()
返回橫向欄外空白。
?
public float getVerticalMargin ()
返回縱向欄外空白。
?
public View getView ()
返回 View 對象。
請參閱
setView(View)
?
public int getXOffset ()
返回相對于參照位置的橫向偏移像素量。
?????????????? Toast msg = Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG);
?????????????? msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
?????????????? msg.show();
?
?
public int getYOffset ()
返回相對于參照位置的縱向偏移像素量。
?
public static Toast makeText (Context context, int resId, int duration)
生成一個從資源中取得的包含文本視圖的標(biāo)準(zhǔn) Toast 對象。
參數(shù)
??? context??
使用的上下文。通常是你的 Application
或 Activity
對象。
?????? resId?? ??要使用的字符串資源ID,可以是已格式化文本。
?????? duration?
該信息的存續(xù)期間。值為 LENGTH_SHORT
或 LENGTH_LONG
異常
當(dāng)資源未找到時拋異常Resources.NotFoundException
?
public static Toast makeText (Context context, CharSequence text, int duration)
生成一個包含文本視圖的標(biāo)準(zhǔn) Toast 對象。
參數(shù)
context |
使用的上下文。通常是你的 |
resId |
要顯示的文本,可以是已格式化文本。 |
duration |
該信息的存續(xù)期間。值為 |
? |
? |
public void setDuration (int duration)
設(shè)置存續(xù)期間。
請參閱
LENGTH_SHORT
LENGTH_LONG
?
public void setGravity (int gravity, int xOffset, int yOffset)
設(shè)置提示信息在屏幕上的顯示位置。
(譯者注:自定義Toast的顯示位置,例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。Toast提示的位置xOffset:大于0向右移,小于0向左移
請參閱
Gravity
getGravity()
?
public void setMargin (float horizontalMargin, float verticalMargin)
設(shè)置視圖的欄外空白。
參數(shù)
horizontalMargin???????? 容器的邊緣與提示信息的橫向空白(與容器寬度的比)。
verticalMargin???????????? 容器的邊緣與提示信息的縱向空白(與容器高度的比)。
?
public void setText (int resId)
更新之前通過 makeText() 方法生成的 Toast 對象的文本內(nèi)容。
參數(shù)
resId????? 為 Toast 指定的新的字符串資源ID。
?
public void setText (CharSequence s)
更新之前通過 makeText() 方法生成的 Toast 對象的文本內(nèi)容。
參數(shù)
s?? 為 Toast 指定的新的文本。
?
public void setView (View view)
設(shè)置要顯示的 View 。
(譯者注:注意這個方法可以顯示自定義的toast視圖,可以包含圖像,文字等等。是比較常用的方法。)
請參閱
getView()
?
public void show ()
按照指定的存續(xù)期間顯示提示信息。
?
補充
???????? 文章鏈接
?????????????????? 讓Toast一直顯示的解決方法
?????????????????? 通知 Toast詳細(xì)用法(顯示view)
?????????????????? Android一種信息提示機制:Toast
?????????????????? [推薦] android Toast大全(五種情形)建立屬于你自己的Toast
???????? 示例代碼
?????????????????? 示例一:使用圖片的Toast
??????????????????????????? Toast toast = new Toast(this);?
ImageView?view = new ImageView(this);?
view.setImageResource(R.drawable.icon);?
toast.setView(view);?
toast.show();
?
?????????????????? 示例二:帶文字帶圖片Toast
???????????????????????????
?????????????????? ???????? private void showToast() {
// 1 創(chuàng)建Toast
?????????????????? ?????????????????? Toast toast = Toast.makeText(this, "圖文顯示", Toast.LENGTH_LONG);
// 2 創(chuàng)建Layout,并設(shè)置為水平布局
?????????????????? ?????????????????? LinearLayout mLayout = new LinearLayout(this);
?????????????????? ?????????????????? mLayout.setOrientation(LinearLayout.HORIZONTAL);
?????????????????? ?????????????????? ImageView mImage = new ImageView(this); // 用于顯示圖像的ImageView
?????????????????? ?????????????????? mImage.setImageResource(R.drawable.icon);
?????????????????? ?????????????????? View toastView = toast.getView(); // 獲取顯示文字的Toast View
?????????????????? ?????????????????? mLayout.addView(mImage); // 添加到Layout
?????????????????? ?????????????????? mLayout.addView(toastView);
// 3 關(guān)鍵,設(shè)置Toast顯示的View(上面生成的Layout).
?????????????????? ?????????????????? toast.setView(mLayout);
?????????????????? ?????????????????? toast.show();
???????? ?????????????????? }
?
?????????????????? 示例三:綜合Toast例子
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="vertical"
??? android:layout_width="fill_parent"
??? android:layout_height="fill_parent"
??? >
<Button android:id="@+id/button1"
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? android:text="Toast顯示View"
/>
<Button android:id="@+id/button2"
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? android:text="Toast直接輸出"
/>
<Button android:id="@+id/button3"
??? android:layout_width="fill_parent"
??? android:layout_height="wrap_content"
??? android:text="VolumeToast應(yīng)用"
/>
</LinearLayout>
Toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
??? android:orientation="vertical"
??? android:layout_width="fill_parent"
??? android:layout_height="fill_parent"
??? >
<ImageView android:src="@drawable/toast"
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
/>
<TextView android:id="@+id/tv1"
??? android:text=""
??? android:layout_width="wrap_content"
??? android:layout_height="wrap_content"
/>
</LinearLayout>
Volumetoast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
? android:layout_width="280dp"
? android:layout_height="wrap_content"
? android:gravity="center_horizontal"
? android:orientation="vertical"
? >
????????? <TextView??????????
????????????????? android:layout_width="fill_parent"
????????????????? android:layout_height="wrap_content"
????????????????? android:text="Volume"
????????? ?? />
????????? ?? <ProgressBar
????????????????? android:id="@+id/progress"
????????????????? android:layout_width="280dp"
????????????????? android:layout_height="wrap_content"
????????????????? android:progress="50"
????????????????? android:max="100"
????????????????? style="?android:attr/progressBarStyleHorizontal"
????????? ?? />
</LinearLayout>
Java代碼文件
public class toasttest extends Activity {
???
??????
??? @Override
??? public void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? setContentView(R.layout.main);
??????? Button button1=(Button)findViewById(R.id.button1);
??????? button1.setOnClickListener(bt1lis);
??????? Button button2=(Button)findViewById(R.id.button2);
??????? button2.setOnClickListener(bt2lis);
??????? Button button3=(Button)findViewById(R.id.button3);
??????? button3.setOnClickListener(bt3lis);
? ??}
??? OnClickListener bt1lis=new OnClickListener(){
?
????????????? @Override
????????????? public void onClick(View v) {
???????????????????? showToast();
????????????? }
?
??? };
??? OnClickListener bt2lis=new OnClickListener(){
????????????? @Override
????????????? public void onClick(View v) {
???????????????????? Toast.makeText(toasttest.this,"直接輸出測試", Toast.LENGTH_LONG).show();
????????????? }
?
??? };
??? OnClickListener bt3lis=new OnClickListener(){
????????????? @Override
????????????? public void onClick(View v) {????????????
???????????????????? showvolumeToast();
????????????? }
?
??? };
??? public void showToast(){
? ???????? LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
??? ?????? View view=li.inflate(R.layout.toast,null);
??? ?????? //把布局文件toast.xml轉(zhuǎn)換成一個view
??? ?????? Toast toast=new Toast(this);
??? ?????? toast.setView(view);
??? ?????? //載入view,即顯示toast.xml的內(nèi)容
??? ?????? TextView tv=(TextView)view.findViewById(R.id.tv1);
??? ?????? tv.setText("Toast顯示View內(nèi)容");
??? ?????? //修改TextView里的內(nèi)容
??? ?????? toast.setDuration(Toast.LENGTH_SHORT);
??? ?????? //設(shè)置顯示時間,長時間Toast.LENGTH_LONG,短時間為Toast.LENGTH_SHORT,不可以自己編輯
??? ?????? toast.show();
??? }
?????? public void showvolumeToast() {
????????????? // TODO Auto-generated method stub
?????? ????? LayoutInflater li=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
??? ?????? View volumeView=li.inflate(R.layout.volumetoast,null);
??????? ((ProgressBar)volumeView.findViewById(R.id.progress)).setProgress(((ProgressBar)volumeView.findViewById(R.id.progress)).getProgress() + 10);
??????? //這里還有點問題,就是progress的進(jìn)度條不動,因為我們主要是想給大家展示
???? //Toast的用法,這里就不深究了
??????? Toast volumeToast= new Toast(this);
??????? volumeToast.setGravity(Gravity.BOTTOM, 0, 100);
??????? volumeToast.setView(volumeView);
??????? volumeToast.setDuration(Toast.LENGTH_SHORT);??????
??????? volumeToast.show();
?????? }
}
?
備注:我們的代碼沒有重復(fù)概述中的通過代碼布局toast實現(xiàn)方法,我們是通過布局文件轉(zhuǎn)換成view視圖來實現(xiàn)復(fù)雜toast,這是兩種常用的方法,大家可以根據(jù)具體情況進(jìn)行選擇。
下載
/Files/over140/2010/11/demo_Toast.rar
?
不足之處
????? 現(xiàn)象:當(dāng)點擊一個按鈕?可以顯示一個四秒的toast,但是我要是連點兩下就是8秒 三下十二秒
????? 解決辦法:只用一個Toast, 自己設(shè)置toast.setText(), setDuration(); 之后無論多少次.show()都能馬上更新顯示, 一會就消失了