gpt4 book ai didi

java - 如何停止 "Countdown timer "并在Android studio的对话框中显示结果?

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:48 24 4
gpt4 key购买 nike

由于我是 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

我正在寻找任何类型的帮助来停止时间并显示在对话框中。

之后,我将从对话框中将其保存到数据库中。

最佳答案

您可以在其回调方法之一中调用 this.cancel() 或简单地取消()

你还检查How to stop CountDownTimer in android

关于java - 如何停止 "Countdown timer "并在Android studio的对话框中显示结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51746666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com