API使用步骤:
public class MainActivity extends Activity {
private EditText qq_num,qq_pwd;
private SharedPreferences sp ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
qq_num = (EditText) findViewById(R.id.qq_num);
qq_pwd = (EditText) findViewById(R.id.qq_pwd);
//在自己的包下会产生config.xml的文件
/* <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="num">asddasd</string>
<string name="pwd">asdsd</string>
</map>*/
sp = this.getSharedPreferences("config", 0);
load();
}
private void load() {
String num = sp.getString("num", "");
String pwd = sp.getString("pwd", "");
qq_num.setText(num);
qq_pwd.setText(pwd);
}
public void send(View v){
String num = qq_num.getText().toString();
String pwd = qq_pwd.getText().toString();
if (TextUtils.isEmpty(num) ||TextUtils.isEmpty(pwd)){
Toast.makeText(this, "信息不能空!", 5).show();
return ;
}
Editor edit = sp.edit();
edit.putString("num", num);
edit.putString("pwd", pwd);
edit.commit();
Toast.makeText(this, "保存成功!", 5).show();
}
}
StringBuilder拼接成文件
如果像name为“xxx>”这样的有>的特殊字符需要转义,不然用StringBuilder产生的xml会出错。
可以用官方api :XmlSerializer生成xml文件,避免问题
public class MainActivity extends Activity {
private EditText number,name;
private RadioGroup gender;
private RadioButton female,male;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
sp = this.getSharedPreferences("config", 0);
// load();
number = (EditText) findViewById(R.id.number);
name = (EditText) findViewById(R.id.name);
gender = (RadioGroup) findViewById(R.id.gender);
female = (RadioButton) findViewById(R.id.gender_female);
male = (RadioButton) findViewById(R.id.gender_male);
}
public void alter(String info){
Toast.makeText(this, info, 4).show();
}
public void load() {
String stuName = sp.getString("name", "");
String stuNumber = sp.getString("number", "");
name.setText(stuName);
number.setText(stuNumber);
String sex = sp.getString("gender", "");
if (sex!=""){
if (sex=="男") male.setChecked(true);
else female.setChecked(true);
}
}
public void save() {
Editor edit = sp.edit();
String stuName = name.getText().toString().trim();
String stuNumber = number.getText().toString().trim();
if (TextUtils.isEmpty(stuName)|| TextUtils.isEmpty(stuNumber)){
alter("信息不能为空!");
return;
}
/*edit.putString("name", stuName);
edit.putString("number", stuNumber);*/
int ge = gender.getCheckedRadioButtonId();
String sex;
if (ge==R.id.gender_male){
sex="男";
}else{
sex="女";
}
/* edit.putString("gender", sex);
edit.commit();*/
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("<student>");
sb.append("<name>");
sb.append(stuName);
sb.append("</name>");
sb.append("<number>");
sb.append(stuNumber);
sb.append("</number>");
sb.append("gender");
sb.append(sex);
sb.append("</gender>");
sb.append("</student>");
File file = new File(this.getFilesDir(),stuName+".xml");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(sb.toString().getBytes());
fos.close();
alter("保存成功!");
} catch (IOException e) {
alter("保存失败!");
e.printStackTrace();
}
}
}
XmlSerializer生成xml文件
XmlSerializer serializer = Xml.newSerializer();
File file = new File(this.getFilesDir(),stuName+".xml");
try {
FileOutputStream fos = new FileOutputStream(file);
serializer.setOutput(fos,"utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "student");
serializer.startTag(null, "name");
serializer.text(stuName);
serializer.endTag(null, "name");
serializer.startTag(null, "number");
serializer.text(stuNumber);
serializer.endTag(null, "number");
serializer.startTag(null, "gender");
serializer.text(sex);
serializer.endTag(null, "gender");
serializer.endTag(null, "student");
serializer.endDocument();
} catch (Exception e) {
e.printStackTrace();
}