【问题标题】:CountDownTimer stops when PopupWindow appears当 PopupWindow 出现时 CountDownTimer 停止
【发布时间】:2017-12-26 16:51:20
【问题描述】:

在 Android 活动中,我使用 CountDownTimer,如下所示:

    timer = new CountDownTimer(time, 1000) {
        public void onTick(long millisUntilFinished) {
            timeView.setText(String.format(Locale.getDefault(), "%ds", millisUntilFinished / 1000));

            if (millisUntilFinished <= 10000) {
                // Blinking text
                ViewUtil.setBlinking(timeView,Animation.INFINITE);
            } else {
                timeView.clearAnimation();
            }
        }

        public void onFinish() {
            timeView.clearAnimation();
            timeView.setText("0s");
    }.start();

此计时器仅以秒为单位倒计时。在同一个活动中,我还使用PopupWindow 类显示了一个弹出窗口,如下所示:

    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ConstraintLayout mRelativeLayout = (ConstraintLayout) findViewById(R.id.task_layout);
        View customView = inflater.inflate(R.layout.popup_window,mRelativeLayout,false);
        mPopupWindow = new PopupWindow(
                customView,
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        mPopupWindow.setElevation(5.0f);
        mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER,0,0);

问题是,一旦弹出窗口出现,CountDownTimer 就会以某种方式停止。我想要的是CountDownTimer 在弹出窗口出现并且背景视图仍然更新时也会运行。

如何做到这一点?

【问题讨论】:

    标签: android popupwindow countdowntimer android-popupwindow


    【解决方案1】:

    您在 UI 线程中运行 CountDownTimer,当您弹出窗口时线程会转到该窗口,但是我搜索了 PopupWindow 文档,但它的文档薄弱,找不到太多

    在后台使用CountDownTimer 解决它。使用AsynctaskServiceRunnable

    【讨论】:

    • 非常感谢。但是如何从后台线程更新 UI(TextView)?
    • 欢迎,UI 更新的首选方法是 Asynctask。在 onPostExecute 中更新 UI
    • 但我想每秒更新一次(倒计时)。 onPostExecute 只执行一次...
    • @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(timer =! null) doInBackground(); }
    • 更新UI后向上代码调用doInBackground,但是在onPostExecute中放一个Runnable postDelayed 1000ms,因为这会让处理器头疼,可能会通过异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多