为什么我的TextView不会在android中更新/重置?

时间:2013-06-25 18:54:47

标签: java android text textview settext

我创建了一个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();
    }
  });
}
} 

2 个答案:

答案 0 :(得分:1)

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

  1. recordClap称为
  2. 调用textView.setText(“listening”)并将其放入队列(但是UI 尚未更新)
  3. Thread.sleep(1000)暂停UI线程
  4. TextView说“聆听”一瞬间然后继续

答案 1 :(得分:0)

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

由于