Android数据存储之Shared Preferences

1.编辑strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Data_SharedPreferencesActivity!</string>
    <string name="app_name">Data_SharedPreferences</string>
    <string name="tvName">姓名</string>
    <string name="tvAge">年龄</string>
    <string name="btSet">设置</string>
    <string name="btRead">读取</string>
    <string name="saveSuccess">保存成功</string>
    <string name="saveFiled">保存失败</string>
</resources>

2.编辑main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

        //姓名
     	<RelativeLayout  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	    <TextView
	        android:id="@+id/tvName" 
	        android:layout_width="25pt" 
	        android:layout_height="wrap_content"
	        android:text="@string/tvName"/>
	        
	    <EditText
	        android:id="@+id/etName" 
	        android:layout_width="300pt" 
	        android:layout_height="wrap_content" 
	        android:inputType="text"
	        android:layout_toRightOf="@id/tvName"/>
	</RelativeLayout> 
    
	 
      //年龄
      <RelativeLayout  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	    <TextView
	        android:id="@+id/tvAge" 
	        android:layout_width="25pt" 
	        android:layout_height="wrap_content"
	        android:text="@string/tvAge"/>
	        
	    <EditText
	        android:id="@+id/etAge" 
	        android:layout_width="300pt" 
	        android:layout_height="wrap_content" 
	        android:inputType="text"
	        android:layout_toRightOf="@id/tvAge"/>
               //etAge位于tvAge的右边   	        
	</RelativeLayout> 

       //按钮	
        <RelativeLayout  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content">
	    <Button
	        android:id="@+id/btSet" 
	        android:layout_width="50pt" 
	        android:layout_height="wrap_content"
	        android:text="@string/btSet"/>
	        
	    <Button
	        android:id="@+id/btRead" 
	        android:layout_width="50pt" 
	        android:layout_height="wrap_content" 
	        android:layout_toRightOf="@id/btSet"
	       android:text="@string/btRead"/>
	</RelativeLayout> 


</LinearLayout>

3.编辑Data_SharedPreferencesActivity.java

package wei.cao.data;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Data_SharedPreferencesActivity extends Activity {

	private EditText etName;
	private EditText etAge;
	private Button btRead;
	private Button btSet;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //获取按钮和编辑框
        etName=(EditText)this.findViewById(R.id.etName);
        etAge=(EditText)this.findViewById(R.id.etAge);
        btRead=(Button)this.findViewById(R.id.btRead);
        btSet=(Button)this.findViewById(R.id.btSet);
        
         //添加事件
        btSet.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//获取输入的姓名和年龄
				String name=etName.getText().toString();
				String age=etAge.getText().toString();
				
				//取得活动的Preferences对象
				SharedPreferences sp=getPreferences(Activity.MODE_PRIVATE);
				
				//取得编辑对象
				Editor editor=sp.edit();
				
				//添加值
				editor.putString("name", name);
				editor.putInt("age", Integer.parseInt(age));
				
                                //保存数据								
                                if(editor.commit())
				{
					Toast.makeText(Data_SharedPreferencesActivity.this, R.string.saveSuccess, 1).show();
				}
				else
				{
					Toast.makeText(Data_SharedPreferencesActivity.this, R.string.saveFiled, 1).show();
				}
			}
		});
        
        btRead.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				//创建SharedPreferences对象
				SharedPreferences sp=getPreferences(MODE_PRIVATE);
				
				//获取数据(第二个参数代表,如果没有找到name,那么name的值就为defaultName,)
				//就是把name改为na,那么name的值就是defaultName
				String name=sp.getString("name", "defaultName");
				String age=sp.getInt("age", 0)+"";
				
				//显示数据
				String result="name:"+name+"===age:"+age;
				Toast.makeText(Data_SharedPreferencesActivity.this, result, 1).show();
			}
		});
        
        
    }
}

4.执行结果:


5.打开DDMS的File Explore 在data/data/wei.cao.data会产生一个文件,
导出后的结果为:


<?xml version='1.0' encoding='utf-8' standalone='yes' ?><map><string name="name">qwe</string><int name="age" value="12" /></map>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值