世界杯2022是哪个国家_国足世界杯出线 - dtfyjq.com

  • 首页
  • 北京世界杯
  • 世界杯新秀
  • 世界杯16强名单

最新发表

  • 微信朋友圈如何合并视频?探索微信编辑功能,开启视频合并之旅
  • sklearn实现12种回归模型
  • 天津市手机批发市场大全在哪里
  • PS形状安装教程
  • 赵的繁体字
  • 含有王的汉字
  • 非公版RTX 3080首日发售情报:各家RTX 3080购买链接汇总
  • 槟榔的前世今生和歌曲《采槟榔》的由来
  • 【Edge小技巧】如何保存所有/多个标签页,关闭浏览器后快速恢复?
  • 局域网内如何远程控制另一台电脑(怎么远程控制局域网内电脑)?5种简单方法|局域网管控电脑注意事项

友情链接

Copyright © 2022 世界杯2022是哪个国家_国足世界杯出线 - dtfyjq.com All Rights Reserved.

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

北京世界杯 · 2025-10-31 19:53:07

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

}

});

}

}


最大飞鸟是哪种?翼展3.7米的信天翁并非最大,曾有种鸟翼展超7米
2025年胡志明市三球开伦式台球世界杯锦标赛火热开杆