我正在尝试创建一个存储变量T1或TextOne EditText的XML文件,但我不确定我是否在下面的代码中正确地执行了该操作。我也试图找出如何读取创建的xml文件并将T1变量显示到另一个EditText中。
package com.example.treasurehunt;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;
import org.xmlpull.v1.XmlSerializer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class CreateTreasure extends Activity {
//variables
EditText TextOne, TextTwo, TextThree, TextFour, TextFive;
Button SaveTreasure;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_treasure);
TextOne = (EditText) findViewById(R.id.EditTextOne);
final String T1 = TextOne.getText().toString();
SaveTreasure = (Button) findViewById(R.id.SaveTreasuresButton);
//onclick
SaveTreasure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final String xmlFile="TreasureList";
FileOutputStream fileos;
try {
fileos = getApplicationContext().openFileOutput(xmlFile, Context.MODE_APPEND);
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter writer = new StringWriter();
xmlSerializer.setOutput(writer);
xmlSerializer.startDocument("UTF-8",true);
xmlSerializer.startTag(null, "TreasureList");
xmlSerializer.startTag(null, "TreasureOne");
xmlSerializer.text(T1);
xmlSerializer.endTag(null,"TreasureOne");
xmlSerializer.endTag(null, "TreasureList");
xmlSerializer.endDocument();
xmlSerializer.flush();
String dataWrite=writer.toString();
fileos.write(dataWrite.getBytes());
fileos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create_treasure, menu);
return true;
}
}