Android开发教程:浅谈Toast


一.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 使用的上下文。通常是你的 Application Activity 对象

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

duration 该信息的存续期间。值为 LENGTH_SHORT LENGTH_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显示样式):

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

相关内容