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

Android中的数据存储之SharedPreferences

使用SharedPreference时,数据的保存必须使用commit()方法,否则数据不会保存;
数据会保存在DDMS的包目录下的shared_prefs下:
Activity代码
 

public class ContentProviderActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private static final String FILENAME = "tmacsky"; 
    private TextView author = null; 
    private TextView age = null; 
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        /*SharedPreferences share = getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();//指定操作的文件名称
        editor.putString("author", "huanglong");
        editor.putInt("age", 24);
        editor.commit();*///第一段代码 
        setContentView(R.layout.main); 
        author = (TextView)findViewById(R.id.author); 
        age = (TextView)findViewById(R.id.age); 
        SharedPreferences share = getSharedPreferences(FILENAME, Activity.MODE_PRIVATE); 
        author.setText("作者: "+share.getString("author", "没有作者信息")); 
        age.setText("年龄: "+share.getInt("age", 0));//第2段代码  
    } 
} 

public class ContentProviderActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String FILENAME = "tmacsky";
    private TextView author = null;
    private TextView age = null;
   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*SharedPreferences share = getSharedPreferences(FILENAME,Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = share.edit();//指定操作的文件名称
        editor.putString("author", "huanglong");
        editor.putInt("age", 24);
        editor.commit();*///第一段代码
        setContentView(R.layout.main);
        author = (TextView)findViewById(R.id.author);
        age = (TextView)findViewById(R.id.age);
        SharedPreferences share = getSharedPreferences(FILENAME, Activity.MODE_PRIVATE);
        author.setText("作者: "+share.getString("author", "没有作者信息"));
        age.setText("年龄: "+share.getInt("age", 0));//第2段代码
    }
}

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" > 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#FFFFFF"  
        android:textSize="22px" 
        android:id="@+id/author"/> 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#FFFFFF"  
        android:textSize="22px" 
        android:id="@+id/age"/> 
</LinearLayout> 

<?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" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="22px"
        android:id="@+id/author"/>
 <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="22px"
        android:id="@+id/age"/>
</LinearLayout>

\

先用第一段代码里的内容写入数据,然后把第一段注释掉,用下面第2段代码加上main.xml来读取数据:

 

 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,