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

In Java, how do I execute lines of code in a certain order with a delay in between them?

在此代码段中,我希望用户按相应的顺序体验以下内容: 1. 查看输入的字母he/she。 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);

我尝试添加另一个声音片段 'fullSentence' 以查看一切执行的确切顺序。

实际发生了什么: 第 1 次:我看到红色的“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 playback and MediaPlayer.OnCompletionListener javadoc