Toast的使用和简单封装

本文详细介绍了Android中Toast的使用,包括系统默认、位置调整、自定义布局及封装,通过实例展示了如何创建和展示不同类型的Toast,特别是对Toast的封装,确保了多次点击只显示最后一次的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toast的使用

效果图:

在这里插入图片描述
这里我用了4个按钮分别实现了Toast的4种不同的效果。

  1. 第一个按钮实现的是系统默认的Toast。
  2. 第二个按钮是改变Toast的位置为居中。
  3. 第三个按钮是自定义的Toast,自己画的布局来实现自定义的Toast,我这里只是为了实现这个效果,画的比较丑,就一个ImageView和一个TextView
  4. 第四个按钮是封装的Toast,实现点击多下按钮只显示最后一次的Toast,你点击前面3个按钮如果多点了几下,它就会停留很长的时间,而第四个按钮却不会,因为我封装好了。

下面我们来看看代码:

ToastActivity中的代码:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ToastActivity extends AppCompatActivity {
    private Button mBtn1, mBtn2, mBtn3, mBtn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
        //初始化
        mBtn1 = findViewById(R.id.bt_1);
        mBtn2 = findViewById(R.id.bt_2);
        mBtn3 = findViewById(R.id.bt_3);
        mBtn4 = findViewById(R.id.bt_4);

        onClick onClick = new onClick();
        // 设置侦听事件
        mBtn1.setOnClickListener(onClick);
        mBtn2.setOnClickListener(onClick);
        mBtn3.setOnClickListener(onClick);
        mBtn4.setOnClickListener(onClick);

    }

    class onClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                // 默认的Toast
                case R.id.bt_1:
                    Toast.makeText(ToastActivity.this, "默认的Toast", Toast.LENGTH_SHORT).show();
                    break;
                // 居中的Toast
                case R.id.bt_2:
                    Toast centerToast = Toast.makeText(ToastActivity.this, "居中的Toast", Toast.LENGTH_SHORT);
                    centerToast.setGravity(Gravity.CENTER, 0, 0);
                    centerToast.show();
                    break;
                // 自定义的Toast
                case R.id.bt_3:
                    Toast customToast = new Toast(getApplicationContext());
                    // 设置自定义的Toast显示自己写的布局
                    View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_toast_item, null);
                    customToast.setView(view);
                    customToast.show();
                    break;
                case R.id.bt_4:
                    ToastUtil.showMsg(getApplicationContext(), "封装的Toast");
                    break;
            }

        }
    }
}

ToastActivity的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
>
        <Button
            android:id="@+id/bt_1"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="默认的Toast"
            android:textAllCaps="false"
            />

        <Button
            android:id="@+id/bt_2"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="居中的Toast"
            android:textAllCaps="false"
            />

        <Button
            android:id="@+id/bt_3"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="自定义的Toast"
            android:textAllCaps="false"
            />

        <Button
            android:id="@+id/bt_4"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="封装的Toast"
            android:textAllCaps="false"
            />


</LinearLayout>

自定义的Toast需要加载的布局,我这里的名字是layout_toast_item.xml,代码如下:

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

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@color/colorGray"
        >

        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@mipmap/apple"
            />

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="130dp"
            android:gravity="center"
            android:text="自定义Toast"
            android:textColor="#000000"
            android:textSize="18sp"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

Toast的简单封装,ToastUtil类中的代码如下:

import android.content.Context;
import android.widget.Toast;

public class ToastUtil {

    public static Toast mToast;

    public static void showMsg(Context context, String msg) {

        if (mToast == null) {
            mToast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
        }
        mToast.show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值