如何填充带文本的活动存放在/生 [英] How to Fill an Activity with text stored in /raw
问题描述
我有一个项目的应用程序。我的想法是创建一个3选项卡中的应用程序,在每个选项卡中有多个按钮重新presenting的话题。每个主题包含一些信息,我想显示点击一个特定的按钮时打开了一个活动的TXT文件。所以同样的活动被打开,但文本填充它取决于按钮pressed。
I have an app for a project. my idea was to create a 3 tab app, in each tab there are several buttons representing a topic. each topic has a txt file containing some information I wish to display in an activity that is opened when clicking a particular button. so the same activity is opened, but the text filling it will depend on the button pressed.
我除了要显示文本的功能应有尽有,目前存放在/生作为一个txt。
I have everything except the ability to display the text, currently stored in /raw as a txt.
我期待在输入读者和txt VS XML(前者似乎更容易),我还在挣扎。
I'm looking at input readers and txt vs xml (former seems easier), and I'm still struggling.
即使我设法得到它显示一个特定的txt文件,我不知道如何得到它的工作依赖于哪个按钮是pressed。
Even if I manage to get it to display a particular txt file, I don't know how to get it to work dependant on which button is pressed.
的内容保持活动有我的计划,以填补从.txt文件文本TextView中。
The content holding activity has a textview which I planned to fill with text from the .txt file.
任何帮助将是AP preciated。
Any help would be appreciated.
我得到该catch语法错误。度过了最后15分钟试图弄明白
I get a syntax error on that catch. spent the last 15 minutes trying to figure it out
修改
在它的按钮活动,
public class MartialTab extends Activity {
Button btn1, btn2;
String choice;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textview = new TextView(this);
textview.setText("This is the martial arts tab");
setContentView(textview);
setContentView(R.layout.martial_tab);//takes layout from martial_tab.xml
btn1 = (Button) findViewById(R.id.kickboxingButton);//instantiates a button called btn1 one from the xml
btn1.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
kickBoxingBut(v, choice);
}//calls the method
});//end of method
btn2 = (Button) findViewById(R.id.grecoroman);//instantiates a button called btn1 one from the xml
btn2.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
grecoromanBut(v);
}//calls the method
});//end of method
}
// private void kickBoxingBut() {//button1 method
//new AlertDialog.Builder(this).setTitle("AlertNotification").setMessage(
// "This is an Alert Dialogue Toast").setNeutralButton( "Click Me!", null).show();
//creates an alert notification with the above text
//}
public String kickBoxingBut(View view, String s) {
startActivity(new Intent(this, ContentHolder.class));
this.choice = "kick";
return choice;
}
public void grecoromanBut(View view) {
startActivity(new Intent(this, ContentHolder.class));
}
}
的活动,保持信息
public class ContentHolder extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context m_Context = getApplicationContext();
TextView textview = new TextView(this);
textview.setText("This is the content view");
setContentView(textview);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("choice");
}
InputStream input;
try {
input = m_Context.getAssets().open("choice");
DataInputStream in = new DataInputStream(input);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println(strLine);
textview.setText(strLine);
}
// Close the input stream
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//setContentView(R.layout.main);
}
此的setContentView是一个问题。如上所述,因为它需要一个返回类型不起作用。将其更改为ContentHolder也不行。
This setContentView is a problem. As above doesn't work as it expects a return type. changing it to ContentHolder doesn't work either.
推荐答案
您可以通过这样的开行资产txt文件,读行:
You can open an asset txt file and read line by line like this:
InputStream input = context.getAssets().open(fileName);
DataInputStream in = new DataInputStream(input);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
} catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
这是一个良好的开端给你,显示您在的TextView
读线。一旦你这样做,你就可以进入下一个步骤。
This is a good start for you, display the lines you read in a TextView
. Once you do this, you can move on to next step.
修改
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This is how you get the context
Context m_Context = getApplicationContext();
// This is how you show text programatically, there is also xml option, which I recommend instead.
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
http://developer.android.com/resources/tutorials/hello- world.html
EDIT2
public class ContentHolder extends Activity {
String fileName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context m_Context = getApplicationContext();
TextView textview = new TextView(this);
textview.setText("This is the content view");
InputStream input;
try {
input = m_Context.getAssets().open(fileName);
DataInputStream in = new DataInputStream(input);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println(strLine);
textview.setText(strLine);
}
// Close the input stream
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
setContentView(R.layout.main);
}
这篇关于如何填充带文本的活动存放在/生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!