Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發步步為營(32):玩轉 Toast 提示

編輯:關於 Android 程式設計

Toast 這個類別在 Android 中是用來彈出提示訊息的,取名「土司」大概是因為提示框的長相就像一塊吐司吧。下面我們就來動手實作,看看 Toast 有哪些玩法。

第一步:設計頁面

主頁面 toastview.xml,放五個按鈕分別觸發不同效果的 Toast:

       

自訂的 toast 頁面 customtoast.xml:

       

第二步:設計 Activity(ToastActivity.java)

package com.figo.helloworld; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class ToastActivity extends Activity implements OnClickListener { Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.toastview); findViewById(R.id.btnSimpleToast).setOnClickListener(this); findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this); findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this); findViewById(R.id.btnCustomToast).setOnClickListener(this); findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this); } public void showToast() { handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(),"我來自其他線程!", Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { Toast toast = null; switch (v.getId()) { case R.id.btnSimpleToast: Toast.makeText(getApplicationContext(),"默認Toast樣式", Toast.LENGTH_SHORT).show(); break; case R.id.btnSimpleToastWithCustomPosition: toast = Toast.makeText(getApplicationContext(), "自定義位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER,0, 0); toast.show(); break; case R.id.btnSimpleToastWithImage: toast = Toast.makeText(getApplicationContext(), "帶圖片的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER,0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon); toastView.addView(imageCodeProject,0); toast.show(); break; case R.id.btnCustomToast: LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.customtoast, (ViewGroup)findViewById(R.id.llToast)); ImageView image = (ImageView) layout.findViewById(R.id.imgToast); image.setImageResource(R.drawable.icon); TextView title = (TextView)layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView)layout.findViewById(R.id.tvTextToast); text.setText("完全自定義Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT| Gravity.TOP, 100, 60); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); break; case R.id.btnRunToastFromOtherThread: new Thread(new Runnable() { public void run() { showToast(); } }).start(); break; } } }

這個範例示範了 Toast 的五種常見用法:預設樣式、自訂顯示位置、加入圖片、完全自訂版面,以及從其他執行緒透過 Handler 回到主執行緒彈出提示。

第三步:在 AndroidManifest.xml 註冊 Activity

  

第四步:運行效果

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved