歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發教程:淺談Toast

Android開發教程:淺談Toast

日期:2017/3/1 10:36:36   编辑:Linux編程
一.Toast的簡介

Toast是Android中一種提供給用戶簡短信息的視圖,該視圖已浮於應用程序之上的形式呈現給用戶。因為它並不獲得焦點,即使用戶正在輸入什麼也不會受到影響。它的目標是盡可能已不顯眼的方式,使用戶看到你提供的信息。顯示的時間是有限制的,過一段時間後會自動消失,不過Toast本身可以控制顯示時間的長短。

二.Toast的常用方法

int

getDuration()

返回Toast視圖顯示持續的時間.

int

getGravity()

取得提示信息在屏幕上顯示的位置.

float

getHorizontalMargin()

返回橫向欄外空白

float

getVerticalMargin()

返回縱向欄外空白.

View

getView()

返回 View 對象.

int

getXOffset()

返回相對於參照位置的橫向偏移像素量。

int

getYOffset()

返回相對於參照位置的縱向偏移像素量

static Toast

makeText(Context context, int resId, int duration)

生成一個從資源中取得的包含文本視圖的標准 Toast 對象。

context 使用的上下文。通常是你的 ApplicationActivity 對象

resId 要使用的字符串資源ID,可以是已格式化文本。

duration 該信息的存續期間。值為 LENGTH_SHORTLENGTH_LONG

static Toast

makeText(Context context, CharSequence text, int duration)

生成一個包含文本視圖的標准 Toast 對象.

void

setDuration(int duration)

設置Toast視圖顯示持續的時間,LENGTH_LONG表示持續時間較長,LENGTH_SHORT表示持續時間較短

void

setGravity(int gravity, int xOffset, int yOffset)

設置提示信息在屏幕上的顯示位置. (自定義Toast的顯示位置,例如toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0)可以把Toast定位在左上角。Toast提示的位置xOffset:大於0向右移,小於0向左移)

void

setMargin(float horizontalMargin, float verticalMargin)

設置視圖的欄外空白.

horizontalMargin 容器的邊緣與提示信息的橫向空白(與容器寬度的比)

verticalMargin 容器的邊緣與提示信息的縱向空白(與容器高度的比)。

void

setText(int resId)

更新之前通過 makeText() 方法生成的 Toast 對象的文本內容. resId 為 Toast 指定的新的字符串資源ID。

void

setText(CharSequence s)

更新之前通過 makeText() 方法生成的 Toast 對象的文本內容.

s 為 Toast 指定的新的文本

void

setView(View view)

設置要顯示的 View. 注意這個方法可以顯示自定義的toast視圖,可以包含圖像,文字等等。是比較常用的方法

void

show()

按照指定的存續期間顯示提示信息

三.Toast的不同顯示樣式

效果圖(有五種不同的Toast顯示樣式):

650) this.width=650;" height=239>

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <Button
  9. android:id="@+id/btn_1"
  10. android:text="@string/btn1"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. />
  14. <Button
  15. android:id="@+id/btn_2"
  16. android:text="@string/btn2"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. />
  20. <Button
  21. android:id="@+id/btn_3"
  22. android:text="@string/btn3"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. />
  26. <Button
  27. android:id="@+id/btn_4"
  28. android:text="@string/btn4"
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. />
  32. <Button
  33. android:id="@+id/btn_5"
  34. android:text="@string/btn5"
  35. android:layout_width="fill_parent"
  36. android:layout_height="wrap_content"
  37. />
  38. </LinearLayout>

toast.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="horizontal"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. android:padding="5dp"
  8. android:background="#708090"
  9. >
  10. <ImageView
  11. android:id="@+id/img"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. />
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="帶圖片文字的Toast"
  19. />
  20. </LinearLayout>

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello Toast!</string>
  4. <string name="app_name">ToastDemo</string>
  5. <string name="btn1">系統默認的Toast</string>
  6. <string name="btn2">自定義位置的Toast</string>
  7. <string name="btn3">帶只有圖片的Toast</string>
  8. <string name="btn4">有圖有文字的Toast</string>
  9. <string name="btn5">自定義布局的Toast</string>
  10. </resources>

ToastDemoActivity.java

  1. package com.android.toast.activity;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.Gravity;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.Toast;
  13. public class ToastDemoActivity extends Activity {
  14. private Button btn_1, btn_2, btn_3, btn_4, btn_5;
  15. private Toast toast = null;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. // TODO Auto-generated method stub
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. btn_1 = (Button) findViewById(R.id.btn_1);
  22. btn_2 = (Button) findViewById(R.id.btn_2);
  23. btn_3 = (Button) findViewById(R.id.btn_3);
  24. btn_4 = (Button) findViewById(R.id.btn_4);
  25. btn_5 = (Button) findViewById(R.id.btn_5);
  26. btn_1.setOnClickListener(new ButtonClick());
  27. btn_2.setOnClickListener(new ButtonClick());
  28. btn_3.setOnClickListener(new ButtonClick());
  29. btn_4.setOnClickListener(new ButtonClick());
  30. btn_5.setOnClickListener(new ButtonClick());
  31. }
  32. class ButtonClick implements OnClickListener{
  33. @Override
  34. public void onClick(View v) {
  35. // TODO Auto-generated method stub
  36. switch (v.getId()) {
  37. case R.id.btn_1:
  38. toast.makeText(ToastDemoActivity.this, "默認的Toast顯示", Toast.LENGTH_LONG).show();
  39. break;
  40. case R.id.btn_2:
  41. // getApplicationContext()得到程序當前的默認Context
  42. toast = Toast.makeText(getApplicationContext(), "自定義位置的Toast顯示",
  43. Toast.LENGTH_LONG);
  44. //設置Toast的位置
  45. toast.setGravity(Gravity.CENTER, toast.getXOffset()/2, toast.getYOffset()/2);
  46. toast.show();
  47. break;
  48. case R.id.btn_3:
  49. toast = Toast.makeText(getApplicationContext(), "只有圖片的Toast顯示",
  50. Toast.LENGTH_LONG);
  51. ImageView img = new ImageView(ToastDemoActivity.this);
  52. img.setImageResource(R.drawable.android);
  53. toast.setView(img);
  54. toast.show();
  55. break;
  56. case R.id.btn_4:
  57. toast = Toast.makeText(getApplicationContext(), "有圖有字的Toast", Toast.LENGTH_LONG);
  58. LinearLayout layout = (LinearLayout)toast.getView();
  59. ImageView img1 = new ImageView(getApplicationContext());
  60. img1.setImageResource(R.drawable.android);
  61. layout.addView(img1,0);
  62. toast.show();
  63. break;
  64. case R.id.btn_5:
  65. //將一個xml布局轉換成一個view對象
  66. LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  67. View view=inflater.inflate(R.layout.toast,null);
  68. Toast toast = new Toast(getApplicationContext());
  69. //在view中查找查找ImageView控件
  70. ImageView image = (ImageView) view.findViewById(R.id.img);
  71. image.setImageResource(R.drawable.android);
  72. toast.setView(view);
  73. toast.show();
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. }
  80. }
Copyright © Linux教程網 All Rights Reserved