android 一些小知识

来源:互联网 发布:c还是java 编辑:程序博客网 时间:2024/05/17 23:48

1,如何自定义一个toast

public static void showToast(Context context){    LayoutInflater inflater = LayoutInflater.from(context);    View toastView = inflater.inflate(R.layout.toast_test_custome, null);    Toast toast = new Toast(context);     toast.setDuration(3000);    toast.setView(toastView);                //设置自定义view    toast.setGravity(Gravity.CENTER, 0, 0);  //控制显示到屏幕中间    toast.show();                            //注意:一定要调用才能显示}

2,自定义toast显示时间

static CountDownTimer mCountDownTimer;    public static void showToast(final Toast toast, long duration, final OnToastStatus toastStatu) {        final long tickTime = 200;        mCountDownTimer = new CountDownTimer(duration, tickTime) {            @Override            public void onTick(long millisUntilFinished) {                if(millisUntilFinished <= 200){                    showToast("最长可录制一分钟");                }else{                    toast.show();                }            }            @Override            public void onFinish() {                toast.cancel();                if(toastStatu != null){                    toastStatu.toastStop();                }            }        }.start();    }    public interface OnToastStatus{        public void toastStop();    }    /**     * 停止显示Toast     */    public static void stopToast(Toast toast){        if(toast != null){            toast.cancel();        }        if(mCountDownTimer != null){            mCountDownTimer.cancel();        }    }
原创粉丝点击