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