จะสร้างตัวจับเวลาที่เริ่มโดยอัตโนมัติได้อย่างไร?

ฉันกำลังทำแบบทดสอบ Android ในแอปของฉัน ฉันมีคลาส QuestionActivity ในเมธอด setQuestions() ใดที่กำหนดข้อความจากฐานข้อมูลในปุ่ม textview และคะแนนถูกตั้งค่าเป็นศูนย์ในครั้งแรก ในข้อความถัดไปของคำถามถัดไปจะได้รับมอบหมายและคะแนนจะได้รับการอัปเดตด้วยว่าคำถามผิดหรือถูก ฉันอยากมีเครื่องจับเวลาอัตโนมัติซึ่งจะเริ่มเมื่อเราเข้าร่วมกิจกรรมนี้ และในรอบต่อไปก็เริ่มใหม่อีกครั้งในช่วงหนึ่งและลดลงเรื่อยๆ ฉันรู้เรื่องนี้ฉันต้องสร้าง textview ในคำถาม xml ที่เกี่ยวข้องกับการโทรนี้ แต่ฉันไม่รู้ว่าจะเขียนโค้ดที่ไหนและอย่างไรในคลาสนี้ ตั้งเวลาได้ 30 วินาที ฉันกำลังโพสต์รหัสของชั้นเรียนนี้ ได้โปรดมีคนช่วยฉันด้วย

public class QuestionActivity extends Activity implements OnClickListener{

    private Question currentQ;
    private GamePlay currentGame;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question);
                /**
         * Configure current game and get question
         */
        currentGame = ((CYKApplication)getApplication()).getCurrentGame();
        currentQ = currentGame.getNextQuestion();
        Button nextBtn1 = (Button) findViewById(R.id.answer1);
        nextBtn1.setOnClickListener(this);
        Button nextBtn2 = (Button) findViewById(R.id.answer2);
        nextBtn2.setOnClickListener(this);
        Button nextBtn3 = (Button) findViewById(R.id.answer3);
        nextBtn3.setOnClickListener(this);
        Button nextBtn4 = (Button) findViewById(R.id.answer4);
        nextBtn4.setOnClickListener(this);
        Button nextBtn5 = (Button) findViewById(R.id.answer5);
        nextBtn5.setOnClickListener(this);


        /**
         * Update the question and answer options..
         */
        setQuestions();

    }


    /**
     * Method to set the text for the question and answers from the current games
     * current question
     */
    private void setQuestions() {
        //set the question text from current question
        String question = Utility.capitalise(currentQ.getQuestion());
        TextView qText = (TextView) findViewById(R.id.question);
        qText.setText(question);

        //set the available options
        List<String> answers = currentQ.getQuestionOptions();
        TextView option1 = (TextView) findViewById(R.id.answer1);
        option1.setText(Utility.capitalise(answers.get(0)));

        TextView option2 = (TextView) findViewById(R.id.answer2);
        option2.setText(Utility.capitalise(answers.get(1)));

        TextView option3 = (TextView) findViewById(R.id.answer3);
        option3.setText(Utility.capitalise(answers.get(2)));

        TextView option4 = (TextView) findViewById(R.id.answer4);
        option4.setText(Utility.capitalise(answers.get(3)));

        int score = currentGame.getScore();
        String scr = String.valueOf(score);
        TextView score1 = (TextView) findViewById(R.id.score);
        score1.setText(scr);
        }


    @Override
    public void onClick(View arg0) {
        //Log.d("Questions", "Moving to next question");
        if(arg0.getId()==R.id.answer5)
        {
        new AlertDialog.Builder(this)
        .setMessage("Are you sure?")
        .setCancelable(true)
        .setPositiveButton("Yes",
         new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
         int id) {
                finish();
                 }
             }).setNegativeButton("No", null).show();

                }

        else
        {
            if(!checkAnswer(arg0)) return;  

        /**
         * check if end of game
         */
        if (currentGame.isGameOver()){
            //Log.d("Questions", "End of game! lets add up the scores..");
            //Log.d("Questions", "Questions Correct: " + currentGame.getRight());
            //Log.d("Questions", "Questions Wrong: " + currentGame.getWrong());
            Intent i = new Intent(this, EndgameActivity.class);
            startActivity(i);
            finish();
        }
        else{
            Intent i = new Intent(this, QuestionActivity.class);
            startActivity(i);
            finish();
          }
        }
      }



    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        switch (keyCode)
        {
        case KeyEvent.KEYCODE_BACK :
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


    /**
     * Check if a checkbox has been selected, and if it
     * has then check if its correct and update gamescore
     */
    private boolean checkAnswer(View v) {

        Button b=(Button) v;
        String answer = b.getText().toString();

            //Log.d("Questions", "Valid Checkbox selection made - check if correct");
            if (currentQ.getAnswer().equalsIgnoreCase(answer))
            {
                b.setBackgroundResource(R.drawable.answercolor);
                //Log.d("Questions", "Correct Answer!");
                currentGame.incrementScore();
            }
            else{
                b.setBackgroundResource(R.drawable.answercolorr);
                //Log.d("Questions", "Incorrect Answer!");
                currentGame.decrementScore();
            }
            return true;
        }

}

