PreferenceActivity基本用法


/*PreferenceActivity继承ListActivity负责用list表示各个preferences项,
  通过Preference类配置view和点击后的行为,负责首选项的界面与持久化功能*/
public class WeiboPreferenceActivity extends PreferenceActivity{
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 this.addPreferencesFromResource(R.xml.preferences);//通过加载preferences文件加载list的每个项
		ClickPreference clickPreference = (ClickPreference) this.findPreference("friend_add");//在代码中能通过key得到preference的实例

	}
	
}
/*用来描述放入preferenceActivity的每一个项,都是Preference子类
*/
R.xml.preferences
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:test="http://schemas.android.com/apk/res/com.weitian">//自定义命名空间
  <!--custom name space:test:跟着declare-styleable定义的属性-->
    <com.weitian.preference.ClickPreference//自定义Preference
      android:key="friend_add"
      android:title="@string/preferences_friend_add_title"
      android:summary="@string/preferences_friend_add_summary"
      test:textSize="20px"  //自定义属性
      test:textColor="#000"/>
    <com.weitian.preference.ClickPreference
      android:key="friend_requests"
      android:title="@string/preferences_friend_requests_title"
      android:summary="@string/preferences_friend_requests_summary" />
</PreferenceScreen>
/*用来自定义属性*/
<?xml version="1.0" encoding="utf-8"?>  
<!--为ClickPreference custom attribute-->
<resources>  
    <declare-styleable name="MyView">//自定义属性所匹配的资源名  
        <attr name="textColor" format="color" />  //自定义属性所匹配值的格式
        <attr name="textSize" format="dimension" />  
    </declare-styleable>  
</resources> 

/*实现单个preference项的显示与持久化功能,例如当用户点击后可以实现创建对话框让用户选择之后持久化等操作*/
public class ClickPreference extends Preference{


	private LayoutInflater layoutInflater = null;
	private int textColor;
	private float textSize;
	
	public ClickPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		
		/*怎么读取自定义的属性*/
		/*当view加载了自定义属性通过以下方式可以引用自定义属性*/
		TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);//检索理想的属性
		textColor = a.getColor(R.styleable.MyView_textColor,0XFF00FFFF);//通过系统自动生成的MyView_textColor名称引用属性值  
		textSize = a.getDimension(R.styleable.MyView_textSize, 36);
		a.recycle(); 
		
		
		//自定义每个项的视图布局
		setLayoutResource(R.layout.click_preference);
	}
	
	
	
	@Override
	public void setLayoutResource(int layoutResId) {
		// TODO Auto-generated method stub
		super.setLayoutResource(layoutResId);
	}






	@Override
	protected View onCreateView(ViewGroup parent) {
		// TODO Auto-generated method stub
		return super.onCreateView(parent);
	}


	


	
    @Override
    protected void onBindView(View view) {
    	view.setBackgroundColor(this.textColor);
        super.onBindView(view);
    }


    @Override
    protected void onClick() {
        // Data has changed, notify so UI can be refreshed!
        notifyChanged();
    }


   
    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        // This preference type's value type is Integer, so we read the default
        // value from the attributes as an Integer.
        return a.getInteger(index, 0);
    }


    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    }


    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        super.onRestoreInstanceState(state);
        notifyChanged();
    }
}

你可能感兴趣的:(PreferenceActivity基本用法)