<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">checkBox组件 天生有焦点 所以无法通过onclickListener监听 </span>
所以可以设置
android:focusable="false"
android:clickable="false"
来取消checkbox的功能
通过监听其他组件 来改变checkbox的状态
自定义控件
1.把自定义组件 写在一个布局文件中
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<<strong>RelativeLayout</strong> xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dip" >
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="8dip"
android:text="设置是否自动更新"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_title"
android:layout_marginLeft="10dip"
android:text="自动更新已经关闭"
android:textColor="@android:color/darker_gray"
android:textSize="18dp" />
<!-- 因为checkBox天生有焦点 无法通过改变焦点的形式来更改内容 所以设置为 没有焦点 不可点击 通过监听整个相对布局
来改变checkbox的checked状态
-->
<CheckBox
android:focusable="false"
android:clickable="false"
android:id="@+id/cb_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:gravity="center_vertical" />
<View
android:layout_width="wrap_content"
android:layout_height="0.2dip"
android:layout_alignParentBottom="true"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:background="#000000" />
</RelativeLayout></span>
<strong style="font-size:14px; font-family: Arial, Helvetica, sans-serif;">2.写一个自定义类 继承相应的布局类</strong>
<span style="font-family:Arial, Helvetica, sans-serif;font-size:14px;"><strong>把先前写好的布局文件 转化成一个View</strong></span>
public class SettingItemView extends <strong>RelativeLayout</strong> {
private CheckBox cb_status;
private TextView tv_title;
private TextView tv_desc;
/**
* 初始化布局文件
* @param context
*/
private void initView(Context context) {
// <strong>把一个布局文件转化成View 并加载在this对象中</strong>
<strong>View.inflate(context, R.layout.setting_item_view, this);</strong>
// 因为已经把setting_item_view加载到SettingItemView中 所以可以用this.find....
cb_status = (CheckBox) this.findViewById(R.id.cb_status);
tv_title = (TextView) this.findViewById(R.id.tv_title);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
}
public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public SettingItemView(Context context) {
super(context);
initView(context);
}
}
3.在界面布局文件中引用
<span style="font-size:14px;"><<strong>com.DLJ.mobilesafe.ui.SettingItemView ///写全包名</strong>
android:id="@+id/siv_update"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></span>
自定义组合控件的过程
1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup;
2.实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件;
3.根据一些需要或者需求,定义一些API方法;
----------------------------------
4.根据需要,自定义控件的属性,可以参照TextView属性;
5.自定义命名空间,例如:
xmlns:itheima="http://schemas.android.com/apk/res/《包名》"
xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"
6.自定义我们的属性,在Res/values/attrs.xml
//系统会根据我们的命名空间去找读取attrs.xml文件
可以参照sdk的attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>
7.使用我们自定义的属性
例如:
itheima:title="设置自动更新"
itheima:desc_on="设置自动更新已经开启"
itheima:desc_off="设置自动更新已经关闭"
8.在我们自定义控件的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义布局文件对应的控件;