在Android中,如何使用Java代码在string.xml中添加字符串?
我在android应用程序的Java代码中创建了两个EditText对象,如下所示:在Android中,如何使用Java代码在string.xml中添加字符串?,java,android,string,android-edittext,Java,Android,String,Android Edittext,我在android应用程序的Java代码中创建了两个EditText对象,如下所示: final EditText et1=(EditText)findViewById(R.id.editText1); final EditText et2=(EditText)findViewById(R.id.editText2); 然后在按钮的onClick()事件上,调用一个参数为- addStringToXmlFile(et1.getText(),et2.getText()); 在下面这个方法的定义
final EditText et1=(EditText)findViewById(R.id.editText1);
final EditText et2=(EditText)findViewById(R.id.editText2);
然后在按钮的onClick()事件上,调用一个参数为-
addStringToXmlFile(et1.getText(),et2.getText());
在下面这个方法的定义中,我写了-
private void addStringToXmlFile(Editable editable1,Editable editable2){
String s1=new String();
s1=editable1.toString();
String s2=new String();
s2=editable2.toString();
}
问题是,现在我想使用这两个String对象s1、s2在数据库的res/values/Strings.xml
文件中创建两个条目,我不知道怎么做
请进一步指导我。看看如何使用SharedReferences来存储字符串值
//Saving your strings
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString("s1", s1);
editor.putString("s2", s2);
editor.commit();
//retrieving your strings from preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
String s1 = prefs.getString("s1", ""); //empty string is the default value
String s2 = prefs.getString("s2", ""); //empty string is the default value
这是不可能的-无法在运行时更改应用程序的apk(包括其资源)。我不能完全确定所有的原因,但我能想到的一件明显的事情是,R.java需要包含对字符串的引用,以便您访问它,并且这个文件是在您创建APK时由编译器生成的
如果您需要跨会话持久化字符串,那么应该考虑使用Android提供的几种字符串之一,例如。您只需执行String s2=editable2.toString();在分配字符串之前,不需要初始化它