嗨,大家好我还在为我的capstone项目工作我正在为一个测验申请工作,我需要放一个计时器,当计时器为0时将结束测验 我需要一个代码,当活动开始时,计时器也会启动,当它结束时,它转到结果活动我已经有一个我的测验活动的代码,其中包含答案和问题,这里是代码,请帮助我在哪里放什么实现倒数计时器你的帮助将非常感激
package org.intercode.lifeatceu;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Random;
public class levelone extends AppCompatActivity {
TextView tv1, tvCred, tvQuestion, tvTime;
Button btNext;
RadioButton rb1, rb2;
RadioGroup rg;
String questions [] = {"Ma. Cristina D. Padolina is CEU's President", "Carlito B. Olaer is the V.P of CEU", "CEU's VISION is to sting every enemy", "One of CEU's Mission is to promote creative and scholarly academic"};
String answer [] = {"True", "False", "False", "True"};
HashSet numbers = new HashSet();
int flag = 0;
Random rnd = new Random();
int flag2 = 0 ;
int score = 0;
int correct = 0;
int wrong = 0;
int coins = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_levelone);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent inquiz = getIntent();
final int credit = inquiz.getIntExtra("passedCredits", 0);
String TotalCoins = String.valueOf(credit);
tv1 = (TextView) findViewById(R.id.tv1);
tvCred = (TextView) findViewById(R.id.tvCred);
tvQuestion = (TextView) findViewById(R.id.tvQuestion);
tvTime = (TextView) findViewById(R.id.tvTime);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rg = (RadioGroup) findViewById(R.id.rg);
btNext = (Button) findViewById(R.id.btNext);
flag2 = rnd.nextInt(4);
numbers.add(flag2);
tvQuestion.setText(questions[flag2]);
tvCred.setText(TotalCoins);
btNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(answer[flag2]))
{
while (numbers.contains(flag2))
{
flag2 = rnd.nextInt(4);
}
correct++;
coins++;
}
else
{
wrong++;
}
flag++;
flag2 = rnd.nextInt(4);
if (flag < questions.length)
{
tvQuestion.setText(questions[flag2]);
}
else
{
score = correct;
Intent in = new Intent(getApplicationContext(), Results.class);
in.putExtra("passedCredits", credit);
in.putExtra("passedWrong", wrong);
in.putExtra("passedCorrect", correct);
in.putExtra("passedCoins", coins);
startActivity(in);
}
}
});
}
}
我希望有一种方法可以将计时器放在这些代码中:P
答案 0 :(得分:0)
import java.util.Timer;
你应该在你的onResume()中加入这样的东西:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Intent intentToResultActivity = new Intent(context, ResultActivity.class);
context.startActivity(intentToResultActivity);
}
}, 10000);`
其中&#34; context&#34;是当前活动的结果,ResultActivity.class是计时器用完后要显示的活动的名称。
答案 1 :(得分:0)
行。这是倒数计时器的代码。您应该检查它并根据自己的需要进行整合:
你有两个活动 - 倒计时的Activity1和计时器用完后的结果活动的Activity2。
Activity1.java:
package com.example.emily.countdowntimer;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class Activity1 extends AppCompatActivity {
Context context = this;
Timer timer;
int countdownValue = 10;
TextView countdownTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
//this view will hold the countdown value
countdownTextView = (TextView)findViewById(R.id.countdown);
//we create a new timer and then schedule a task which will execute every one second
//(it will change the value of the text view)
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
countdownValue--;
countdownTextView.setText("" + countdownValue);
//when the value of the countdown reaches 0, we start the result activity:
if (countdownValue == 0) {
Intent intentToResultActivity = new Intent(context, Activity2.class);
context.startActivity(intentToResultActivity);
timer.cancel();
}
}
});
}
},0, 1000);
}
}
activity_1.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Activity1">
<TextView android:text="Time Left:" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:textSize="28sp"/>
<TextView android:text="10" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id = "@+id/countdown"
android:layout_centerInParent="true"
android:padding="5dp"
android:textColor="#ff111d"
android:textSize="58sp"/>
</RelativeLayout>
Activity2.java:
package com.example.emily.countdowntimer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
activity_2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#406447">
<TextView android:text="Result Activity" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_centerInParent="true"
android:textColor="#ffffff"
android:textSize="28sp"/>
</RelativeLayout>
的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emily.countdowntimer" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="Result Activity" >
</activity>
</application>
</manifest>
这就是您需要的所有代码。我建议您将它放在Android Studio中,看看它是如何工作的。使用它时,理解代码要容易得多。
答案 2 :(得分:0)
请尝试以下倒数计时器代码:
long durationSec = (duration * 60L);
final long durationInMilliSec = TimeUnit.SECONDS.toMillis(durationSec);
CountDownTimer countDownTimer= new CountDownTimer(durationInMilliSec, 1000) {
public void onTick(long millisUntilFinished) {
tvQuestionAnswerTimer.setText("" + String.format("%02d : %02d : %02d ",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
}
public void onFinish() {
Toast.makeText(this,Time's up!, Toast.LENGTH_SHORT).show();
}
}.start();
在此代码中
duration
是长输入分钟。
durationSec
是长型秒。
durationInMilliSec
是长型毫秒。
然后毫秒将倒数计时器转换为小时,分钟和秒。
希望它对您有用。