You tagged it as "android-assets" so I'm going to assume your file is in the assets folder. Here:
InputStream in;
BufferedReader reader;
String line;
TextView text;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.textView1);
in = this.getAssets().open(<your file>);
reader = new BufferedReader(new InputStreamReader(in));
line = reader.readLine();
text.setText(line);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(this);
}
public void onClick(View v){
line = reader.readLine();
if (line != null){
text.setText(line);
} else {
//you may want to close the file now since there's nothing more to be done here.
}
}
Give this a try. I haven't been able to verify that it works completely, but I believe this is the general idea you want to follow. Naturally you'll want to replace any R.id.textView1/button1
with the names that you've specified in your layout file.
Also: There is very little error checking here for the sake of space. You will want to check that your asset exists and I'm pretty sure there should be an try/catch
block when you open the file for reading.
Edit: Big error, It's not R.layout
, it's R.id
I have edited my answer to fix the problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…