import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.widget.TextView;
public class CustomToast {
//Toast.LENGTH_SHORT
public static final int DIALOG_THEME_TIP_SHORT = 0;
//Toast.LENGTH_LONG
public static final int DIALOG_THEME_TIP_LONG = 1;
private int layout;
private int style;
private Dialog dialog = null;
private final Context context;
private final int theme;
private OnCustomDialogListener mListener;
public CustomToast(Context context, int theme, String message) {
this.context = context;
this.theme = theme;
init();
setMessage(message);
}
private void init() {
style = R.style.DialogTheme;
dialog = new Dialog(context, style) {
@Override
protected void onStop() {
super.onStop();
if (mListener != null) {
mListener.onFinish();
}
}
};
layout = R.layout.dialog_toast;
dialog.setContentView(layout);
}
/**
* 设置提示内容 * * @param message * @return
*/
public MyToast setMessage(String message) {
TextView title = (TextView) dialog.findViewById(R.id.text);
title.setText(message);
return this;
}
public MyToast setOnCustomDialogListener(OnCustomDialogListener listener) {
this.mListener = listener;
return this;
}
public Dialog show() {
dialog.show();
long delayMillis = 0;
if (theme == DIALOG_THEME_TIP_LONG) {
delayMillis = 2000;
} else {
delayMillis = 1000;
}
mHandler.postDelayed(new CloseDialog(), delayMillis);
return dialog;
}
public void dismiss() {
if (dialog == null) {
return;
}
if (isShowing()) {
dialog.dismiss();
dialog.getWindow().setWindowAnimations(R.anim.translate);
}
}
public boolean isShowing() {
return dialog.isShowing();
}
Handler mHandler = new Handler();
private class CloseDialog implements Runnable {
@Override
public void run() {
dismiss();
}
}
}
public interface OnCustomDialogListener {
public void onFinish();
}
android CustomToast
最新推荐文章于 2024-08-22 10:14:23 发布