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

时间:2016-12-15 05:20:17

标签: android

我需要有关计时器的帮助。我需要一个计时器来激活每个问题,当它完成时它应该禁用无线电组。此代码中的问题是,当计时器完成时,它将禁用所有问题中的所有无线电组。

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" />

2 个答案:

答案 0 :(得分:1)

此代码现在适用于我。

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

答案 1 :(得分:0)

private CountDownTimer timer;
onCrate()....
  ....

  timer = new CountDownTimer(20000, 1000) {....};
  timer.start();
  ....

  onClick(){....
    rb4.setText(opt[(flag * 4) + 3]);
    timer.start();
  };