java - 如何停止“倒数计时器”并在 Android Studio 的对话框中显示结果?
问题描述
因为我是 android 开发的新手,所以我需要一些帮助。我正在开发测验应用程序,所以我有分数倒计时。我无法弄清楚如何停止计时器并在自定义对话框中显示。因此,如果每个问题都将完成,它应该停止计时器并且它必须在最后一次对话。之后我将它存储到数据库中以按时间和分数对其排名进行排序。
这里是倒计时方法。
private void countD(){
countDownTimer = new CountDownTimer(timeleftinmilliseconds, 1000) {
@Override
public void onTick(long l) {
timeleftinmilliseconds = l;
int minutes = (int) (timeleftinmilliseconds/1000)/60;
int second = (int) (timeleftinmilliseconds/1000)%60;
String timeletfFormated = String.format(Locale.getDefault(), "%02d:%02d", minutes, second);
timetext.setText(timeletfFormated);
}
@Override
public void onFinish() {
timetext.setText("00:00");
}
}.start();
自定义对话框的 xml
<TextView
android:id="@+id/dialog_time_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Time is: "
android:layout_below="@id/dialog_message"
android:textSize="18dp"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/dialog_time_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/dialog_message"
android:paddingLeft="10dp"
android:text="00:00"
android:textSize="18dp"
android:layout_marginTop="20dp"
android:layout_below="@+id/dialog_score"
/>
<EditText
android:id="@+id/edittext_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Please enter your name"
android:layout_below="@+id/dialog_time_message"
android:layout_marginTop="10dp"
/>
这也是为了弹出对话框
private void gameOver(){
final AlertDialog.Builder builder = new AlertDialog.Builder(StartQuiz.this);
View view = getLayoutInflater().inflate(R.layout.layout_dialog, null);
//Declare and casting for score and nameField in customized dialog
final EditText editTextName = (EditText)view.findViewById(R.id.edittext_name);
final TextView dialog_score_show = (TextView)view.findViewById(R.id.dialog_score);
//Declare and casting for time in customized dialog
final TextView time_view_in_cDialog = (TextView)view.findViewById(R.id.dialog_time_score);
dialog_score_show.setText(""+Score);
//dialog_time_show.setText(""+countDownTimer);//check this(it should show the final time stopped which also has to be shown in resulst)
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//TODO
}
});
builder.setView(view);
builder.setCancelable(false);
final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {//clicked submit button
@Override
public void onClick(View view) {
if(!editTextName.getText().toString().isEmpty()) {//checking the empty field(not empty)
boolean isInserted = myDb.insertData(editTextName.getText().toString(),
dialog_score_show.getText().toString());//inserting data to database from field
//while inserting data it should sort by score and time and delete the last 11th
if(isInserted == true){
Toast.makeText(StartQuiz.this, "Quiz Completed", Toast.LENGTH_SHORT).show();
getData();/*gets all the data name and the result*/ }
else
Toast.makeText(StartQuiz.this, "Quiz Incompleted", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} else//(empty field)
Toast.makeText(StartQuiz.this, "Please enter your name", Toast.LENGTH_SHORT).show();
}
}//end of submit button
);
}//end of game over
我正在寻找任何帮助来停止时间并显示在对话框中。
之后,我将从对话框中将其保存到数据库中。
解决方案
您可以使用 cancel() 方法:
private CountDownTimer countDownTimer;
public void startCount(){
countDownTimer = new CountDownTimer(SECONDS_TO_DISMISS, 1000) {
@Override
public void onTick(long time) {
strong text
**strong text**
}
@Override
public void onFinish() {
}
}.start();
}
public void stopCount(){
countDownTimer.cancel();
}
推荐阅读
- apache-camel - Apache Camel Ant 包含模式未按预期工作
- laravel - 将删除和补丁放在laravel的同一页面中
- java - 搜索查看片段恢复后如何再次搜索
- javascript - 在不同的屏幕尺寸上重新排序 DOM 元素
- node.js - 发生异常:TypeError TypeError:connection.queryRaw 不是函数
- python - 无法在 Mac (Python) 上安装 matplotlib
- cakephp - 将列中的值与数字进行比较时出现问题
- javascript - 如何从 JSON 响应动态填充 div
- excel - Excel - 自上次 B 列为“是”以来,使用 C 列对 A 列中的所有值求和
- c# - 在 C# 中使用 Selenium 将 PDF 内容下载到字符串中