简体   繁体   English

设置TextView的文本无法正常工作

[英]Setting text for TextView not working properly

When I Set Text in TextView , its text changes after going to correctCondition() method; 当我在TextView设置Text时,其文本将在correctCondition()方法后更改; but I want to change text before going to correctCondition() . 但是我想在correctCondition()之前更改文本。 so That TextView will change when I click button, and then further operations run. 因此,当我单击按钮时, TextView将会更改,然后运行进一步的操作。

I want to play sound "one" and display text "1" at the same time 我想播放声音“一个”并同时显示文本“ 1”

Here I add some code in 我在这里添加一些代码

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

}

Thanks in advance 提前致谢

Your switch needs a "break" , do it like this: 您的交换机需要“中断” ,请按照以下步骤操作:

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
 }

Furthermore, you should not let your correctCondition() method run on the UI-Thread. 此外, 您不应让您的correctCondition()方法在UI-Thread上运行。 Instead, let your SoundManager.one(); 相反,让您的SoundManager.one(); run in a separate Thread. 在单独的线程中运行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM