自定义Toast及解决Toast的重复显示问题

我们时常会用到Toast,但是源生的Toast有时会满足不了我们的需求,这就需要我们自定义了。

首先,来一个Toast工具类:

public class MyToast {
    private Toast mToast;
    private TextView textView;
    public void showToast(Context context, String text){

        View toastView = LayoutInflater.from(context).inflate(R.layout.mytoast, null);

        if (mToast == null){
            mToast = new Toast(context);
            mToast.setView(toastView);
            textView = (TextView) toastView.findViewById(R.id.toastText);
            textView.setText(text);
        }else {
            textView.setText(text);
        }
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.show();
    }

    //取消Toast(用于退出当前界面的时候)
    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }
}
贴上xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:alpha="0.9"
        android:id="@+id/toastText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shapetoast"
        android:text="1245"
        android:gravity="center"
        android:textColor="#000"
        />

</LinearLayout>

然后就是MainActiviy:

public class MainActivity extends AppCompatActivity {

    private MyToast myToast;
    private Context context;

    //用来记录第几次点击,并且验证不会弹出多个Toast
    private int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        myToast = new MyToast();

        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                myToast.showToast(context, "第" + count + "次弹出自定的Toast");
            }
        });
    }

    //返回键退出
    @Override
    public void onBackPressed() {
        myToast.cancelToast();
        super.onBackPressed();
    }
}
Demo下载

  • 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、付费专栏及课程。

余额充值