为什么我的文本视图无法在安卓中更新/重置?



我创建了一个TextView public TextView textView;,稍后在onCreate:中的MainActivity.java中定义它

    textView = (TextView) findViewById(R.id.textViewName);

然而,当我设置文本时,它没有正确更新。每当我使用setText方法时,它不会在屏幕上更新。对setText的初始调用在一个名为recordClap()的方法中。

    /**set text view*/
    textView.setText("listening...");

此文本不会更新到屏幕上。

最后,我将文本设置为在满足某些条件后显示"成功!"。

    textView.setText("Success!");

出于某种原因,这是对"setText"的唯一有效调用。

那么,为什么TextView没有正确更新新文本呢?我遗漏了什么吗?

以下完整代码:

public class MainActivity extends Activity{
private static final String TAG = "Clapper";
private static final long DEFAULT_CLIP_TIME = 1000;
private long clipTime = DEFAULT_CLIP_TIME;
/**create text view*/
public TextView textView;
private boolean continueRecording;
public static final int AMPLITUDE_DIFF_LOW = 10000;
public static final int AMPLITUDE_DIFF_MED = 18000;
public static final int AMPLITUDE_DIFF_HIGH = 32767; 
private int amplitudeThreshold=AMPLITUDE_DIFF_HIGH;
private MediaRecorder recorder = null;
private static String tmpAudioFile = null;
public boolean recordClap()
{
/**set text view*/
textView.setText("listening...");
Log.i(TAG, "record clap");
boolean clapDetected = false;
try
{
    tmpAudioFile = Environment.getExternalStorageDirectory().getAbsolutePath();
    tmpAudioFile += "/audiorecordtest.3gp";
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile(tmpAudioFile);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.prepare();
    Log.i(TAG, "i've been prepared!");
}
catch (IOException io)
{
    Log.e(TAG, "failed to prepare recorder ", io);
}
recorder.start();
int startAmplitude = recorder.getMaxAmplitude();
Log.i(TAG, "starting amplitude: " + startAmplitude);
do
{
    Log.i(TAG, "waiting while recording...");
    waitSome();
    int finishAmplitude = recorder.getMaxAmplitude();
    int ampDifference = finishAmplitude - startAmplitude;
    if (ampDifference >= amplitudeThreshold)
    {
        Log.w(TAG, "heard a clap!");
        /**here is the output to screen*/
        /**reset text view*/
        textView.setText("Success!");
        clapDetected = true;
    }
    Log.d(TAG, "finishing amplitude: " + finishAmplitude + " diff: "
            + ampDifference);
} while (continueRecording || !clapDetected);
Log.i(TAG, "stopped recording");
done();
return clapDetected;
}
private void waitSome()
{
try
{
    // wait a while
    Thread.sleep(clipTime);
} catch (InterruptedException e)
{
    Log.i(TAG, "interrupted");
}
}
public void done()
{
Log.d(TAG, "stop recording");
if (recorder != null)
{
    if (isRecording())
    {
        stopRecording();
    }
        //now stop the media player
        recorder.stop();
        recorder.release();
    }
}
public boolean isRecording()
{
    return continueRecording;
}
public void stopRecording()
{
    continueRecording = false;
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("hello", "world!");
    setContentView(R.layout.activity_main);
    /**define text view*/
    textView = (TextView) findViewById(R.id.textViewName);
    /**run*/
    recordClap();
/**Restart Button*/    
final Button button = (Button) findViewById(R.id.button_id);
    button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
        recordClap();
    }
  });
}
} 

正如我在评论中所说,变化发生得太快了,你看不到。试着放慢速度,或者增加等待时间。可能吗?但是请注意——如果我没有记错的话,在你的waitSome()方法中,你是Thread.正在UI线程上睡觉——这将导致UI在此期间无法更新。所以它可能是这样发生的(我记不清UI线程队列是如何工作的):

  1. recordClap被称为
  2. textView.setText("正在侦听")被调用并放入队列(但UI尚未更新)
  3. Thread.sleep(1000)暂停UI线程
  4. TextView说"听"一秒钟,然后继续

请参考textview,即textView = (TextView) findViewById(R.id.textViewName);在recordclap()函数中,然后再设置文本。

感谢

最新更新