Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android Toast 全攻略(五種情境):打造屬於你自己的 Toast

編輯:Android 開發實例

Toast 常用來向使用者顯示一些提示或說明訊息。以下我實作了五種效果,藉此說明 Toast 的強大之處,並示範如何定義一個屬於你自己的 Toast。

1. 預設效果

程式碼

  1. Toast.makeText(getApplicationContext(), "預設 Toast 樣式",
  2.      Toast.LENGTH_SHORT).show();

2. 自訂顯示位置

  1. toast = Toast.makeText(getApplicationContext(),
  2.      "自訂位置 Toast", Toast.LENGTH_LONG);
  3.    toast.setGravity(Gravity.CENTER, 0, 0);
  4.    toast.show();

3. 帶圖片效果

  1. toast = Toast.makeText(getApplicationContext(),
  2.      "帶圖片的 Toast", Toast.LENGTH_LONG);
  3.    toast.setGravity(Gravity.CENTER, 0, 0);
  4.    LinearLayout toastView = (LinearLayout) toast.getView();
  5.    ImageView imageCodeProject = new ImageView(getApplicationContext());
  6.    imageCodeProject.setImageResource(R.drawable.icon);
  7.    toastView.addView(imageCodeProject, 0);
  8.    toast.show();

4. 完全自訂效果

  1. LayoutInflater inflater = getLayoutInflater();
  2.    View layout = inflater.inflate(R.layout.custom,
  3.      (ViewGroup) findViewById(R.id.llToast));
  4.    ImageView image = (ImageView) layout
  5.      .findViewById(R.id.tvImageToast);
  6.    image.setImageResource(R.drawable.icon);
  7.    TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
  8.    title.setText("Attention");
  9.    TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
  10.    text.setText("完全自訂 Toast");
  11.    toast = new Toast(getApplicationContext());
  12.    toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
  13.    toast.setDuration(Toast.LENGTH_LONG);
  14.    toast.setView(layout);
  15.    toast.show();

5. 在其他執行緒中顯示

  1. new Thread(new Runnable() {
  2.     public void run() {
  3.      showToast();
  4.     }
  5.    }).start();

完整程式碼

1. Main.java

  1. package com.wjq.toast;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.view.Gravity;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.View.OnClickListener;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. public class Main extends Activity implements OnClickListener {
  15.  Handler handler = new Handler();
  16.  @Override
  17.  public void onCreate(Bundle savedInstanceState) {
  18.   super.onCreate(savedInstanceState);
  19.   setContentView(R.layout.main);
  20.  
  21.   findViewById(R.id.btnSimpleToast).setOnClickListener(this);
  22.   findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);
  23.   findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
  24.   findViewById(R.id.btnCustomToast).setOnClickListener(this);
  25.   findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
  26.  
  27.  }
  28.  
  29.  public void showToast() {
  30.   handler.post(new Runnable() {
  31.    @Override
  32.    public void run() {
  33.     Toast.makeText(getApplicationContext(), "我來自其他執行緒!", Toast.LENGTH_SHORT).show();
  34.  
  35.    }
  36.   });
  37.  }
  38.  
  39.  @Override
  40.  public void onClick(View v) {
  41.   Toast toast = null;
  42.   switch (v.getId()) {
  43.   case R.id.btnSimpleToast:
  44.    Toast.makeText(getApplicationContext(), "預設 Toast 樣式", Toast.LENGTH_SHORT).show();
  45.    break;
  46.   case R.id.btnSimpleToastWithCustomPosition:
  47.    toast = Toast.makeText(getApplicationContext(), "自訂位置 Toast", Toast.LENGTH_LONG);
  48.    toast.setGravity(Gravity.CENTER, 0, 0);
  49.    toast.show();
  50.    break;
  51.   case R.id.btnSimpleToastWithImage:
  52.    toast = Toast.makeText(getApplicationContext(), "帶圖片的 Toast", Toast.LENGTH_LONG);
  53.    toast.setGravity(Gravity.CENTER, 0, 0);
  54.    LinearLayout toastView = (LinearLayout) toast.getView();
  55.    ImageView imageCodeProject = new ImageView(getApplicationContext());
  56.    imageCodeProject.setImageResource(R.drawable.icon);
  57.    toastView.addView(imageCodeProject, 0);
  58.    toast.show();
  59.    break;
  60.   case R.id.btnCustomToast:
  61.    LayoutInflater inflater = getLayoutInflater();
  62.    View layout = inflater.inflate(R.layout.custom, (ViewGroup) findViewById(R.id.llToast));
  63.    ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
  64.    image.setImageResource(R.drawable.icon);
  65.    TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
  66.    title.setText("Attention");
  67.    TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
  68.    text.setText("完全自訂 Toast");
  69.    toast = new Toast(getApplicationContext());
  70.    toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
  71.    toast.setDuration(Toast.LENGTH_LONG);
  72.    toast.setView(layout);
  73.    toast.show();
  74.    break;
  75.   case R.id.btnRunToastFromOtherThread:
  76.    new Thread(new Runnable() {
  77.     public void run() {
  78.      showToast();
  79.     }
  80.    }).start();
  81.    break;
  82.   }
  83.  }
  84. }

