我对我正在做的事情完全不熟悉,而且我认为我完全错了。
无论如何......我正在尝试创建一个测试应用程序,在其中将一些String写入editText,应用程序应将其保存到下一次启动。
我试图通过xml资源做到这一点,它似乎完全搞砸了。拜托,帮助我!
这就是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Filedemo" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="130dp"
android:text="finish" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="111dp"
android:ems="10"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</RelativeLayout>
这是我想要将字符串保存到的xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="thetext" type="id"/>
<item name="textik" type="whatever">
<thetext>hello</thetext>
</item>
</resources>
...最后我的java代码:
public class Filedemo extends Activity {
private Document doc;
private EditText etext;
private Button but;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filedemo);
etext = (EditText)findViewById(R.id.editText1);
but = (Button)findViewById(R.id.button1);
Context context = getApplicationContext();
InputStream istream = context.getResources().openRawResource(R.id.thetext);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = null;
try {
docBuilder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
doc = docBuilder.parse(istream);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Node textik = doc.getElementsByTagName("thetext").item(1);
etext.setText(textik.getTextContent());
but.setOnClickListener(new OnClickListener(){
public void onClick(View v){
String s = etext.getText().toString();
textik.setTextContent(s);
System.exit(0);
}
});
}
}