Android学习笔记六十一:Toast(示例,出错代码)

出处:http://www.apihome.cn/api/android/Toast.html

android.widget
类 Toast

java.lang.Object
  继承者 android.widget.Toast

public class Toast
   
   
    
    extends Object
   
   

A toast is a view containing a quick little message for the user. The toast class helps you create and show those.

When the view is shown to the user, appears as a floating view over the application. It will never receive focus. The user will probably be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two examples are the volume control, and the brief message saying that your settings have been saved.

The easiest way to use this class is to call one of the static methods that constructs everything you need and returns a new Toast object.


字段摘要
static intLENGTH_LONG 
          Show the view or text notification for a long period of time.
static intLENGTH_SHORT 
          Show the view or text notification for a short period of time.
 
构造方法摘要
Toast(Context context) 
          Construct an empty Toast object.
 
方法摘要
 voidcancel() 
          Close the view if it's showing, or don't show it if it isn't showing yet.
 intgetDuration() 
          Return the duration.
 intgetGravity() 
          Get the location at which the notification should appear on the screen.
 floatgetHorizontalMargin() 
          Return the horizontal margin.
 floatgetVerticalMargin() 
          Return the vertical margin.
 ViewgetView() 
          Return the view.
 intgetXOffset() 
          Return the X offset in pixels to apply to the gravity's location.
 intgetYOffset() 
          Return the Y offset in pixels to apply to the gravity's location.
static ToastmakeText(Context context, CharSequence text, int duration) 
          Make a standard toast that just contains a text view.
static ToastmakeText(Context context, int resId, int duration) 
          Make a standard toast that just contains a text view with the text from a resource.
 voidsetDuration(int duration) 
          Set how long to show the view for.
 voidsetGravity(int gravity, int xOffset, int yOffset) 
          Set the location at which the notification should appear on the screen.
 voidsetMargin(float horizontalMargin, float verticalMargin) 
          Set the margins of the view.
 voidsetText(CharSequence s) 
          Update the text in a Toast that was previously created using one of the makeText() methods.
 voidsetText(int resId) 
          Update the text in a Toast that was previously created using one of the makeText() methods.
 voidsetView(View view) 
          Set the view to show.
 voidshow() 
          Show the view for the specified duration.
 
从类 java.lang.Object 继承的方法
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

LENGTH_SHORT

public static final int LENGTH_SHORT
Show the view or text notification for a short period of time. This time could be user-definable. This is the default.

另请参见:
setDuration(int), 常量字段值

LENGTH_LONG

public static final int LENGTH_LONG
Show the view or text notification for a long period of time. This time could be user-definable.

另请参见:
setDuration(int), 常量字段值
构造方法详细信息

Toast

public Toast(Context context)
Construct an empty Toast object. You must call setView(android.view.View) before you can call show().

参数:
context - The context to use. Usually your Application or Activity object.
方法详细信息

show

public void show()
Show the view for the specified duration.


cancel

public void cancel()
Close the view if it's showing, or don't show it if it isn't showing yet. You do not normally have to call this. Normally view will disappear on its own after the appropriate duration.


setView

public void setView(View view)
Set the view to show.

另请参见:
getView()

getView

public View getView()
Return the view.

另请参见:
setView(android.view.View)

setDuration

public void setDuration(int duration)
Set how long to show the view for.

另请参见:
LENGTH_SHORT, LENGTH_LONG

getDuration

public int getDuration()
Return the duration.

另请参见:
setDuration(int)

setMargin

public void setMargin(float horizontalMargin,
                      float verticalMargin)
Set the margins of the view.

参数:
horizontalMargin - The horizontal margin, in percentage of the container width, between the container's edges and the notification
verticalMargin - The vertical margin, in percentage of the container height, between the container's edges and the notification

getHorizontalMargin

public float getHorizontalMargin()
Return the horizontal margin.


getVerticalMargin

public float getVerticalMargin()
Return the vertical margin.


setGravity

public void setGravity(int gravity,
                       int xOffset,
                       int yOffset)
Set the location at which the notification should appear on the screen.

另请参见:
Gravity, getGravity()

getGravity

public int getGravity()
Get the location at which the notification should appear on the screen.

另请参见:
Gravity, getGravity()

getXOffset

public int getXOffset()
Return the X offset in pixels to apply to the gravity's location.


getYOffset

public int getYOffset()
Return the Y offset in pixels to apply to the gravity's location.


makeText

public static Toast makeText(Context context,
                             CharSequence text,
                             int duration)
Make a standard toast that just contains a text view.

参数:
context - The context to use. Usually your Application or Activity object.
text - The text to show. Can be formatted text.
duration - How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

makeText

public static Toast makeText(Context context,
                             int resId,
                             int duration)
                      throws Resources.NotFoundException
Make a standard toast that just contains a text view with the text from a resource.

参数:
context - The context to use. Usually your Application or Activity object.
resId - The resource id of the string resource to use. Can be formatted text.
duration - How long to display the message. Either LENGTH_SHORT or LENGTH_LONG
抛出:
Resources.NotFoundException - if the resource can't be found.

setText

public void setText(int resId)
Update the text in a Toast that was previously created using one of the makeText() methods.

参数:
resId - The new text for the Toast.

setText

public void setText(CharSequence s)
Update the text in a Toast that was previously created using one of the makeText() methods.

参数:
s - The new text for the Toast.



android toast设置比Toast.LENGTH_SHORT还短的持续时间(已验证)

出处: http://m.blog.csdn.net/article/details?id=45720031

<span style="white-space:pre">	</span>/**
	 * 
	 * 显示toast,自己定义显示长短。
	 * param1:activity  传入context
	 * param2:word   我们需要显示的toast的内容
	 * param3:time length  long类型,我们传入的时间长度(如500)
	 */
	public static void showToast(final Activity activity, final String word, final long time){
		activity.runOnUiThread(new Runnable() {	
			public void run() {
				final Toast toast = Toast.makeText(activity, word, Toast.LENGTH_LONG);
				toast.show();
				Handler handler = new Handler();
			        handler.postDelayed(new Runnable() {
			           public void run() {
			               toast.cancel(); 
			           }
			    }, time);
			}
		});
	}

解决Android Toast重复显示等待时间过长的问题(未验证)

Toast是一种简易的消息提示框,它无法获取焦点,按设置的时间来显示完以后会自动消失。一般用于帮助或提示。

当触发点击事件显示toast信息时,如果设置了时间长短类型为LENGTH_LONG,虽然回到后台运行,但是依然会显示toast信息,尤其是当连续点击时,toast就会排队等待直到所有toast显示完毕,这种界面的用户体验是很差的。

1 toast         2  toast       3 toast          4 toast          5 toast        

为了避免这种问题,可以再toast信息显示的地方加个判断,方法如下:

private Context mcontext;  private Toast mtoast;    if(mtoast!=null)  {      mtoast.setText(R.string.neterror);      }  else  {      /*       * 第一个参数:当前的上下文环境,用this或getApplicationContext()表示。       * 第二个参数:显示的字符串,用R.string表示。       * 第三个参数:显示的时间长短。用LENGTH_LONG(长)或LENGTH_SHORT(短)表示,也可以用毫秒。       */      mtoast=Toast.makeText(mcontext,R.string.neterror, Toast.LENGTH_SHORT);
} 
mtoast.show(); //显示toast信息

另外,还可以通过设置Toast的其他属性来设计自己风格的Toast消息框。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值