Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中的數據存儲之SharedPreferences

Android中的數據存儲之SharedPreferences

編輯:關於Android編程

使用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來讀取數據:

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved