在Android中使用Toast進行提示

來源:互聯網
上載者:User

Toast可能是1個相當有Android特色的東西,在現實中也經常被用到,本教程會對Toast的使用進行1個比較全面的總結,一共有4個例子,分別講述最簡單的用法,如何調整顯示位置,以及如何建立自訂的Toast顯示。
這篇教程,以代碼為主,注釋中對使用方法進行了一些講解,基本上可以作為1個Toast用法速查。
Android中經常用到的另1種提示方法可以參見另一篇教程:《教程:在Android中使用Notification進行提示》

Activity: ToastSample.java

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
import net.learningandroid.apitest.R;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast; /** * Toast 使用大全. * @author bing * */public class ToastSample extends Activity implements View.OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.toast_sample); findViewById(R.id.btnToastNormal).setOnClickListener(this);findViewById(R.id.btnToastPosition).setOnClickListener(this);findViewById(R.id.btnToastMargin).setOnClickListener(this);findViewById(R.id.btnToastCustomView).setOnClickListener(this);} @Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btnToastNormal://最普通的調用方式,顯示時間長度為了short,這個值根據不同的系統會有不同,也可自行指定毫秒數//第1個參數是Context,一般直接指定為當前Activity執行個體即可//第2個參數是要顯示的文本,此處直接使用String,建議使用在xml中預定義的string//第3個參數是顯示時間長度,單位為毫秒數,此處使用了預定義的Toast.LENGTH_SHORT,//另有Toast.LENGTH_LONG可以使用,這2個值會根據系統而略有不同//別忘了最後的.show()Toast.makeText(this, "Toast text, normal", Toast.LENGTH_SHORT).show();break;case R.id.btnToastPosition://預設的Gravity就是Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM//此處只對 yOffset 進行調整,讓文本顯示的位置更往靠下一些Toast t2=Toast.makeText(this, "Toast text with specific position", Toast.LENGTH_LONG);t2.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 10);t2.show();break;case R.id.btnToastMargin://如果希望對顯示位置進行較大幅度的調整,建議使用了setMargin方法//setMargin接受的參數分別是橫向和縱向的百分比,這樣在不同解析度下的適應力更好。//此處是修改為在螢幕縱向正中間的上方顯示Toast t3=Toast.makeText(this, "Toast text with specific margin and position", Toast.LENGTH_SHORT);t3.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 0);t3.setMargin(0f, 0.5f);t3.show();break;case R.id.btnToastCustomView://使用自訂的View來顯示Toast,必須先編寫1個layout定義檔案//事實上Toast.makeText方法也是這樣調用的LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.toast_view_sample,                               (ViewGroup) findViewById(R.id.toastSampleLayout), false); TextView text = (TextView) layout.findViewById(R.id.toast_text);text.setText("Toast with custom view, it's a long text :" +" Manuka honey interferes with bacteria infecting a wound" +" by keeping the microbes from attaching to tissue" +" and even by making antibiotics more effective." +" Cynthia Graber reports. "); Toast t4 = new Toast(this);t4.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 50);t4.setDuration(Toast.LENGTH_LONG);t4.setView(layout);t4.show();break;default:break;}}}

第4個例子中使用的自訂View的定義檔案:toast_view_sample.xml

123456789101112131415161718
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:id="@+id/toastViewLayout" android:orientation="horizontal"android:padding="20dp" android:background="@android:drawable/toast_frame">    <ImageView android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_marginRight="10dp"               android:src="@drawable/toast_icon"               />    <TextView android:id="@+id/toast_text"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:textColor="#FFFFFFFF"              /></LinearLayout>

這裡用到@android:drawable/toast_frame其實就是Android預設的Toast所使用的背景,而@drawable/toast_icon則是我隨便找了1個表徵圖來用的。
最終運行效果:

2011,
Bing. 著作權。 所有轉載請以連結方式進行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.