您的位置:首页 > 移动开发 > Android开发

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

2016-06-01 16:24 381 查看
出处: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 int
LENGTH_LONG
 

          Show the view or text notification for a long period of time.
static int
LENGTH_SHORT
 

          Show the view or text notification for a short period of time.
 
构造方法摘要
Toast(Context context)
 

          Construct an empty Toast object.
 
方法摘要
 void
cancel()
 

          Close the view if it's showing, or don't show it if it isn't showing yet.
 int
getDuration()
 

          Return the duration.
 int
getGravity()
 

          Get the location at which the notification should appear on the screen.
 float
getHorizontalMargin()
 

          Return the horizontal margin.
 float
getVerticalMargin()
 

          Return the vertical margin.
 View
getView()
 

          Return the view.
 int
getXOffset()
 

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

          Return the Y offset in pixels to apply to the gravity's location.
static Toast
makeText(Context context, CharSequence text, int duration)
 

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

          Make a standard toast that just contains a text view with the text from a resource.
 void
setDuration(int duration)
 

          Set how long to show the view for.
 void
setGravity(int gravity, int xOffset, int yOffset)
 

          Set the location at which the notification should appear on the screen.
 void
setMargin(float horizontalMargin, float verticalMargin)
 

          Set the margins of the view.
 void
setText(CharSequence s)
 

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

          Update the text in a Toast that was previously created using one of the makeText() methods.
 void
setView(View view)
 

          Set the view to show.
 void
show()
 

          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重复显示等待时间过长的问题(未验证)

出处:http://www.ziliao1.com/Article/Show/B0E7BEF930216CB6C308033321C7BCEB.html

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消息框。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: