Android开发->SharedPreferences存储数据

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();
    }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值