gpt4 book ai didi

android - 我的测验应用程序中的计时器

转载 作者:行者123 更新时间:2023-11-30 00:50:15 24 4
gpt4 key购买 nike

我需要计时器方面的帮助。我需要一个计时器来激活每个问题,当它完成时它应该禁用 radio 组。此代码中的问题是,当计时器结束时,它会禁用所有问题中的所有 radio 组。

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class QuizActivity extends AppCompatActivity {
TextView tv,mTextField;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2,rb3,rb4;
String questions[]={"First Android Phone?","Name of Android Version 4.4?","Android is which kind of software?"};
String ans[]={"Motorola Droid","Kitkat","Operating-System"};
String opt[]={"HTC-G1","HTC-One","Motorola Droid","Samsung S4","Jelly Bean","Kitkat","Froyo","Lollipop",
"Operating-System","Antivirus","Application","Tools"};
int flag=0;
public static int marks,correct,wrong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
tv = (TextView)findViewById(R.id.tvque);
mTextField = (TextView)findViewById(R.id.tv2);
btnNext = (Button)findViewById(R.id.btnNext);
rg = (RadioGroup)findViewById(R.id.radioGroup);
rb1 = (RadioButton)findViewById(R.id.radio0);
rb2 = (RadioButton)findViewById(R.id.radio1);
rb3 = (RadioButton)findViewById(R.id.radio2);
rb4 = (RadioButton)findViewById(R.id.radio3);
tv.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
rb4.setText(opt[3]);
new CountDownTimer(20000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}

@Override
public void onFinish() {
for (int i=0; i< rg.getChildCount();i++){
rg.getChildAt(i).setEnabled(false);
}
//mTextField.setText("done!");
}
}.start();
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<questions.length)
{
tv.setText(questions[flag]);
rb1.setText(opt[flag*4]);
rb2.setText(opt[(flag*4)+1]);
rb3.setText(opt[(flag*4)+2]);
rb4.setText(opt[(flag*4)+3]);
}
else
{
marks=correct;
Intent in = new Intent(getApplicationContext(),ResultActivity.class);
startActivity(in);
}
}
});
}
}

UI of the application

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id = "@+id/container"
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/textAppearanceLarge"
android:text="Question"
android:id="@+id/tvque" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup"
android:layout_below="@+id/tvque"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton"/>
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"/>
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"/>
</RadioGroup>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next Question"
android:id="@+id/btnNext"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnNext"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:id="@+id/tv2" />

最佳答案

这段代码现在对我有用。

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class QuizActivity extends AppCompatActivity {
TextView tv,mTextField;
Button btnNext;
RadioGroup rg;
RadioButton rb1,rb2,rb3,rb4;
String questions[]={"First Android Phone?","Name of Android Version 4.4?","Android is which kind of software?"};
String ans[]={"Motorola Droid","Kitkat","Operating-System"};
String opt[]={"HTC-G1","HTC-One","Motorola Droid","Samsung S4","Jelly Bean","Kitkat","Froyo","Lollipop",
"Operating-System","Antivirus","Application","Tools"};
int flag=0;
public static int marks,correct,wrong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
tv = (TextView)findViewById(R.id.tvque);
mTextField = (TextView)findViewById(R.id.tv2);
btnNext = (Button)findViewById(R.id.btnNext);
rg = (RadioGroup)findViewById(R.id.radioGroup);
rb1 = (RadioButton)findViewById(R.id.radio0);
rb2 = (RadioButton)findViewById(R.id.radio1);
rb3 = (RadioButton)findViewById(R.id.radio2);
rb4 = (RadioButton)findViewById(R.id.radio3);
tv.setText(questions[flag]);
rb1.setText(opt[0]);
rb2.setText(opt[1]);
rb3.setText(opt[2]);
rb4.setText(opt[3]);
final CountDownTimer Counter1 = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}

@Override
public void onFinish() {
for (int i=0; i< rg.getChildCount();i++){
rg.getChildAt(i).setEnabled(false);
}
}
}.start();
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<questions.length)
{
tv.setText(questions[flag]);
rb1.setText(opt[flag*4]);
rb2.setText(opt[(flag*4)+1]);
rb3.setText(opt[(flag*4)+2]);
rb4.setText(opt[(flag*4)+3]);
for (int i=0; i< rg.getChildCount();i++){
rg.getChildAt(i).setEnabled(true);
}
Counter1.start();
}
else
{
marks=correct;
Intent in = new Intent(getApplicationContext(),ResultActivity.class);
startActivity(in);
}
}
});
}
}

关于android - 我的测验应用程序中的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41156941/

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