Toast

一、toast:Toast是一种提供给用户简洁信息的视图。Toast类帮助你创建和显示该信息。

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失

 方法:

Public Methods
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, int resId, int duration)
Make a standard toast that just contains a text view with the text from a resource.
static  Toast makeText( Context context,  CharSequence text, int duration)
Make a standard toast that just contains a text view.
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(int resId)
Update the text in a Toast that was previously created using one of the makeText() methods.
void setText( CharSequence s)
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.
二、不同显示toast提示

@默认的情况下:

private  context mContext;
mContext=getApplicationContext();
String text="默认位置显示";
int duration="Toast.LENGTH_LONG/Toast.LENGTH_SHOW";
Toast  toast=new  Toast.makeText(mContext,text,duration).show();


如果你决定Toast应该出现在左上角,您可以设置重力是这样的:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

@@自定义显示位置的情况下:

private  Toast  toast;
toast=Toast.makeText(getApplicationContext(),"自定义位置显示",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();  


@@@带图片显示的情况下:

toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   LinearLayout toastView = (LinearLayout) toast.getView();
   ImageView imageCodeProject = new ImageView(getApplicationContext());
   imageCodeProject.setImageResource(R.drawable.icon);
   toastView.addView(imageCodeProject, 0);
   toast.show();

@@@@完全自定义的情况下:

LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定义Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();

@@@@@其他线程情况下:

new Thread(new Runnable() {
    public void run() {
     showToast();
    }
   }).start();


public  void  showToast(){


Toast toast=new  Toast.makeText(mContext,"请输入:",Toast.LENGTH_SHOW).show();
}

注意:

不要使用公共构造函数一个Toast,除非你要定义布局withsetView(视图)。如果你没有使用一个自定义的布局,您必须使用makeText(上下文,int,int)创建吐司。


android4.0中控制Toast时间:

http://kakadu.blog.51cto.com/4050768/1019824


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值