Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio如何操作倒计时_Android_Performance_Android Layout_Android Fragments_Android Intent - Fatal编程技术网

Android Studio如何操作倒计时

Android Studio如何操作倒计时,android,performance,android-layout,android-fragments,android-intent,Android,Performance,Android Layout,Android Fragments,Android Intent,我尝试使用布尔值暂停倒计时。当我按下暂停按钮时,tvTimer停止更新时间,但倒计时计时器持续计数,然后当我按下恢复按钮时,tvTimer更新倒计时计时器的状态,例如倒计时计时器开始,例如30 29 28,然后暂停tvTimer在28停止,但实际上倒计时仍在工作,那么让我们假设倒计时现在在25,但textview仍在28,对吗?然后我按恢复显示计时器的文本视图,将其更新为25,其中是倒计时。我想我只是缺少了一个停止倒计时的代码。。。。我真的不知道是什么,你们能帮我吗 package org.in

我尝试使用布尔值暂停倒计时。当我按下暂停按钮时,tvTimer停止更新时间,但倒计时计时器持续计数,然后当我按下恢复按钮时,tvTimer更新倒计时计时器的状态,例如倒计时计时器开始,例如30 29 28,然后暂停tvTimer在28停止,但实际上倒计时仍在工作,那么让我们假设倒计时现在在25,但textview仍在28,对吗?然后我按恢复显示计时器的文本视图,将其更新为25,其中是倒计时。我想我只是缺少了一个停止倒计时的代码。。。。我真的不知道是什么,你们能帮我吗

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

}
  }
package org.intercode.lifeatceu;
导入android.app.AlertDialog;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.graphics.Color;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.provider.MediaStore;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.view;
导入android.widget.Button;
导入android.widget.LinearLayout;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.w3c.dom.Text;
导入java.util.HashSet;
导入java.util.Random;
公共类levelone扩展了AppCompatActivity{
文本视图tv1、tvCred、tvQuestion、tvTimer;
按钮btNext、btPause、btResume;
单选按钮rb1、rb2;
放射组rg;
线性布局;
倒计时;
字符串问题[]=
{
“Ma Cristina D.Padolina博士是校长兼首席学术官”,
“Irineo C.Espino先生是学术事务副总裁”,
“Teresa R.Perez博士是主管行政和信息官的副总裁”,
“Corazon M.Yongco女士是thr助理司库和采购部主管”,
“Rhoda C.Aguilar博士是大学注册官”,
“Olivia M.Limuaca博士是Malolos校区的副校长”,
“马·弗洛德里扎·阿纳斯塔西奥博士是马卡蒂校区的副校长”,
“Maria Clara Perlita Erna V.Yabut博士是研究与评估副总裁”,
“Carlito B.Olaer博士是学生事务副总裁”,
“Bernardita T.Traje女士是助理主计长”,
“Cesar F.Tan先生是财务主管、助理公司秘书兼助理合规官”,
“Josue N.Bellosillo法官是CEU MAKATI法学院院长”,
“Marcial L.Anacio先生是CEU MAKATI计算机科学和信息技术部的项目负责人”,
“Atty.Rita Linda V.Jimeno是CEU MAKATI法学院副院长”,
“Teresita I.Barcelona博士是CEU马尼拉分校的护理院长”,
“Cecilia C.Uncad博士是CEU马尼拉分校的验光院长”,
“Jessica L.Flor Torre博士是马尼拉CEU营养与酒店管理学院院长”,
“院长Melito S.Salazar,Jr.是马尼拉CEU会计和管理学院院长”,
“Charito M.Bermido博士是CEU马尼拉医学技术学院院长”,
“Teresita G.Carey博士是CEU马尼拉分校科学与技术学院院长”,
“Julieta Z.Dungca博士是马尼拉CEU教育、文科、艺术、音乐和社会工作学院院长”,
“Pearly P.Lim博士是CEU马尼拉分校的药学主任”,
“Cecilla D,Santiago博士是CEU马尼拉分校牙科院长”,
“Milagros L.Borabo博士是专业和继续教育部(PACE)主任”,
“Ma.Flordeliza L.Anastacio博士是CEU Malololos的副校长兼研究主任”,
“Nilo V.Francisco博士是CEU Malololos管理与技术学院院长”,
“Elizabeth C.Roces博士是CEU Malololos的教育、文理学院院长”,
“Veronica F.Balintona女士是CEU马尼拉分校营养和酒店管理副院长”,
“Elvira L.Urgel女士是CEU马尼拉分校护理副院长”,
“Aileen C.Patron博士是CEU马尼拉医学技术副院长”,
“Juliana M.Laraya博士是马尼拉CEU教育、文科、音乐、社会工作副院长”,
“Emma C.Castro女士是CEU MAKITA的行政官员(一般事务),
“Julie L.Ibe女士是CEU Malololos现金部门的学术协调员”,
“Ma.Armela Pamiloza女士是CEU Malololos的三级护理学术协调员”,
“Rosa M.Quizon女士是CEU Malololos的二级护理学术协调员”,
“Luningning O.Marcelino女士是CEU Malololos的学术协调员图书馆部门”,
“Jemelyn O.Garcia女士是CEU Malololos社区外展科的学术协调员”,
“Shella Marie S.Ignacio女士是CEU Malololos旅游项目负责人”,
“Marietta C.Alvarez女士是CEU Malololos文科项目/大众传播实验室的负责人”,
“Amelia T.Valencia女士是CEU Malololos的营销安置负责人”,
“Elvira G.Borlongan女士是CEU Malololos的注册主管”,
“Ada Ruth A.Aguasian女士是CEU MAKATI的旅游管理学术协调员”,
“马里博士
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;
timer = createCountDownTimer(yourTime);