Android 如何使用/raw中存储的文本填充活动
我有一个项目应用程序。我的想法是创建一个3个标签的应用程序,每个标签上都有几个代表一个主题的按钮。每个主题都有一个txt文件,其中包含一些我希望在单击特定按钮时打开的活动中显示的信息。因此,打开相同的活动,但填充该活动的文本将取决于按下的按钮 除了显示文本(当前以txt格式存储在/raw中)之外,我拥有所有功能 我正在研究输入阅读器和txt与xml(前者似乎更容易),但我仍在苦苦挣扎 即使我设法让它显示一个特定的txt文件,我也不知道如何让它工作取决于按下哪个按钮 内容保存活动有一个textview,我计划用.txt文件中的文本填充它 任何帮助都将不胜感激 我在那个捕获上得到了一个语法错误。我花了15分钟想弄明白 编辑 带有按钮的活动,Android 如何使用/raw中存储的文本填充活动,android,Android,我有一个项目应用程序。我的想法是创建一个3个标签的应用程序,每个标签上都有几个代表一个主题的按钮。每个主题都有一个txt文件,其中包含一些我希望在单击特定按钮时打开的活动中显示的信息。因此,打开相同的活动,但填充该活动的文本将取决于按下的按钮 除了显示文本(当前以txt格式存储在/raw中)之外,我拥有所有功能 我正在研究输入阅读器和txt与xml(前者似乎更容易),但我仍在苦苦挣扎 即使我设法让它显示一个特定的txt文件,我也不知道如何让它工作取决于按下哪个按钮 内容保存活动有一个textvi
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也不起作用。您可以打开一个asset txt文件并按如下方式逐行读取:
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());
}
这是一个很好的开始,在文本视图中显示您阅读的行。一旦您这样做了,您就可以进入下一步
编辑
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);
}
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);
}
这不就是把它打印到eclipse控制台吗?另外,我在我挖掘的几个示例中看到了上下文,但我不知道如何处理它。eclipse说它无法解决,而且谷歌在Android中也不友好,你的程序起点是一个扩展活动的类。在这个类中,你可以调用getApplicationContext
,请参阅我在五月回答的编辑。在编辑我的帖子的部分是我在atm上工作的类。现在唯一的问题是接球。我尝试了很多变体,但它总是给我一个语法错误。有什么想法吗?这是因为您没有正确关闭打开的{
。有关固定版本,请参阅我的edit2。您还需要将一些值设置为String fileName;
,类似于String fileName=“myfile.txt”
;我将顶部的var更改为String fileName=“txtfile.txt”;。最后3行正在标记,但是eclipse需要setcontentView的返回类型正如您所看到的,我在底部注释掉了setcontentView,它在那里不起作用。我完全被困在atm机上。所以您基本上只是根据按下的按钮来读取文本文档?没错。用户单击一个按钮,就会打开一个活动我觉得这很简单。也不喜欢那个打印语句,所以你在阅读文本文件时遇到了问题?这就是问题吗?我在所有这些方面都遇到了问题。输出文本与我的代码不兼容,而你给我的位只会导致一个按钮打开新的活动,所以我现在不知所措我已经准备好放下这个试试别的了