【发布时间】:2015-09-28 23:32:25
【问题描述】:
好的,我正在开发我的第一个 Android 应用程序。如果这听起来很天真,请原谅我,因为我对此有点陌生,但我使用以下代码从 6 个不同的 .txt 文件读取到一个
private void displayFiles(int path) {
TextView text = (TextView) findViewById(R.id.text);
ImageView display = (ImageView) findViewById(R.id.head_image);
String line = "";
StringBuilder finalString = new StringBuilder();
InputStream iStream = null;
if (path == 0) {
iStream = getResources().openRawResource(R.raw.salm);
display.setImageResource(R.drawable.salm);
} else if (path == 1) {
iStream = getResources().openRawResource(R.raw.lyme);
display.setImageResource(R.drawable.lyme);
} else if (path == 2) {
iStream = getResources().openRawResource(R.raw.pox);
display.setImageResource(R.drawable.pox);
} else if (path == 3) {
iStream = getResources().openRawResource(R.raw.shig);
display.setImageResource(R.drawable.shig);
} else if (path == 4) {
iStream = getResources().openRawResource(R.raw.gia);
display.setImageResource(R.drawable.gia);
} else if (path == 5) {
iStream = getResources().openRawResource(R.raw.intro);
display.setImageResource(R.drawable.intro);
} else {
Toast.makeText(this, "Something went wrong! Please refresh EpiQuick.", Toast.LENGTH_SHORT).show();
return;
}
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
try {
while ((line = bReader.readLine()) != null) {
finalString.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
text.setText(finalString);
}
对应的xml:
<TextView
android:id="@+id/text"
style="@style/body"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/head_image"
android:layout_marginBottom="16dp"
/>
<style name="body">
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#000000</item>
</style>
文本示例如下:
有风险的人: 前往贾第虫病常见国家的旅行者 托儿所中的人 与患病者有密切接触的人 吞下受污染饮用水的人 从湖泊或河流中饮用未经处理的水的背包客或露营者 与患病动物接触的人 男男性行为者
但是,当我在调用后运行应用程序时,所有换行符都会消失。我想知道是否有办法解决这个问题?如果有怎么办?
【问题讨论】: