弹出窗口中的CountDownTimer [英] CountDownTimer inside Popup

查看:160
本文介绍了弹出窗口中的CountDownTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哦,所以我已更新为:

final AlertDialog.Builder popup_timer = new AlertDialog.Builder(ScoreNewGame.this);
popup_timer.setTitle("Timer:\t90 sec between games");
    CountDownTimer gameTimer = new CountDownTimer(9000, 1000) 
    {
        @Override
        public void onTick(long time_remaining) 
        {
            popup_timer.setMessage("Time remaining:\t" + time_remaining);
        }
        @Override
        public void onFinish() 
        {
        }
    };
    gameTimer.start();
    popup_timer.show();

但是现在我收到以下错误-

But now I get the following error-

Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75)

我搜索了一下,但找不到任何解决方案.同样,0x010802c9与R.java中的任何内容都不对应. 该应用程序不会崩溃,但弹出窗口不会像我想要的那样显示倒数计时器

I searched around a bit but can't find any solution. Also, 0x010802c9 doesn't correspond to anything in R.java. The app doesn't crash though, the popup just doesn't display the countdowntimer like I want

推荐答案

我建议改为创建一个对话框片段.届时,您可以使用所需的任何方式处理片段中的倒数计时器.

I would suggest creating a dialog fragment instead. At that point you can handle the countdown timer in the fragment any which way you want.

这是一个对话框片段的示例:

Here's an example of a dialog fragment:

public class CountDownDialog extends DialogFragment {

private TextView mCountdownView;

public CountDownDialog(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_countdowntimer_dialog, container);
    mCountdownView = (TextView) view.findViewById(R.id.countdownTimer);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    CountDownTimer gameTimer = new CountDownTimer(10000, 1000) {
        @Override
        public void onTick(long l) {
            mCountdownView.setText(""+((int)Math.round(l/1000.0)-1));
        }

        @Override
        public void onFinish() {
            dismiss();
        }
    };
    gameTimer.start();
}
}

这就是你的称呼方式

    CountDownDialog countDownDialog = new CountDownDialog();
    countDownDialog.show(getSupportFragmentManager(), "fragment_countdownTimer");

我使用了一个非常简单的布局来测试片段:

I used a pretty simple layout for the fragment to to test it:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Countdown:"
        android:id="@+id/countdownTitle"
        android:layout_marginLeft="@dimen/activity_horizontal_margin" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="9"
        android:id="@+id/countdownTimer"
        android:layout_marginLeft="8dp" />
</LinearLayout>

这篇关于弹出窗口中的CountDownTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