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