Android中实现Toast效果Dialog自动消失
- UID
- 1066743
|
Android中实现Toast效果Dialog自动消失
1、核心代码
public class ToastDialog extends Dialog {
public static final int DIALOG_SHOW_TIME_SHORT = 3000;
public static final int DIALOG_SHOW_TIME_LONG = 5000;
/**
* 显示时间,单位毫秒,默认3秒
*/
private int showTime = DIALOG_SHOW_TIME_SHORT;
private int toastDialogTioIconId = 0;
private Object toastShowContent;
public ToastDialog(Context context) {
super(context, R.style.style_dialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setCanceledOnTouchOutside(false);
setCancelable(false);
setContentView(R.layout.dialog_customize_toast);
initView();
}
private void initView() {
ImageView ivToastDialogIcon = (ImageView) findViewById(R.id.iv_dialog_toast_icon);
if (getToastDialogTioIconId() != 0) {
ivToastDialogIcon.setImageResource(getToastDialogTioIconId());
}
TextView tvToastDialogContent = (TextView) findViewById(R.id.tv_dialog_toast_content);
ViewUtils.loadContent(tvToastDialogContent, getToastShowContent());
}
@Override
public void show() {
dismissDialog();
super.show();
}
private void dismissDialog() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
dismiss();
}
}, getShowTime());
}
@Override
public void dismiss() {
super.dismiss();
}
@Override
public void onBackPressed() {
return;
}
private int getShowTime() {
return showTime;
}
/**
* 设置显示时间,单位毫秒
* @param showTime 时间
*/
public void setShowTime(int showTime) {
this.showTime = showTime;
}
private int getToastDialogTioIconId() {
return toastDialogTioIconId;
}
/**
* 设置显示图标
* @param toastDialogTioIconId 图标ID
*/
public void setToastDialogTioIconId(int toastDialogTioIconId) {
this.toastDialogTioIconId = toastDialogTioIconId;
}
private Object getToastShowContent() {
return toastShowContent;
}
/**
* 显示内容
* @param toastShowContent 内容
*/
public void setToastShowContent(Object toastShowContent) {
this.toastShowContent = toastShowContent;
} |
|
|
|
|
|