【发布时间】:2017-11-28 16:13:58
【问题描述】:
我正在构建一个应用程序。当我单击一个按钮时,它将启动一个带有文本“Hello world”的活动(例如.activity_sub),当单击另一个按钮时,它将启动相同的活动但不同的文本视图“hi伙伴”。 我还没有按下按钮,因为我不知道如何继续。 我正在使用安卓工作室。
这是我从资产中获取 txtfile 的 java 代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
InputStream is = this.getAssets().open("assetstext.txt");
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer);
// Finally stick the string into the text view.
TextView tv = (TextView)findViewById(R.id.assetstext);
tv.setText(text);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
【问题讨论】:
-
根据上面的代码,我知道按钮上的文本将从外部文件中读取?或者,文本描述了您要完成的任务,但您不确定如何完成?
-
为什么要重新开始相同的活动?单击按钮时更改文本视图文本不是更容易吗?。
-
是的,如果我不开始相同的活动,那么我必须创建很多活动。因为我想构建歌词应用程序。谢谢
标签: android android-activity textview assets android-studio-3.0