Android - Toast

1.概述
Toast是一种简洁提示视图,它浮现于应用程序之上,并不获得焦点,所以不会影响输入。

2.常量
int  LENGTH_LONG
持续显示视图或文本提示较长的时间。该事件长度可以定制。

int  LENGTH_SHORT
持续显示视图或文本提示较短的时间。可定制,该值为默认值

参见 setDuration(int)

3.构造函数

public Toast (Context context)
构造一个空的Toast对象。在调用show()之前,必须先调用setView()方法。只有使用setView()的时候才使用new Toast()构造toast对象,否则必须用makeText()方法来构建对象。并且这种方式获取的Toast对象不能使用setText更改文本信息。

Context使用的上下文,通常是你的application或者activity对象。

4.公共方法

public int cancel ()
如果视图已经显示则将其关闭,没有显示则不再显示。一般不需要调用该方法。正常情况下,视图会在超过设置的持续时长后自动消失。

public int getDuration ()
返回存续期间
参阅: setDuration(int)

public int getGravity ()
获取提示信息在屏幕上显示的位置。
参阅:Gravity setGravity()

public float getHorizontalMargin ()
返回横向栏外的空白。

public float getVerticalMargin ()
返回纵向栏外空白。

public View getView ()
返回 View 对象。
参阅:setView(View)

public int getXOffset ()
返回相对于参照位置的横向偏移像素量。

        Toast msg = Toast.makeText(Main.this, "Message", Toast.LENGTH_LONG);
        msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
        msg.show();


public int getYOffset ()
返回相对于参照位置的纵向偏移像素量。

public static Toast makeText (Context context, int resId, int duration)
生成一个从资源中取得的包含文本视图的标准 Toast 对象。
参数
context 使用的上下文。通常是你的 Application 或 Activity 对象。
resId 要使用的字符串资源ID,可以是已格式化文本。
duration 该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LON

     异常
      当资源未找到时抛异常Resources.NotFoundException

public static Toast makeText (Context context, CharSequence text, int duration)
生成一个包含文本视图的标准 Toast 对象。
参数
context 使用的上下文。通常是你的 Application 或 Activity 对象。
text 要显示的文本,可以是已格式化文本。
duration 该信息的存续期间。值为 LENGTH_SHORT 或 LENGTH_LONG


public void setDuration (int duration)
设置存续期间。
请参阅
LENGTH_SHORT
LENGTH_LONG

public void setGravity (int gravity, int xOffset, int yOffset)
设置提示信息在屏幕上的显示位置。
请参阅
Gravity
getGravity()

public void setMargin (float horizontalMargin, float verticalMargin)
设置视图的栏外空白。
参数
horizontalMargin         容器的边缘与提示信息的横向空白(与容器宽度的比)。
verticalMargin             容器的边缘与提示信息的纵向空白(与容器高度的比)。

public void setText (int resId)
更新之前通过 makeText() 方法生成的 Toast 对象的文本内容。
参数
resId      为 Toast 指定的新的字符串资源ID。

public void setText (CharSequence s)
更新之前通过 makeText() 方法生成的 Toast 对象的文本内容。
参数
s   为 Toast 指定的新的文本。

public void setView (View view)
设置要显示的 View 。
(译者注:注意这个方法可以显示自定义的toast视图,可以包含图像,文字等等。是比较常用的方法。)
请参阅
getView()

public void show ()
按照指定的存续期间显示提示信息。


以上为ToastAPI中文说明,转载于http://www.cnblogs.com/over140/archive/2010/11/12/1875403.html
也可以直接到android查看官方版





Activity类
package com.example.activitys;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Toast两种显示方式
 * 
 * 使用View显示时,需要注意LayoutInflater的使用,它的主要功能是将Layout中的xml文件转换为View,它是专门供Layout使用的inflater。
 * 虽然Layout也是View的子类,但是在Android中如果想将xml文件中的Layout转换为View放入Java代码中操作,只能通过Inflater,而不能通过findViewById()。
 * 
 * @author Administrator
 */
public class ToastActivity extends Activity 
{

	/**
	 * 直接显示
	 */
	private OnClickListener btnToastClickHandler = new OnClickListener()
		{

			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				showToast("测试信息1");
			}
			
		};
	
	/**
	 * 使用View显示
	 */
	private OnClickListener btnToastViewClickHandler = new OnClickListener()
	{

		@Override
		public void onClick(View v) 
		{
			// TODO Auto-generated method stub
			LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			//XML转换为view
			View view = (View)inflater.inflate(R.layout.toastview, null);
			
			showToastView("测试信息2",view);
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.toast);
		
		//获取组件对象并监听Click事件
		Button btn_toast = (Button)this.findViewById(R.id.btn_toast);
		btn_toast.setOnClickListener(btnToastClickHandler);
		
		Button btn_toast_view = (Button)this.findViewById(R.id.btn_toast_ivew);
		btn_toast_view.setOnClickListener(btnToastViewClickHandler);
		
	}
	
	
	/**
	 * Toast提示
	 * @param str
	 */
	public void showToast(String v)
	{
		Toast toast = Toast.makeText(this, v, Toast.LENGTH_SHORT);
		
		//设置toast显示的位置
		toast.setGravity(Gravity.CENTER, 0, 180);
		//显示toast
		toast.show();
	}
	
	/**
	 * Toast使用View提示
	 * @param v
	 * @param view
	 */
	public void showToastView(String v,View view)
	{
		Toast toast = new Toast(this);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.setView(view);
		TextView text = (TextView)view.findViewById(R.id.texttoast);
		text.setText(v); // 此处将v改成"显示Toast",不知为何最终结果只显示一个"显"字。其余文字均无问题。
		toast.show();
	}

}


toastview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <!-- android:contentDescription="@string/app_name" 避免编辑器的警告提示 -->
    <ImageView
        android:id="@+id/imagetoast" 
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/texttoast"
        android:text="@string/btn_toast_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:width="100dp"/>
    
</LinearLayout>



toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_toast"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/btn_toast"/>
    
    
    <Button
        android:id="@+id/btn_toast_ivew"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="@string/btn_toast_view"/>

</LinearLayout>

猜你喜欢

转载自jacksang.iteye.com/blog/2053208