当前位置: 代码网 > it编程>App开发>Android > Android Toast的几种使用方式及注意事项

Android Toast的几种使用方式及注意事项

2024年05月15日 Android 我要评论
toast是android中常用的组件,下面介绍下toast使用的几种方式和注意事项。toast的使用方式简单来说有下面五种:1、默认的显示// 第一个参数:当前的上下文环境。可用getapplica

toast是android中常用的组件,下面介绍下toast使用的几种方式和注意事项。

toast的使用方式简单来说有下面五种:

1、默认的显示

// 第一个参数:当前的上下文环境。可用getapplicationcontext()或activity的context 
// 第二个参数:要显示的字符串。也可是r.string中字符串id 
// 第三个参数:显示的时间长短。toast默认的有两个length_long(长)和length_short(短),也可以使用毫秒如2000ms 
toast toast=toast.maketext(mcontext, "默认的toast", toast.length_short); 
//显示toast信息 
toast.show();

2、自定义位置显示(值改变位置)

toast toast=toast.maketext(mcontext, "自定义显示位置的toast", toast.length_short); 
//第一个参数:设置toast在屏幕中显示的位置。这里设置是居中靠顶 
//第二个参数:相对于第一个参数设置toast位置的横向x轴的偏移量,正数向右偏移,负数向左偏移 
//第三个参数:相对于第一个参数设置toast位置的纵向y轴的偏移量,正数向下偏移,负数向上偏移 
//如果你设置的偏移量超过了屏幕的范围,toast将在屏幕内靠近超出的那个边界显示 
toast.setgravity(gravity.top|gravity.center, -50, 100); 
//屏幕居中显示,x轴和y轴偏移量都是0 
//toast.setgravity(gravity.center, 0, 0); 
toast.show();

3、带图片显示(能够显示一个图标)

toast toast=toast.maketext(mcontext, "显示带图片的toast", 2000); 
toast.setgravity(gravity.center, 0, 0); 
//创建图片视图对象 
imageview imageview= new imageview(mcontext); 
//设置图片 
imageview.setimageresource(r.drawable.image); 
//获得toast的布局 
linearlayout toastview = (linearlayout) toast.getview(); 
//设置此布局为横向的 
toastview.setorientation(linearlayout.horizontal); 
//将imageview在加入到此布局中的第一个位置 
toastview.addview(imageview, 0); 
toast.show();

4、完全自定义显示

layoutinflater inflater = getlayoutinflater(); 
//通过制定xml文件及布局id来填充一个视图对象 
view layout = inflater.inflate(r.layout.test,(viewgroup)findviewbyid(r.id.toast)); 
imageview image = (imageview) layout.findviewbyid(r.id.image); 
//设置布局中图片视图中图片 
image.setimageresource(r.drawable.toast_image); 
textview title = (textview) layout.findviewbyid(r.id.title); 
//设置标题 
title.settext("标题"); 
textview text = (textview) layout.findviewbyid(r.id.content); 
//设置内容 
text.settext("自定义toast"); 
toast toast= new toast(mcontext); 
toast.setgravity(gravity.center , 0, 0); 
toast.setduration(toast.length_long); 
toast.setview(layout); 
toast.show();

5、在其他线程中调用显示

toast只能运行在主ui线程,所以在线程中使用时必须结合handler,通过发消息的方式最终在主线程显示toast

handler handler=new handler(){ 
    @override 
    public void handlemessage(message msg) { 
    int what=msg.what; 
    switch (what) { 
        case 1: 
            showtoast(); 
        break; 
    } 
};
public void showtoast(){ 
    toast toast=toast.maketext(getapplicationcontext(), "toast在其他线程中显示",         toast.length_short); 
    toast.show(); 
} 
runnable runnable = new runnable(){
    @override 
    public void run() { 
        handler.sendemptymessage(1); 
    }
}
 

toast使用注意事项:

1、toast只能在ui线程当中使用,在非ui线程使用会抛异常;

2、使用toast时最好定义一个全局的 toast 对象,这样可以避免连续显示
toast 时不能取消上一次 toast 消息的情况(如果你有连续弹出 toast 的情况,避免
使用 toast.maketext)。

取消toast的方法为toast.cancel();

到此这篇关于android toast的几种使用方式的文章就介绍到这了,更多相关android toast的几种使用方式内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com