person John R    schedule 05.09.2013    source แหล่งที่มา


คำตอบ (2)


ฉันอยากมีเครื่องจับเวลาอัตโนมัติซึ่งจะเริ่มเมื่อเราเข้าร่วมกิจกรรมนี้ และในรอบต่อไปก็เริ่มใหม่อีกครั้งในช่วงหนึ่งและลดลงเรื่อยๆ ฉันรู้เรื่องนี้ฉันต้องสร้าง textview ในคำถาม xml ที่เกี่ยวข้องกับการโทรนี้ แต่ฉันไม่รู้ว่าจะเขียนโค้ดที่ไหนและอย่างไรในคลาสนี้ ตั้งเวลาได้ 30 วินาที

คุณสามารถบรรลุสิ่งนี้ได้โดยใช้ CountDownTimer

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

สำหรับข้อมูลเพิ่มเติม ดูที่นี่

person CRUSADER    schedule 05.09.2013
comment
ฉันไม่เข้าใจคุณ ฉันคิดว่ารหัสนี้ไม่เพียงพอที่จะจับเวลาเหมือนที่ฉันต้องการในเกมของฉัน คุณช่วยบอกวิธีแก้ไขปัญหาอื่นให้ฉันด้วยรหัสได้ไหม - person John R; 05.09.2013
comment
เหตุใดจึงไม่เพียงพอ? ตัวจับเวลาจะทำงานในกิจกรรม... คุณสงสัยว่าส่วนหนึ่งจะได้รับการรีเฟรช... ฉันแนะนำให้คุณเพราะฉันทำแอปตอบคำถามประเภทเดียวกัน.. - person CRUSADER; 05.09.2013
comment
ตกลง. แต่เมื่อฉันกำหนด textview ที่ฉันสร้างขึ้นในคำถาม xml แทนที่ mTextField.setText( ) มันแสดงข้อผิดพลาด - person John R; 05.09.2013
comment
ฉันแก้ไขข้อผิดพลาดแล้ว มันใช้งานได้ คุณแก้ไขปัญหาของฉันแล้ว พี่ชาย. ถ้าฉันต้องการความช่วยเหลืออื่นใด ฉันจะส่งข้อความถึงคุณเพราะคุณทำแบบทดสอบอยู่ - person John R; 05.09.2013
comment
@JohnR ดีใจที่ได้ช่วยเหลือคุณ .. BTW ฉันไม่ใช่คนเดียว .. คุณสามารถโพสต์คำถามใหม่บนเว็บไซต์นี้ได้ตลอดเวลามีคนเหมือนฉันหลายคนที่สามารถแนะนำคุณไปในทิศทางที่ถูกต้อง .. :) - person CRUSADER; 05.09.2013
comment
ฉันมีปัญหาหนึ่ง รหัสที่คุณแนะนำให้ฉันใช้ตัวจับเวลาทำงานได้อย่างถูกต้อง ฉันต้องการเมื่อตัวจับเวลาถอยหลังหมดลง คำถามถัดไปก็คือ สำหรับสิ่งนี้ ฉันเขียนบรรทัดเหล่านี้ด้วยวิธีการสิ้นสุดของตัวจับเวลา /*รหัส*/ เจตนา i; i.setClassName(com.pkgname,com.pkgname.classname); startActivityForResult (i,0); รหัสนี้ยังใช้งานได้ แต่หากฉันรวมรหัสนี้ ปุ่มออกของฉันจะทำงานไม่ถูกต้อง มันแสดงกล่องโต้ตอบและกดใช่ไม่ออก หลังจากคลิก 3-4 ครั้งก็จะออก - person John R; 06.09.2013
comment
@JohnR โพสต์คำถามใหม่บนเว็บไซต์นี้โดยกล่าวถึงสิ่งที่คุณลองอย่างชัดเจนและคุณติดอยู่ที่ใด ... - person CRUSADER; 06.09.2013
comment
stackoverflow.com/questions/18657036/ ลิงก์ของคำถามนั้น - person John R; 06.09.2013
comment
พี่ชาย. คุณเป็นเพียงคนเดียวที่สามารถแก้ไขปัญหานี้ของฉันได้ โปรดช่วยฉันด้วย stackoverflow.com/questions/18800528/ - person John R; 14.09.2013

สำหรับตัวจับเวลา มีคลาสตัวจับเวลาที่ฉันใช้ส่งข้อมูลทุกๆ 300 มิลลิวินาทีจากโทรศัพท์ของฉัน แต่คุณต้องมีตัวนับไม่ใช่ตัวจับเวลาที่คุณใช้ตัวจัดการสำหรับเธรดและจำนวนเต็มที่เริ่มต้นจากตัวนับ = 30 และในตัวจัดการคุณลดมันลงหนึ่งตัวนับ - และตัวจัดการจะอยู่ที่ 1 วินาที (1,000 มิลลิวินาที)

person cesztoszule    schedule 05.09.2013