Android中Toast显示时间的自定义控制

  @SuppressLint("InflateParams")
    public static void show(Context context, String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.toast_layout, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_toast);
        textView.setText(text);
        Toast toast = new Toast(context);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(view);
       <span style="white-space:pre">	</span>showMyToast(toast, 1000);//时间间隔在这是1秒,这个是可以控制的

    }

    //自定义Toast控件
    //    private static void showMyToast(final Toast toast, final int cnt) {
    //        final Timer timer = new Timer();
    //        timer.schedule(new TimerTask() {
    //            @Override
    //            public void run() {
    //                toast.show();
    //            }
    //        }, 0, Toast.LENGTH_LONG);
    //        new Timer().schedule(new TimerTask() {
    //            @Override
    //            public void run() {
    //                toast.cancel();
    //                timer.cancel();
    //            }
    //        }, cnt);
    //    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android,可以通过自定义Toast的布局文件和使用Handler来实现自定义Toast显示时间和取消。 首先,创建一个自定义Toast布局文件,例如: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#CC000000" android:padding="10dp"> <ImageView android:id="@+id/toast_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:src="@drawable/ic_info"/> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFF"/> </LinearLayout> ``` 然后,在你的Activity,使用以下代码创建自定义Toast: ```java // 获取自定义布局文件 View toastView = LayoutInflater.from(this).inflate(R.layout.custom_toast_layout, null); // 设置Toast显示的图片和文本 ImageView imageView = toastView.findViewById(R.id.toast_image); imageView.setImageResource(R.drawable.ic_info); TextView textView = toastView.findViewById(R.id.toast_text); textView.setText("This is a custom Toast message."); // 创建Toast对象 Toast toast = new Toast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setView(toastView); // 延迟显示Toast Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { toast.show(); } }, 2000); // 取消Toast handler.postDelayed(new Runnable() { @Override public void run() { toast.cancel(); } }, 5000); ``` 上述代码,首先获取自定义布局文件,并设置Toast显示的图片和文本。然后创建Toast对象,并使用Handler来延迟2秒显示Toast,并在5秒后取消Toast。通过调用Toast的cancel()方法可以取消正在显示Toast

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值