gpt4 book ai didi

java - 在Java中,如何按一定顺序执行代码行并在它们之间有延迟?

转载 作者:行者123 更新时间:2023-12-01 11:56:17 25 4
gpt4 key购买 nike

在此代码 fragment 中,我希望用户按相应的顺序体验以下内容:1. 查看他/她输入的字母。2. 听到“wrongAns”声音。3.声音结束后,将其文字替换为默认的“0”。

这是我尝试过的:

    tv_1.setText(input1);
tv_1.setTextColor(Color.BLACK);
wrongAns.start();
try{
Thread.sleep(2300);
}catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
// fullSentence.start();
tv_1.setText("0");
tv_1.setTextColor(Color.RED);

我尝试添加另一个声音 fragment “fullSentence”来查看所有内容的执行顺序。

实际发生的情况:第一:我看到红色的“0”,同时,我听到“wrongAns”的声音。第二:在“wrongAns”开始后 2.3 秒我听到“fullSentence”。

如何让它正常工作?

编辑:(这是类(class))

public class TestCorrectSentence extends Activity implements OnClickListener {

MediaPlayer fullSentence, correctAns, wrongAns;
TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6, tv_7, tv_8, tv_9, tv_10;
String A_1, A_2, A_3, A_4, A_5, A_6, A_7, A_8, A_9, A_10;
int counter;
EditText user_input;
Button b_Chk;
String input1, input2;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sentence_activity);
SoundsMethod();
TextViewsMethod();
InitializeMethod();
ETandButtonMethod();
}

private void SoundsMethod() {
fullSentence = MediaPlayer.create(TestCorrectSentence.this, R.raw.voice_sent);
wrongAns = MediaPlayer.create(TestCorrectSentence.this, R.raw.voice_wrong);
}

private void TextViewsMethod() {
tv_1 = (TextView) findViewById(R.id.tvAns1);
}

private void InitializeMethod() {
A_1 = tv_1.getText().toString();

counter = 1;
}

private void ETandButtonMethod() {
user_input = (EditText) findViewById(R.id.etInput);

b_Chk = (Button) findViewById(R.id.bCheck);
b_Chk.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.bCheck:
try{
CheckAnsMethod();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}

private void CheckAnsMethod() {
// TODO Auto-generated method stub
if (counter == 1){
if ("0".equals(A_1)){
input1 = user_input.getText().toString();
if ("T".equals(input1)){
tv_1.setText(input1);
tv_1.setTextColor(Color.GREEN);
correctAns.start();
counter++;
}else{
wrongAns.start();
tv_1.setText(input1);
tv_1.setTextColor(Color.BLACK);
try{
Thread.sleep(2300);
}catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
tv_1.setText("0");
tv_1.setTextColor(Color.RED);
fullSentence.start();
}
}else{
// wrongAns.start();
// fullSentence.start();
}
}else if (counter == 2){
if ("0".equals(A_2)){
input2 = user_input.getText().toString();
if ("h".equals(input2)){
// tv_2.setText(input2);
// tv_2.setTextColor(Color.GREEN);
// tv_2.setTypeface(null, Typeface.BOLD);;
// correctAns.start();
// correctAns.start();
// counter++;
}else{
// tv_2.setText(input2);
// tv_2.setTextColor(Color.BLACK);
// tv_2.setTypeface(null, Typeface.BOLD);;
// wrongAns.start();
// wrongAns.start();
// tv_2.setText("0");
// tv_2.setTypeface(null, Typeface.BOLD);;
}
}
}
}
}

最佳答案

您必须使用MediaPlayer lifecycle这里的听众,类似这样的:

final TextView textView1 = tv_1;
wrongAns.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// reset your values here
textView1.setText("0");
}
});
wrongAns.start();

更多信息:Android dev. guide about media playbackMediaPlayer.OnCompletionListener javadoc

关于java - 在Java中,如何按一定顺序执行代码行并在它们之间有延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com