SharedPreferences & Preference XML 之间的羁绊(一)

Quick Start

/**
 * Main Activity
 */
class DemoActivity extends Activtiy {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SharedPreferences prefs = getSharedPreferences("net.oschina.demo_prefs", MODE_PRIVATE);
        ...
    }
}
/**
 * Preference Activity
 */
public class DemoPreferenceActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getPreferenceManager().setSharedPreferencesName("net.oschina.demo_prefs");
        getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE);
        addPreferencesFromResource(R.xml.demo_prefs);
    }
}


上述代码中, 将 SharedPreferences 和 xml 建立联系的语句调用就是 getPreferenceManager().setSharedPreferencesName(String), 而 Prefs Mode 则是通过getPreferenceManager().setSharedPreferencesMode(int) 来设定的.



你可能感兴趣的:(android,xml,Preference)