- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 bool 值暂停 CountDownTimer,当我按下暂停按钮时,tvTimer 停止更新时间,但倒计时计时器不断计数,然后当我按下恢复按钮时,tvTimer 更新倒计时计时器的状态,就像它真正打开的地方一样例如倒数计时器开始说 30 29 28 然后暂停 tvTimer 在 28 停止但实际上倒数计时器仍在工作然后让说倒数计时器现在是 25 但 textview 仍然是 28 对吗?然后我按 resume 显示计时器的 textview 将其更新为 25,其中倒数计时器是我想我只是缺少停止倒数计时器的代码....我真的不知道它是什么 T_T 你们能帮我吗
package org.intercode.lifeatceu;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.MediaPlayer;
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.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.HashSet;
import java.util.Random;
public class levelone extends AppCompatActivity {
TextView tv1, tvCred, tvQuestion, tvTimer;
Button btNext, btPause ,btResume;
RadioButton rb1, rb2;
RadioGroup rg;
LinearLayout ll;
CountDownTimer timer;
String questions[] =
{
"Dr. Ma Cristina D. Padolina is the President and Chief Academic Officer",
"Mr. Irineo C. Espino is the Vice President for Academic Affairs",
"Dr. Teresa R. Perez is the Vice President for Administration and Information Officer",
"Ms. Corazon M. Tiongco is thr Asst. Treasurer and Procurement Dept. Head",
"Dr. Rhoda C. Aguilar is the University Registrar",
"Dr. Olivia M. Limuaca is the Vice President of Malolos Campus",
"Dr. Ma. Flordeliza L. Anastacio is the Vice President of Makati Campus",
"Dr. Maria Clara Perlita Erna V. Yabut is the Vice President for Research & Evaluation",
"Dr. Carlito B. Olaer is the Vice President for Student Affairs",
"Ms. Bernardita T. Traje is the Asst. Comptroller",
"Mr. Cesar F. Tan is the Treasurer, Asst. Corporate Secretary & Asst. Compliance Officer",
"Justice Josue N. Bellosillo is the Dean of School of Law and Jurisprudence at CEU MAKATI",
"Mr. Marcial L. Anacio is the Program head of Computer Science and Information Tech. Department at CEU MAKATI",
"Atty. Rita Linda V. Jimeno is the Associate Dean of School of Law and Jurisprudence at CEU MAKATI",
"Dr. Teresita I. Barcelona is the dean of Nursing at CEU MANILA",
"Dr. Cecilia C. Uncad is the Dean of Optometry at CEU MANILA",
"Dr. Jessica L. Flor-Torre is the Dean of Nutrition and Hospitality Management at CEU MANILA",
"Dean Melito S. Salazar, Jr. is the dean of Accountancy and Management at CEU MANILA",
"Dr. Charito M. Bermido is the dean of Medical Technology at CEU MANILA",
"Dr. Teresita G. Carey is the Dean of Science and Technology at CEU MANILA",
"Dr. Julieta Z. Dungca is the Dean of Educational, Liberal, Arts, Music, Social Wok at CEU MANILA",
"Dr. Pearly P. Lim is the Dean of Pharmacy at CEU MANILA",
"Dr. Cecilla D, Santiago is the Dean of Dentistry at CEU MANILA",
"Dr. Milagros L. Borabo is the Director of Professional and Continuing Educational(PACE)",
"Dr. Ma. Flordeliza L. Anastacio is the Vice President and Dean of Studies at CEU MALOLOS",
"Dr. Nilo V. Francisco is the Dean of Management and Technology at CEU MALOLOS",
"Dr. Elizabeth C. Roces is the Dean of Education, Liberal Arts and Science at CEU MALOLOS",
"Ms. Veronica F. Balintona is the Asst. Dean of Nutrition and Hospitality management at CEU MANILA",
"Mrs. Elvira L. Urgel is the Asst. Dean of Nursing at CEU MANILA",
"Dr. Aileen C. Patron is the Asst. Dean of Medical Technology at CEU MANILA",
"Dr. Juliana M. Laraya is the Asst. Dean of Educational, Liberal Arts, Music, Social Work at CEU MANILA",
"Mrs. Emma C. Castro is the Administrative Officer (General Services) at CEU MAKITA",
"Mrs Julie L. Ibe is the Academic Coordinator of Cash Division at CEU MALOLOS",
"Mrs. Ma. Armela Pamiloza is the Academic Coordinator Level III Nursing at CEU MALOLOS",
"Mrs. Rosa M. Quizon is the Academic Coordinator of Level II Nursing at CEU MALOLOS",
"Mrs. Luningning O. Marcelino is the Academic Coordinator Library Section at CEU MALOLOS",
"Mrs. Jemelyn O. Garcia is the Academic Coordinator of Community Outreach Section at CEU MALOLOS",
"Mrs. Shella Marie S. Ignacio is the Head of Tourism Program at CEU MALOLOS",
"Mrs. Marietta C. Alvarez is the Head of Liberal Arts Program/ Mass Communication Laboratory at CEU MALOLOS",
"Mrs. Amelia T. Valencia is the head of Marketing Placement at CEU MALOLOS",
"Mrs. Elvira G. Borlongan is the head of Registrar at CEU MALOLOS",
"Mrs. Ada Ruth A. Aguasian is the Academic Coordinator of Tourism Management at CEU MAKATI",
"Dr. Maria Wanda I. Martinez is the Academic Coordinator of Community Nursing at CEU MAKATI",
"Mr. Rommel Rufuz A. Lopez is the Academic Coordinator of Clinical Dentistry at CEU MAKATI",
"Dr. Amelita M. Borlongan is the Academic Coordinator of Research at CEU MAKATI",
"Ms. Annabelle is the Academic Coordinator of Physical Education at CEU MAKATI",
"Mrs. Maria Victoria S. Coloma is the Academic Coordinator of Clinical Nursing at CEU MAKATI",
"Dr. Maria Corazon L. Andoy is the Assistant to the Vice President at CEU MAKATI",
"Mrs. Eleanor C. Reyes is the Coordinator of Science Laboratory at CEU MALOLOS",
"Dr. Flora L. Camay is the Program Head of Alumni Relation and Education at CEU MALOLOS"
};
String answer[] =
{
"True",//1
"False",//2
"False",//3
"True",//4
"True",//5
"False",//6
"False",//7
"True",//8
"True",//9
"True",//10
"True",//11
"True",//12
"True",//13
"True",//14
"True",//15
"False",//16
"False",//17
"True",//18
"True",//19
"False",//20
"False",//21
"False",//22
"False",//23
"True",//24
"True",//25
"True",//26
"True",//27
"True",//28
"True",//29
"True",//30
"True",//31
"True",//32
"False",//33
"True",//34
"False",//35
"False",//36
"False",//37
"True",//38
"True",//39
"False",//40
"False",//41
"True",//42
"False",//43
"False",//44
"True",//45
"True",//46
"True",//47
"True",//48
"True",//49
"True",//50
};
HashSet numbers = new HashSet();
int flag = 0;
Random rnd = new Random();
int flag2 = 0;
int correct = 0;
int wrong = 0;
int coins = 0;
int time = 0;
int credit = 0;
private boolean isPaused = false;
private long timeremaining = 0;
String Executives = "lock";
String Liberators = "lock";
String Travellers = "lock";
String Specialist = "lock";
MediaPlayer mpTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_levelone);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
isPaused = false;
SharedPreferences loadGame = getSharedPreferences("Game", MODE_PRIVATE);
credit = loadGame.getInt("savedPlayerCredit", 0);
time = loadGame.getInt("savedPlayerTime", 0);
SharedPreferences loadGame2 = getSharedPreferences("Theme", MODE_PRIVATE);
Executives = loadGame2.getString("Executives", "lock");
Liberators = loadGame2.getString("Liberators", "lock");
Specialist = loadGame2.getString("Specialist", "lock");
Travellers = loadGame2.getString("Travellers", "lock");
final MediaPlayer mpWrong = MediaPlayer.create(this, R.raw.wrong);
final MediaPlayer mpCorrect = MediaPlayer.create(this, R.raw.correct);
final MediaPlayer mpNormal = MediaPlayer.create(this, R.raw.normal);
mpTimer = MediaPlayer.create(this, R.raw.timer);
tv1 = (TextView) findViewById(R.id.tv1);
tvCred = (TextView) findViewById(R.id.tvCred);
tvQuestion = (TextView) findViewById(R.id.tvQuestion);
tvTimer = (TextView) findViewById(R.id.tvTimer);
rb1 = (RadioButton) findViewById(R.id.rb1);
rb2 = (RadioButton) findViewById(R.id.rb2);
rg = (RadioGroup) findViewById(R.id.rg);
btNext = (Button) findViewById(R.id.btNext);
btPause = (Button) findViewById(R.id.btPause);
btResume = (Button) findViewById(R.id.btResume);
btNext.setBackgroundResource(R.drawable.next);
ll = (LinearLayout) findViewById(R.id.ll);
if (Executives.equals("active")) {
time = time + 5000;
} else if (Liberators.equals("active")) {
coins = coins + 20;
} else if (Travellers.equals("active")) {
time = time + 5000;
} else if (Specialist.equals("active")) {
coins = coins + 20;
} else {
Toast.makeText(this, "Go!", Toast.LENGTH_SHORT).show();
}
flag2 = rnd.nextInt(50);
numbers.add(flag2);
tvQuestion.setText(questions[flag2]);
tvCred.setText(String.valueOf(credit));
mpTimer.start();
timer = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
if(isPaused)
{
timer.cancel();
}
else
{
tvTimer.setText("" + millisUntilFinished / 1000);
timeremaining=millisUntilFinished;
}
}
@Override
public void onFinish() {
Intent inResult = new Intent(getApplicationContext(), Results.class);
SharedPreferences saveGame = getSharedPreferences("Game", MODE_PRIVATE);
SharedPreferences.Editor editor = saveGame.edit();
editor.putInt("savedPlayerCredit", credit);
editor.putInt("savedPlayerCoins", coins);
editor.putInt("savedPlayerCorrect", correct);
editor.putInt("savedPlayerWrong", wrong);
editor.apply();
startActivity(inResult);
mpTimer.stop();
}
}.start();
btNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btNext.setBackgroundResource(R.drawable.nextonclick);
if (rg.getCheckedRadioButtonId() == -1) {
Toast.makeText(getApplicationContext(), "Choose an Answer", Toast.LENGTH_LONG).show();
} else {
RadioButton uans = (RadioButton) findViewById(rg.getCheckedRadioButtonId());
String ansText = uans.getText().toString();
if (ansText.equalsIgnoreCase(answer[flag2])) {
while (numbers.contains(flag2)) {
flag2 = rnd.nextInt(50);
}
correct++;
coins++;
mpCorrect.start();
Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
} else {
wrong++;
mpWrong.start();
Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
}
flag++;
flag2 = rnd.nextInt(50);
if (flag < questions.length) {
tvQuestion.setText(questions[flag2]);
rb1.setChecked(false);
rb2.setChecked(false);
rg.clearCheck();
btNext.setBackgroundResource(R.drawable.next);
} else {
Intent inResult = new Intent(getApplicationContext(), Results.class);
SharedPreferences saveGame = getSharedPreferences("Game", MODE_PRIVATE);
SharedPreferences.Editor editor = saveGame.edit();
editor.putInt("savedPlayerCredit", credit);
editor.putInt("savedPlayerCoins", coins + 100);
editor.putInt("savedPlayerCorrect", correct);
editor.putInt("savedPlayerWrong", wrong);
editor.apply();
timer.cancel();
startActivity(inResult);
mpTimer.stop();
}
}
mpNormal.start();
}
});
btPause.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
isPaused = true;
mpTimer.stop();
}
});
btResume.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
isPaused = false;
mpTimer.start();
}
});
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Quit");
builder.setMessage("Are you sure you want to Quit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
timer.onFinish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
protected void onStop() {
super.onStop();
timer.cancel();
mpTimer.stop();
}
@Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
}
}
最佳答案
这就是我要做的。我会在 onTick 处理程序中添加一些内容,以保存类中计时器的进度(类似于 timeLeft = millisUntilFinished/1000;
)
当你想暂停计时器时,执行timer.cancel()
。当你想继续运行计时器时,创建一个新的计时器,保留剩余的毫秒数。
这样,您就无需担心计时器在应该暂停时是否仍在运行,以及将额外的值传递给 CountDownTimer。
编辑:
您可以创建一个私有(private)方法来在需要时创建一个新的 CountDownTimer:
private CountDownTimer createTimer(int time){
return new CountDownTimer(time*1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvTimer.setText("" + millisUntilFinished / 1000);
timeLeft = millisUntilFinished / 1000;
}
@Override
public void onFinish() {
//Do what you want, cancel the timer...
}
}.start();
添加一个全局变量
private CountDownTimer timer;
当你需要创建一个 CountDownTimer 时:
timer = createCountDownTimer(yourTime);
如果你想暂停,请执行 timer.cancel();
关于Android Studio 如何操作 CountDownTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35865588/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!