自定义toast

总是感觉项目中使用系统封装好toast样式太丑,所以决定自己写一个
1.工具类

package mytoast.example.mytoast;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.os.CountDownTimer;
/**
 * Created by jiawei on 2018/8/6.
 */

public class ToastUtils {

        private Toast mToast;
        private TextView mTextView;
        private String message;
        private Handler mHandler = new Handler();
        private boolean canceled = true;
        private TimeCount timeCount;
        public ToastUtils(Context context, int layoutId, String msg) {
            message = msg;
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //自定义布局
            View view = inflater.inflate(layoutId, null);
            //自定义toast文本
            mTextView = (TextView) view.findViewById(R.id.toast_msg);
            mTextView.setText(msg);
            Log.i("ToastUtil", "Toast start...");
            if (mToast == null) {
                mToast = new Toast(context);
                Log.i("ToastUtil", "Toast create...");
            }
            //设置toast居中显示
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.setView(view);
        }

    /**
     * 自定义居中显示toast
     */
    public void show() {
        mToast.show();
        Log.i("ToastUtil", "Toast show...");
    }


    /**
     * 自定义时长、居中显示toast
     * @param duration
     */
    public void show(int duration) {
        timeCount = new TimeCount(duration, 1000);
        Log.i("ToastUtil", "Toast show...");
        if (canceled) {
            timeCount.start();
            canceled = false;
            showUntilCancel();
        }
    }

    /**
     * 隐藏toast
     */
    private void hide() {
        if (mToast != null) {
            mToast.cancel();
        }
        canceled = true;
        Log.i("ToastUtil", "Toast that customed duration hide...");
    }

    private void showUntilCancel() {
        if (canceled) { //如果已经取消显示,就直接return
            return;
        }
        mToast.show();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Log.i("ToastUtil", "Toast showUntilCancel...");
                showUntilCancel();
            }
        }, Toast.LENGTH_LONG);
    }

    /**
     *  自定义计时器
     */
    private class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval); //millisInFuture总计时长,countDownInterval时间间隔(一般为1000ms)
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTextView.setText(message + ": " + millisUntilFinished / 1000 + "s后消失");
        }

        @Override
        public void onFinish() {
            hide();
        }
    }
}

2.使用

package mytoast.example.mytoast;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener{

    private Context context = MainActivity.this;
    private Button btDefaultToast;
    private Button btCenterToast;
    private Button btTopToast;
    private Button btCenterImgToast1;
    private Button btCenterImgToast2;
    private Button btCustomDurationToast;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }

    private void initView() {
        btDefaultToast = (Button)findViewById(R.id.bt_default_toast);
        btCenterToast = (Button)findViewById(R.id.bt_center_toast);
        btTopToast = (Button)findViewById(R.id.bt_top_toast);
        btCenterImgToast1 = (Button)findViewById(R.id.bt_center_img_toast1);
        btCenterImgToast2 = (Button)findViewById(R.id.bt_center_img_toast2);
        btCustomDurationToast = (Button)findViewById(R.id.bt_custom_duration_toast);
    }

    private void initListener() {
        btDefaultToast.setOnClickListener(this);
        btCenterToast.setOnClickListener(this);
        btTopToast.setOnClickListener(this);
        btCenterImgToast1.setOnClickListener(this);
        btCenterImgToast2.setOnClickListener(this);
        btCustomDurationToast.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_default_toast:
                Toast.makeText(context, "默认toast", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_center_toast:
                Toast toast = Toast.makeText(context, "自定义居中toast", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case R.id.bt_top_toast:
                Toast toast1 = Toast.makeText(context, "自定义顶部toast", Toast.LENGTH_SHORT);
                toast1.setGravity(Gravity.TOP, 0, 0);
                toast1.show();
                break;
            case R.id.bt_center_img_toast1:
                ToastUtils toastUtil = new ToastUtils(context, R.layout.toast_center, "完全自定义居中toast 1");
                toastUtil.show();
                break;
            case R.id.bt_center_img_toast2:
                ToastUtils toastUtil2 = new ToastUtils(context, R.layout.toast_center_horizontal, "完全自定义居中toast 2");
                toastUtil2.show();
                break;
            case R.id.bt_custom_duration_toast:
                ToastUtils toastUtil3 = new ToastUtils(context, R.layout.toast_center_horizontal, "带图片自定义时长toast");
                toastUtil3.show(5000);
                break;
            default:
                break;
        }
    }
}

3.展示
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值