Android自定義吐司Toast:自定義樣式、自定義顯示時長

Android自定義吐司Toast:自定義樣式、自定義顯示時長

鑑於系統toast,通常都是黑色背景且位於界面底部,咱們看到有些app彈出的toast,有的在界面中間、有的在界面頂部,還有的是帶圖片的,那是怎麼實現的呢?android


瞭解系統toast類有哪些方法

  • setView( ):設置toast視圖,也就是經過layout佈局來控制toast顯示不一樣的視圖。
  • setGravity( ):設置toast顯示位置。
  • setDuration( ):設置toast顯示的時長,注意:此方法設置自定義時長不起做用,需用其餘方法實現。

自定義Toast類ToastUtil:包括自定義樣式、自定義顯示時長

package com.nicksong.toastutil.util;

import android.content.Context;
import android.os.CountDownTimer;
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 com.nicksong.toastutil.R;

/**
 * 做者:nicksong
 * 建立時間:2016/11/21
 * 功能描述:自定義toast樣式、顯示時長
 */

public class ToastUtil {

    private Toast mToast;
    private TextView mTextView;
    private TimeCount timeCount;
    private String message;
    private Handler mHandler = new Handler();
    private boolean canceled = true;

    public ToastUtil(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_LONG);
        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();
        }
    }
}

##具體使用方法git

package com.nicksong.toastutil;

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

import com.nicksong.toastutil.util.ToastUtil;

public class MainActivity extends AppCompatActivity 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:
                ToastUtil toastUtil = new ToastUtil(context, R.layout.toast_center, "徹底自定義居中toast 1");
                toastUtil.show();
                break;
            case R.id.bt_center_img_toast2:
                ToastUtil toastUtil2 = new ToastUtil(context, R.layout.toast_center_horizontal, "徹底自定義居中toast 2");
                toastUtil2.show();
                break;
            case R.id.bt_custom_duration_toast:
                ToastUtil toastUtil3 = new ToastUtil(context, R.layout.toast_center_horizontal, "帶圖片自定義時長toast");
                toastUtil3.show(5000);
                break;
            default:
                break;
        }
    }
}

##自定義樣式和時長的Toast效果圖 ![輸入圖片說明]github

##項目源碼 https://github.com/Ericsongyl/AndroidToastUtilapp

相關文章
相關標籤/搜索