2. main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  3.  android:orientation="vertical" android:layout_width="fill_parent"
  4.  android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
  5.  <Button android:layout_height="wrap_content"
  6.   android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
  7.   android:text="預設"></Button>
  8.  <Button android:layout_height="wrap_content"
  9.   android:layout_width="fill_parent" android:text="自訂顯示位置"
  10.   android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
  11.  <Button android:layout_height="wrap_content"
  12.   android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
  13.   android:text="帶圖片"></Button>
  14.  <Button android:layout_height="wrap_content"
  15.   android:layout_width="fill_parent" android:text="完全自訂"
  16.   android:id="@+id/btnCustomToast"></Button>
  17.  <Button android:layout_height="wrap_content"
  18.   android:layout_width="fill_parent" android:text="其他執行緒"
  19.   android:id="@+id/btnRunToastFromOtherThread"></Button>
  20.  </LinearLayout>

3. custom.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.  xmlns:android="https://schemas.android.com/apk/res/android"
  4.  android:layout_height="wrap_content" android:layout_width="wrap_content"
  5.  android:background="#ffffffff" android:orientation="vertical"
  6.  android:id="@+id/llToast">
  7.  <TextView
  8.   android:layout_height="wrap_content"
  9.   android:layout_margin="1dip"
  10.   android:textColor="#ffffffff"
  11.   android:layout_width="fill_parent"
  12.   android:gravity="center"
  13.   android:background="#bb000000"
  14.   android:id="@+id/tvTitleToast" />
  15.  <LinearLayout
  16.   android:layout_height="wrap_content"
  17.   android:orientation="vertical"
  18.   android:id="@+id/llToastContent"
  19.   android:layout_marginLeft="1dip"
  20.   android:layout_marginRight="1dip"
  21.   android:layout_marginBottom="1dip"
  22.   android:layout_width="wrap_content"
  23.   android:padding="15dip"
  24.   android:background="#44000000" >
  25.   <ImageView
  26.    android:layout_height="wrap_content"
  27.    android:layout_gravity="center"
  28.    android:layout_width="wrap_content"
  29.    android:id="@+id/tvImageToast" />
  30.   <TextView
  31.    android:layout_height="wrap_content"
  32.    android:paddingRight="10dip"
  33.    android:paddingLeft="10dip"
  34.    android:layout_width="wrap_content"
  35.    android:gravity="center"
  36.    android:textColor="#ff000000"
  37.    android:id="@+id/tvTextToast" />
  38.  </LinearLayout>
  39. </LinearLayout>

源碼下載

轉自:https://www.cnblogs.com/salam/archive/2010/11/10/1873654.html

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