1

当我在中设置文本时TextView,它的文本在转到correctCondition()方法后会发生变化;但我想在去之前更改文本correctCondition()。所以TextView当我单击按钮时这会改变,然后运行进一步的操作。

我想同时播放声音“一”和显示文本“1”

在这里我添加一些代码

image1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (mClicked == false) {
                mClicked = true;
                count = count + 1;
                numberWrite.setText("" + count);
                one();
                correctCondition();

            }  
        }
    });




private void one() {

        Thread tr = new Thread(new Runnable() {

            @Override
            public void run() {

        AssetFileDescriptor one;
        try {
            one = mActivity.getAssets().openFd(
                    "counting/one.mp3");

        MediaPlayer counting = new MediaPlayer();


            counting.setDataSource(one.getFileDescriptor(),
                    one.getStartOffset(), one.getLength());
            one.close();
            counting.prepare();
            counting.setVolume(1f, 1f);
            counting.start();

            counting.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    mp.release();
                }
            });





        } catch (IOException e1) {

            e1.printStackTrace();
        }
        }
    });

        tr.start();
}

public void playCorrectBeep() {
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            MediaPlayer correctMp = new MediaPlayer();

            try {
                AssetFileDescriptor correct = mContext.getAssets().openFd(
                        "correct.mp3");
                correctMp.setDataSource(correct.getFileDescriptor(),
                correct.getStartOffset(),correct.getLength());

                correct.close();
                correctMp.prepare();
                correctMp.start();
                correctMp.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                                mp.release();
                            }
                        });
            } catch (IllegalArgumentException e) {

                e.printStackTrace();
            } catch (IllegalStateException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

        }
    });

    t.start();

}

提前致谢

4

1 回答 1

1

你的 switch 需要一个 "break",这样做:

switch (v.getId()) {
    case R.id.imageView1:

        if (mClicked == false) {
            mClicked = true;
            count = count + 1;                    
            numberWrite.setText(""+ count);  
            toast.cancel();

            soundManager.one();
            correctCondition();
        }

     break; // add this line
 }

此外,您不应该让您的 correctCondition() 方法在 UI 线程上运行。相反,让您的 SoundManager.one(); 在单独的线程中运行。

public void one() {

    Thread t = new Thread(new Runnable) {

        public void run() {

          counting = new MediaPlayer();
          try {

            counting.setDataSource(one.getFileDescriptor(), one.getStartOffset(), one.getLength());
            one.close();
            counting.prepare();
            counting.setVolume(1f, 1f);
            counting.start();

            counting.setOnCompletionListener(new OnCompletionListener() {
                   public void onCompletion(MediaPlayer mp) {
                        mp.release();
                   }
            });

         } catch (IOException e) {
               e.printStackTrace();
         }
       }
     };
     t.start();
}
于 2013-09-03T15:19:55.607 回答