Android基础控件——Toast的自定义、按两次返回键返回桌面并弹出自定义吐司、仿映客吐司

Toast的自定义、按两次返回键返回桌面并弹出自定义吐司、仿映客吐司


步骤一:在xml文件中自定义吐司布局(模仿映客吐司)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#5DCD9F"
        android:gravity="center"
        android:text="再按一次返回键返回桌面"
        android:textColor="#fff"
        android:textSize="16dp" />
</RelativeLayout>

步骤二:在代码中的使用

        Toast toast = new Toast(this);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(View.inflate(this, R.layout.view_toast, null));
        toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        toast.setMargin(0, 0);
        toast.show();

步骤三:按两次返回键返回桌面功能代码(复写dispatchTouchEvent和finish方法)

    //两次退出
    private int isTouchBack;
    public boolean dispatchTouchEvent(MotionEvent ev) {
        isTouchBack = 0;
        return super.dispatchTouchEvent(ev);
    }

    public void finish() {
        //按2次返回键退出
        isTouchBack++;
        if (isTouchBack == 1) {
            //弹出自定义Toast
            ToastUtils.showToast(this, Config.TOAST_BACK_HOME);
        } else if (isTouchBack == 2) {
            super.finish();
        }
    }

步骤四:查看效果图

Android基础控件——Toast的自定义、按两次返回键返回桌面并弹出自定义吐司、仿映客吐司_第1张图片


你可能感兴趣的:(android,toast,控件,自定义吐司)