当前位置:操作系统 > 安卓/Android >>

Android开发--SharedPreferences初步介绍

    存储数据在Android开发中是一项非常重要的功能,下面介绍的这种技术是利用XML文件存储键值对。
 
     SharedPreferences的使用主要有四步:
 
           1)获得SharedPreferences对象
 
           2)获得SharedPrefercences.Editor对象
 
           3)使用putXXX方法保存数据
 
           4)将数据保存在文件中
 
下面的这个例子利用SharedPreferences实现存储简单的数据,储存的内容为test.xml,文件中的内容如下
 
 
[html]  www.zzzyk.com
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>  
<map>  
<string name="name">zhanghu</string>  
<string name="habit">android,surfing,playing basketball</string>  
</map>  
 
下面是实现的截图:
 
具体的实现代码如下:
 
 
[java] 
public class SharedPreferences_Activity extends Activity {  
    private EditText editText1,editText2,editText3,editText4;  
    private Button button1,button2;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_shared_preferences_);  
        editText1=(EditText)findViewById(R.id.editname);  
        editText2=(EditText)findViewById(R.id.edithabbit);  
        editText3=(EditText)findViewById(R.id.editname2);  
        editText4=(EditText)findViewById(R.id.edithabbit2);  
        button1=(Button)findViewById(R.id.commit);  
        button2=(Button)findViewById(R.id.display);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                //获得SharedPreferences对象(第一步)  
                SharedPreferences mySharedPreferences=getSharedPreferences("test", Activity.MODE_PRIVATE);  
                //获得SharedPrefercences.Editor对象(第二步)  
                SharedPreferences.Editor editor=mySharedPreferences.edit();  
                //使用putXXX方法保存数据(第三步)  
                editor.putString("name", editText1.getText().toString());  
                editor.putString("habit", editText2.getText().toString());  
                //将数据保存在文件中(第四步)  
                editor.commit();  
                editText1.setText("");  
                editText2.setText("");  
            }  
        });  
        button2.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
            SharedPreferences sharedPreferences=getSharedPreferences("test", Activity.MODE_PRIVATE);  
            String nameString=sharedPreferences.getString("name", "");  
            String habitString=sharedPreferences.getString("habit", "");  
            editText3.setText(nameString);  
            editText4.setText(habitString);  
            }  
        });  
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_shared_preferences_, menu);  
        return true;  
    }  
  
}  
 
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,