我的好友和我正在Android Studio中编写一个简单的应用程序。按下按钮时,将打开一个新活动,其中包含您按下的按钮的名称,并在该文件中显示该文本。
我有生成第一组按钮的代码(这些是硬编码的),我可以得到按下按钮的名称。我的麻烦是阅读文本文件并显示内容。文本文件中的每一行都是一个需要作为按钮文本值的单词。我不能对这些词进行硬编码,因为它们可以经常更改。
实施例;在主活动上,按下标有" Round"的按钮,它会将您发送到一个页面,其中包含文本文件中名为" round"的所有单词。列为按钮。
我早些时候问了这个问题,但是因为太模糊而被搁置了。 我希望这更清楚。这是我正在使用的代码,但需要代码来读取文件。这不行。
我甚至无法显示第一行。文件内容是这个--- 管 弯头 减速器 点击平坦 EC
请帮忙。 提前谢谢。
public class test extends Activity {
int counter = 0;
protected void onCreate(Bundle savedInstanceState) {
counter=0;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("round.txt")));
// do reading, usually loop until end of file reading
String mLine;
while ((mLine = reader.readLine()) != null) {
for (int row = 0; row < 10; row++){
TableRow tblRow = new TableRow(this);
tblRow.setPadding(5,30,5,5);
table.addView(tblRow);
int NUM_COL = 3;
for (int col = 0; col != NUM_COL; col++){
Button btn = new Button(this);
btn.setText(mLine);
tblRow.addView(btn);
NUM_COL++;
}
}
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
}
}
}
答案 0 :(得分:3)
我找到了答案。谢谢你指点我正确的方向。 这是
try {
InputStream is = getAssets().open("round.txt");
// We guarantee that the available method returns the total
// size of the asset... of course, this does mean that a single
// asset can't be more than 2 gigs.
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.
// Replace with whatever you need to have the text into.
TextView tv = (TextView)findViewById(R.id.text);
tv.setText(text);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
答案 1 :(得分:1)
重写了代码,这是有效的代码。 BufferedReader读卡器; 尝试{ InputStream = getAssets()。open(&#34; round.txt&#34;);
reader = new BufferedReader(new InputStreamReader(is));
// Finally stick the string into the text of the button.
TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent);
String line = reader.readLine();
int lineLength = (line.length());
while (line != null){
TableRow tblRow = new TableRow(this);
tblRow.setPadding(5, 30, 5, 5);
table.addView(tblRow);
for (int col = 0; col < NUM_COL; col++) {
Button btn = new Button(this);
btn.setTextSize(14);
btn.setText(line);
tblRow.addView(btn);
line = reader.readLine();
}
};
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
}
答案 2 :(得分:0)
试试这个...添加 getResources()
reader = new BufferedReader(
new InputStreamReader(getResources().getAssets().open("round.txt")));
答案 3 :(得分:0)
U可以逐行读取文件,如下所示:
String filename = "filename.txt";
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader
(this.getAssets().open(filename), StandardCharsets.UTF_8));
String line;
while ((line = bufferedReader.readLine()) != null) {
//add the lines in some arraylist if you want to set them.
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 4 :(得分:0)
在科特林,我们可以这样做
val string = requireContext().assets.open("round.txt").bufferedReader().use {
it.readText()
}