计时器
private Timer mTimer;
private TimerTask mTimerTask;
private TextView mTiming;//显示控件
/**
* 开启计时器
*/
private void startCountDown() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
mTimer = new Timer();
if (mTimerTask != null) {
mTimerTask.cancel();
}
mTimerTask = new TimerTask() {
@Override
public void run() {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
setCountDown();
}
});
}
};
mTimer.schedule(mTimerTask, 0, 1000); //0s后执行task,经过1s再次执行
}
private void setCountDown() {
String mCurrentTime = DateTimeUtil.getCurrentDate(DateTimeUtil.FORMAT_YMD_HMS);
//DateTimeUtil为工具栏,计算当前时间和两个时间的时差精确到秒
String distanceTime[] = DateTimeUtil.getDistanceTime(mStartTime,mCurrentTime);
for (int i = 1; i < distanceTime.length; i++) {
if (distanceTime[i].length() == 1) {
distanceTime[i] = "0" + distanceTime[i];
}
}
if(distanceTime[0].equals("0") && distanceTime[1].equals("00")){
mTiming.setText(distanceTime[2]+":"+distanceTime[3]);
}else if(distanceTime[0].equals("0") && !distanceTime[1].equals("00")){
mTiming.setText(distanceTime[1]+":"+distanceTime[2]+":"+distanceTime[3]);
}else if(!distanceTime[0].equals("0")){
mTiming.setText(distanceTime[0]+":"+distanceTime[1]+":"+distanceTime[2]+":"+distanceTime[3]);
}
}
键盘调用
private View mRootView;
private int mScreenHeight = 0;
private int mKeyboardOldHeight = -1;
private int mKeyboardNowHeight = -1;
/**
* 监听键盘收起事件
*/
private void keyboardHideListener() {
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
if (mScreenHeight == 0) {
mScreenHeight = r.bottom;
}
mKeyboardNowHeight = mScreenHeight - r.bottom;
if (mKeyboardOldHeight != -1 && mKeyboardNowHeight != mKeyboardOldHeight && mKeyboardNowHeight <= 0) {
hideCommentLayout();
}
mKeyboardOldHeight = mKeyboardNowHeight;
}
});
}