Android中使用自定义Toast

自定义Toast

1、layout下面toast_main文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_mainid"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/ToastContent"
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_marginBottom="75dp"
        android:background="@drawable/toast_background"
        android:gravity="center"
        android:textColor="@color/toastTextColor"/>

LinearLayout>

2、drawable下面toast_background文件设置toast背景形状颜色


<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    
    <solid android:color="@color/colorPrimary"/>
    <corners android:radius="23dp"/>
    <padding
        android:left="23dp"
        android:right="23dp"/>

shape>

3、自定义myToast类

package com.example.diaocha;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


public class MyToast {
    private boolean canceled = true;
    private Handler handler;
    private Toast toast;
    private TimeCount time;
    private TextView toast_content;


    public MyToast(Context context, ViewGroup viewGroup) {
        this(context, viewGroup, new Handler());
    }

    public MyToast(Context context, ViewGroup viewGroup, Handler handler) {
        this.handler = handler;

        View layout = LayoutInflater.from(context).inflate(R.layout.toast_main, viewGroup);
        toast_content = (TextView) layout.findViewById(R.id.ToastContent);
        if (toast == null) {
            toast = new Toast(context);
        }
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
    }

    /**
     * @param text     要显示的内容
     * @param duration 显示的时间长
     *                 根据LENGTH_MAX进行判断
     *                 如果不匹配,进行系统显示
     *                 如果匹配,永久显示,直到调用hide()
     */
    public void show(String text, int duration) {
        time = new TimeCount(duration, 1000);
        toast_content.setText(text);
        if (canceled) {
            time.start();
            canceled = false;
            showUntilCancel();
        }
    }

    /**
     * 隐藏Toast
     */
    public void hide() {
        if (toast != null) {
            toast.cancel();
        }
        canceled = true;
    }

    private void showUntilCancel() {
        if (canceled) {
            return;
        }
        toast.show();
        handler.postDelayed(new Runnable() {
            public void run() {
                showUntilCancel();
            }
        }, 3000);
    }

    /**
     * 计时器
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval); // 总时长,计时的时间间隔
        }

        @Override
        public void onFinish() { // 计时完毕时触发
            hide();
        }

        @Override
        public void onTick(long millisUntilFinished) { // 计时过程显示
        }

    }

}

4、MainActivity中调用
(1)创建对象,相应地方调用

private MyToast toast;

(2)要加入自定义方法

//自定义toast
private void toastMessage(String con)
{
    if (toast!=null)
    {
        toast.hide();
    }
    toast=new MyToast(MainActivity.this, (ViewGroup) this.findViewById(R.id.toast_mainid));
    toast.show(con,800);
}

你可能感兴趣的:(Android,安卓控件,android,android,studio)