Android 使用append更新TextView不会打印新文本
我有一个活动,它使用RoboGuice打印日志。其中有一个文本视图,显示从文件读取的大文本字符串 问题是我想使用textView.append()添加新行,但什么也没发生。textView.setText工作正常,但我不能使用它,因为文本重置很慢,并且会消耗CPU 我试了很多东西。在UI线程上运行它,使视图无效,使用view.post(Runnable)并在UI线程的处理程序上发布它,但没有任何帮助 以下是代码示例:Android 使用append更新TextView不会打印新文本,android,android-activity,textview,Android,Android Activity,Textview,我有一个活动,它使用RoboGuice打印日志。其中有一个文本视图,显示从文件读取的大文本字符串 问题是我想使用textView.append()添加新行,但什么也没发生。textView.setText工作正常,但我不能使用它,因为文本重置很慢,并且会消耗CPU 我试了很多东西。在UI线程上运行它,使视图无效,使用view.post(Runnable)并在UI线程的处理程序上发布它,但没有任何帮助 以下是代码示例: public class DiagnosticsActivity extend
public class DiagnosticsActivity extends RoboActionBarActivity implements LogListener {
@Inject
private LogMessageHandler logger;
@InjectView(R.id.logView)
TextView logView;
@InjectView(R.id.logScrollView)
ScrollView scrollView;
public boolean logFileReadFinished = false;
private StringBuilder textUntilReadFinished = new StringBuilder();
private File sdcard;
private class AppendRunnable implements Runnable {
private String msg;
private AppendRunnable(String msg) {
this.msg = msg;
}
public void run() {
logView.append(msg);
logView.invalidate();
}
};
@Override
protected void onResume() {
logger.registerLogListener(this);
readLogFile();
scrollView.scrollTo(0, Integer.MAX_VALUE);
synchronized (this) {
final String stringUntilReadFinished = textUntilReadFinished.toString();
textUntilReadFinished = new StringBuilder();
DiagnosticsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Handler mHandler = new Handler();
mHandler.post(new AppendRunnable(stringUntilReadFinished + "\n"));
}
});
logFileReadFinished = true;
}
super.onResume();
}
@Override
protected void onPause() {
logger.removeLogListener();
logFileReadFinished = false;
super.onPause();
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_diagnostics);
ActionBar actionBar = getSupportActionBar();
actionBar.show();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setTitle(R.string.title_diagnostics);
sdcard = Environment.getExternalStorageDirectory();
}
private void readLogFile() {
FileInputStream fileInputStream = null;
BufferedReader reader = null;
try {
final StringBuilder text = new StringBuilder();
File file = new File(sdcard, LoggerImpl.logName + ".log");
if (file.exists()) {
try {
fileInputStream = new FileInputStream(file);
reader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
while ((line = reader.readLine()) != null) {
text.append(line + "\n");
}
DiagnosticsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
logView.setText(text);
}
});
} catch (FileNotFoundException e) {
logger.error("Log file not found!", e);
} catch (IOException e) {
logger.error("Cannot read log file!", e);
}
}
} finally {
try {
reader.close();
fileInputStream.close();
} catch (IOException e) {
logger.error("Cannot close log file", e);
}
}
}
@Override
public void onLogEvent(final String msg, Throwable throwable) {
synchronized (this) {
if (logFileReadFinished) {
DiagnosticsActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Handler mHandler = new Handler();
mHandler.post(new AppendRunnable(msg + "\n"));
}
});
} else {
textUntilReadFinished.append(msg + "\n");
}
}
}
在调用
append()
时,是否已检查以确保msg
不是空的
或者,您可以使用以下命令更新文本视图
,而无需删除旧信息:
logView.setText(logView.getText() + msg);
上面的代码片段将获取当前显示的文本,附加新消息,然后显示整个
字符串
Hi!是的,我检查过了,它不是一个空字符串。logView.setText(logView.getText()+msg)工作正常,但会阻塞UI线程,因为文本很长,更新频繁。