Sharedpreference(参数)保存文件

Activity代码:

 1 package parameterpackage.namespace;
 2 
 3 import java.util.jar.Attributes.Name;
 4 import android.R.string;
 5 import android.app.Activity;
 6 import android.content.Context;
 7 import android.content.SharedPreferences;
 8 import android.content.SharedPreferences.Editor;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.Button;
12 import android.widget.EditText;
13 import android.widget.Toast;
14 public class ParameterActivity extends Activity {
15     /** Called when the activity is first created. */
16     public EditText textname;
17     public EditText textage;
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.main);
22         Button buttonsave =(Button) findViewById(R.id.buttonsave);//保存按钮
23         Button buttonrecover =(Button) findViewById(R.id.buttonrecover);//读取按钮
24         textname =(EditText) findViewById(R.id.editTextname);
25         textage=(EditText)findViewById(R.id.editTextage);
26         buttonsave.setOnClickListener(new View.OnClickListener()//保存 
27         {        
28             @Override
29             public void onClick(View v) {
30                 // TODO Auto-generated method stub
31                 String entername=textname.getText().toString();
32             int enterage = Integer.parseInt(textage.getText().toString());
33                   // String enterage =textage.getText().toString();
34                 SharedPreferences preferenes= getSharedPreferences("preferaname", Context.MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);
35                 //getSharedPreferences以xml格式存放文件的,括号解释(name 是保存文件时的标题名字,权限模式)
36                 Editor editor=preferenes.edit();//申请一个编辑对象
37                 editor.putString("name", entername);//括号解释(“名称/key”,内容)现在的文件还只是放到内存中
38                 editor.putInt("Age", enterage);
39                 editor.commit();;//把编辑的内容统一提交到上面的 prename 中
40                 Toast.makeText(ParameterActivity.this, "succeed", 1).show();
41             }
42         });
43         
44         buttonrecover.setOnClickListener(new View.OnClickListener()//读取 
45         {
46             @Override
47             public void onClick(View v) {
48                 // TODO Auto-generated method stub
49                 //textname =(EditText) findViewById(R.id.editTextname);
50                 //textage=(EditText)findViewById(R.id.editTextage);
51                   SharedPreferences preferences= getSharedPreferences("preferaname",MODE_PRIVATE);
52                   String name=preferences.getString("name", "NULL");//括号内解释(“名字/key,默认值”如果可以不存在返回默认值)
53                   String age=String.valueOf(preferences.getInt("age", 0));//注意括号内的类型,默认值要求是int
54                   textname.setText(name);
55                   textage.setText(age);
56                 Toast.makeText(ParameterActivity.this, "Recoversucceed", 1).show();
57             }
58         });
59     }
60 }

保存成功后,导出的文件:

1 <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
2 <map>
3 <int name="Age" value="0" />
4 <string name="name">123</string>
5 </map>

 

posted @ 2012-12-13 20:25  1小松  阅读(420)  评论(0编辑  收藏  举报