当我导航到应用程序中的页面时,我希望显示文本文件中的文本,但事实并非如此。我错过了什么吗?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
Button backButton = findViewById(R.id.button_back);
Button selectAnotherButton = findViewById(R.id.button_select_another);
TextView contentText = findViewById(R.id.content_text);
String text = "";
try {
// file to inputstream
InputStream input = getAssets().open("jokes.txt");
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
// byte buffer into a string
text = new String(buffer);
} catch (Exception e) {
System.out.println(e);
}
contentText.setText(text);
}
最佳答案
上面的代码很好,它按预期工作。无法看到文本的原因是因为有一个 TextView 布局与文件文本 TextView 布局重叠,所以我看不到文本
关于java - 如何读取文本文件并在 TextView 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50842812/