首页 > 代码库 > 关于PreferenceActivity的使用和一些问题的解决(自定义Title和取值)

关于PreferenceActivity的使用和一些问题的解决(自定义Title和取值)

android的Setting往往用PreferenceActivity来写的

我们在建立layout文件:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="常规设置" android:key="set_local">
	<CheckBoxPreference
            android:key="new_message_notify"
            android:title="新消息通知"
            android:defaultValue=http://www.mamicode.com/"true">
类的代码如下:

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设置成以下的样子

    <style name="AppTheme" parent="AppBaseTheme">  
  
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->  
        <!-- 避免重复设置标题 -->  
        <item name="android:windowActionBar">false</item>  
        <!-- 设置标题栏宽度 -->  
        <item name="android:windowTitleSize">60dp</item>   //titlebar的高度
        <!-- <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> -->  
    </style>


取值的时候可以这样做:

SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this);
		String s = shp.getString("list", null);  //这个是取ListPreference
		TextView listData = http://www.mamicode.com/(TextView)findViewById(R.id.listData);>

截图是: