android的Setting往往用PreferenceActivity来写的
我们在建立layout文件:
?
类的代码如下:
?
?
public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//这个是给Settings加自定义Title
final boolean isCustom = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
if(isCustom){
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_list);
}
TextView title_text = (TextView)findViewById(R.id.title_text);
title_text.setText(Settings);
Button back = (Button)findViewById(R.id.back);
back.setVisibility(View.VISIBLE);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
addPreferencesFromResource(R.xml.seting_preferences);
}
}
定义themes,把这个activity的theme设置成以下的样子
?
?
?
?
取值的时候可以这样做:?
SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this); String s = shp.getString(list, null); //这个是取ListPreference TextView listData = (TextView)findViewById(R.id.listData); listData.setText(s); HashSet set = (HashSet) shp.getStringSet(mutiSelect, null); //这个是取MultiSelectListPreference的值 Iteratorit = set.iterator(); String content = ; while(it.hasNext()) { content += it.next()+,; } TextView muti_select_data = (TextView)findViewById(R.id.muti_select_data); muti_select_data.setText(content);
截图是:

?