1

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

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

2 回答 2

1

正如我在评论中所说,变化发生得如此之快,以至于你看不到它。尝试放慢速度,或者增加你的 waitSome 可能吗?但请注意——如果我在你的 waitSome() 方法中没记错的话,你是在 UI 线程上的 Thread.sleeping——这将导致 UI 在此期间不更新。所以它可能会发生这样的事情(我不记得 UI 线程队列是如何工作的):

  1. recordClap 被称为
  2. 调用 textView.setText("listening") 并将其放入队列(但 UI 尚未更新)
  3. Thread.sleep(1000) 暂停 UI 线程
  4. TextView 说“听”一秒钟然后继续
于 2013-06-25T21:47:10.340 回答
0

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

谢谢

于 2013-06-25T19:20:27.993 回答