Android的计时器和键盘处理

计时器

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;
            }
        });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值