在java中,如何在两个事件之间等待?
我正在创建一个琐事应用程序。我希望在用户单击答案后立即出现正确/错误动画,然后在3秒钟后,问题自动切换(淡入下一个问题)。我试着用Thread.sleep(3000)来做这个,但是整个程序都冻结了。这是我目前的代码:在java中,如何在两个事件之间等待?,java,android,multithreading,button,Java,Android,Multithreading,Button,我正在创建一个琐事应用程序。我希望在用户单击答案后立即出现正确/错误动画,然后在3秒钟后,问题自动切换(淡入下一个问题)。我试着用Thread.sleep(3000)来做这个,但是整个程序都冻结了。这是我目前的代码: binding.buttonTrue.setOnClickListener(view -> { checkAnswer(true); updateQuestion(); //these two calls
binding.buttonTrue.setOnClickListener(view -> {
checkAnswer(true);
updateQuestion();
//these two calls invoke right/wrong animation
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
changeTheQuestion();
//this invokes fade animation
});
如何在这两个动画之间添加一段时间?谢谢
完整代码:
包com.bawp.trivia
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import com.bawp.trivia.data.Repository;
import com.bawp.trivia.databinding.ActivityMainBinding;
import com.bawp.trivia.model.Question;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
public class MainActivity extends AppCompatActivity {
List<Question> questionList;
Handler mHandler;
private ActivityMainBinding binding;
private int currentQuestionIndex = 0;
SoundPool soundPool;
private int sound1, sound2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
questionList = new Repository().getQuestions(questionArrayList -> {
binding.questionTextview.setText(questionArrayList.get(currentQuestionIndex)
.getAnswer());
updateCounter(questionArrayList);
}
);
mHandler = new Handler();
binding.buttonNext.setOnClickListener(view -> {
currentQuestionIndex = (currentQuestionIndex + 1) % questionList.size();
updateQuestion();
});
binding.buttonTrue.setOnClickListener(view -> {
checkAnswer(true);
updateQuestion();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
changeTheQuestion();
}
}, 3000);
});
binding.buttonFalse.setOnClickListener(view -> {
checkAnswer(true);
updateQuestion();
new Handler().postDelayed(new Runnable() {
public void run() {
changeTheQuestion();
}
}, 3000);
});
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.build();
soundPool = new SoundPool.Builder()
.setMaxStreams(4)
.setAudioAttributes(audioAttributes)
.build();
sound1 = soundPool.load(this, R.raw.correct, 1);
sound2 = soundPool.load(this, R.raw.wrong, 1);
}
private void checkAnswer(boolean userChoseCorrect) {
boolean answer = questionList.get(currentQuestionIndex).isAnswerTrue();
int snackMessageId = 0;
if (userChoseCorrect == answer) {
snackMessageId = R.string.correct_answer;
fadeAnimation();
soundPool.play(sound1, 1, 1, 0, 0, 1);
} else {
snackMessageId = R.string.incorrect;
shakeAnimation();
soundPool.play(sound2, 1, 1, 0, 0, 1);
}
Snackbar.make(binding.cardView, snackMessageId, Snackbar.LENGTH_SHORT)
.show();
}
private void updateCounter(ArrayList<Question> questionArrayList) {
binding.textViewOutOf.setText(String.format(getString(R.string.text_formatted),
currentQuestionIndex, questionArrayList.size()));
}
private void fadeAnimation() {
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(300);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
binding.cardView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
binding.questionTextview.setTextColor(Color.GREEN);
}
@Override
public void onAnimationEnd(Animation animation) {
binding.questionTextview.setTextColor(Color.WHITE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void changeTheQuestion() {
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(300);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
binding.cardView.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
binding.questionTextview.setTextColor(Color.BLUE);
}
@Override
public void onAnimationEnd(Animation animation) {
currentQuestionIndex = (currentQuestionIndex + 1) % questionList.size();
updateQuestion();
binding.questionTextview.setTextColor(Color.WHITE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
private void updateQuestion() {
String question = questionList.get(currentQuestionIndex).getAnswer();
binding.questionTextview.setText(question);
updateCounter((ArrayList<Question>) questionList);
}
private void shakeAnimation() {
Animation shake = AnimationUtils.loadAnimation(MainActivity.this,
R.anim.shake_animation);
binding.cardView.setAnimation(shake);
shake.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
binding.questionTextview.setTextColor(Color.RED);
}
@Override
public void onAnimationEnd(Animation animation) {
binding.questionTextview.setTextColor(Color.WHITE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
导入android.graphics.Color;
导入android.media.AudioAttributes;
导入android.media.SoundPool;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.Handler;
导入android.view.animation.AlphaAnimation;
导入android.view.animation.animation;
导入android.view.animation.AnimationUtils;
导入com.bawp.trivia.data.Repository;
导入com.bawp.trivia.databinding.ActivityMainBinding;
导入com.bawp.trivia.model.Question;
导入com.google.android.material.snackbar.snackbar;
导入java.util.ArrayList;
导入java.util.List;
导入androidx.appcompat.app.appcompat活动;
导入androidx.databinding.DataBindingUtil;
公共类MainActivity扩展了AppCompatActivity{
列表问题列表;
汉德勒;
私人活动主要约束;
private int currentQuestionIndex=0;
声池声池;
私人int sound1、sound2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
questionList=新存储库().getQuestions(questionArrayList->{
binding.questionTextview.setText(questionArrayList.get(currentQuestionIndex)
.getAnswer());
更新计数器(问题列表);
}
);
mHandler=新处理程序();
binding.buttonNext.setOnClickListener(视图->{
currentQuestionIndex=(currentQuestionIndex+1)%questionList.size();
updateQuestion();
});
binding.buttonTrue.setOnClickListener(视图->{
核对答案(正确);
updateQuestion();
new Handler().postDelayed(new Runnable()){
@凌驾
公开募捐{
改变问题();
}
}, 3000);
});
binding.buttonFalse.setOnClickListener(视图->{
核对答案(正确);
updateQuestion();
new Handler().postDelayed(new Runnable()){
公开募捐{
改变问题();
}
}, 3000);
});
AudioAttributes AudioAttributes=新建AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT\u TYPE\u声音化)
.setUsage(AudioAttributes.USAGE\u ASSISTANCE\u SONIFICATION)
.build();
soundPool=新建soundPool.Builder()
.setMaxStreams(4)
.setAudioAttributes(audioAttributes)
.build();
sound1=soundPool.load(这个,R.raw.correct,1);
sound2=soundPool.load(这个,R.raw.error,1);
}
私有void checkAnswer(布尔userChoseCorrect){
布尔答案=questionList.get(currentQuestionIndex.isAnswerTrue();
int snackMessageId=0;
if(userChoseCorrect==答案){
snackMessageId=R.string.correct\u答案;
fadeAnimation();
soundPool.play(声音1,1,1,0,0,1);
}否则{
snackMessageId=R.string.error;
shakeAnimation();
soundPool.play(声音2,1,1,0,0,1);
}
Snackbar.make(binding.cardwiew、snackMessageId、Snackbar.LENGTH\u SHORT)
.show();
}
私有void更新计数器(ArrayList questionArrayList){
binding.textViewOutOf.setText(String.format(getString)(R.String.text\u格式化),
currentQuestionIndex,questionArrayList.size());
}
私有void fadeAnimation(){
AlphaAnimation AlphaAnimation=新的AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(300);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
binding.cardwiew.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(新的Animation.AnimationListener(){
@凌驾
onAnimationStart上的公共无效(动画){
binding.questionTextview.setTextColor(Color.GREEN);
}
@凌驾
onAnimationEnd上的公共无效(动画){
binding.questionTextview.setTextColor(Color.WHITE);
}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
}
私有无效更改问题(){
AlphaAnimation AlphaAnimation=新的AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(300);
alphaAnimation.setRepeatCount(1);
alphaAnimation.setRepeatMode(Animation.REVERSE);
binding.cardwiew.setAnimation(alphaAnimation);
alphaAnimation.setAnimationListener(新的Animation.AnimationListener(){
@凌驾
onAnimationStart上的公共无效(动画){
binding.questionTextview.setTextColor(Color.BLUE);
}
@凌驾
onAnimationEnd上的公共无效(动画){
currentQuestionIndex=(currentQuestionIndex+1)%questionList.size();
updateQuestion();
binding.questionTextview.setTextColor(Color.WHITE);
}
@凌驾
onAnimationRepeat上的公共无效(动画){
}
});
}
私有void updateQuestion(){
String question=questionList.get(currentQuestionIndex)
package com.bawp.trivia.data;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.toolbox.JsonArrayRequest;
import com.bawp.trivia.controller.AppController;
import com.bawp.trivia.model.Question;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
public class Repository {
ArrayList<Question> questionArrayList = new ArrayList<>();
String url = "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json";
public List<Question> getQuestions( final AnswerListAsyncResponse callBack) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET,
url, null, response -> {
for (int i = 0; i < response.length(); i++) {
try {
Question question = new Question(response.getJSONArray(i).get(0).toString(),
response.getJSONArray(i).getBoolean(1));
//Add questions to arraylist/list
questionArrayList.add(question);
//Log.d("Hello", "getQuestions: " + questionArrayList);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (null != callBack) callBack.processFinished(questionArrayList);
}, error -> {
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
return questionArrayList;
}
}
new Handler().postDelayed(new Runnable() {
public void run() {
changeTheQuestion();
}
}, 3000);