Popup中的CountDownTimer

时间:2014-05-09 22:05:23

标签: android alertdialog 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();

但现在我收到以下错误 -

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

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

1 个答案:

答案 0 :(得分:3)

我建议改为创建一个对话框片段。此时,您可以按照您想要的方式处理片段中的倒数计时器。

以下是对话框片段的示例:

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");

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

<?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>