初始化
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.arpa.hndahesudintocctmsdriver.util.alert;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.arpa.hndahesudintocctmsdriver.R;
|
||||
|
||||
|
||||
public class CustomDialog extends Dialog {
|
||||
private String content;
|
||||
private boolean key=false;
|
||||
public CustomDialog(Context context, String content) {
|
||||
super(context, R.style.CustomDialog);
|
||||
this.content=content;
|
||||
initView();
|
||||
}
|
||||
public CustomDialog(Context context, String content,boolean key) {
|
||||
super(context, R.style.CustomDialog);
|
||||
this.content=content;
|
||||
this.key=key;
|
||||
initView();
|
||||
|
||||
}
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
switch (keyCode){
|
||||
case KeyEvent.KEYCODE_BACK:
|
||||
if(CustomDialog.this.isShowing())
|
||||
if (!key){
|
||||
CustomDialog.this.dismiss();
|
||||
}
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void initView(){
|
||||
setContentView(R.layout.load);
|
||||
((TextView)findViewById(R.id.tvcontent)).setText(content);
|
||||
setCanceledOnTouchOutside(true);
|
||||
WindowManager.LayoutParams attributes = getWindow().getAttributes();
|
||||
attributes.alpha=0.8f;
|
||||
getWindow().setAttributes(attributes);
|
||||
setCancelable(false);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.arpa.hndahesudintocctmsdriver.util.alert;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.CountDownTimer;
|
||||
import android.os.Handler;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* @author hlh
|
||||
* @version 1.0.0
|
||||
* @date 2021/11/2 21:02
|
||||
* @description:
|
||||
*/
|
||||
public class ToastUtil {
|
||||
|
||||
private Toast mToast;
|
||||
private TimeCount timeCount;
|
||||
private String message;
|
||||
private int gravity;
|
||||
private Context mContext;
|
||||
private Handler mHandler = new Handler();
|
||||
private boolean canceled = true;
|
||||
|
||||
public ToastUtil(Context context, int gravity, String msg) {
|
||||
message = msg;
|
||||
mContext = context;
|
||||
this.gravity = gravity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义时长、居中显示toast
|
||||
*
|
||||
* @param duration
|
||||
*/
|
||||
public void show(int duration) {
|
||||
timeCount = new TimeCount(duration, 1000);
|
||||
if (canceled) {
|
||||
timeCount.start();
|
||||
canceled = false;
|
||||
showUntilCancel();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 隐藏toast
|
||||
*/
|
||||
public void hide() {
|
||||
if (mToast != null) {
|
||||
mToast.cancel();
|
||||
}
|
||||
if (timeCount != null) {
|
||||
timeCount.cancel();
|
||||
}
|
||||
canceled = true;
|
||||
}
|
||||
|
||||
private void showUntilCancel() {
|
||||
if (canceled) { //如果已经取消显示,就直接return
|
||||
return;
|
||||
}
|
||||
mToast = Toast.makeText(mContext,message,Toast.LENGTH_LONG);
|
||||
mToast.setGravity(gravity, 0, 0);
|
||||
mToast.show();
|
||||
mHandler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
showUntilCancel();
|
||||
}
|
||||
}, 3500);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义计时器
|
||||
*/
|
||||
private class TimeCount extends CountDownTimer {
|
||||
|
||||
public TimeCount(long millisInFuture, long countDownInterval) {
|
||||
super(millisInFuture, countDownInterval); //millisInFuture总计时长,countDownInterval时间间隔(一般为1000ms)
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTick(long millisUntilFinished) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user