注:源自传智播客视频教程
现整理此案例,以供日后自己或大家学习、参考:
自定义组合控件demo
实现效果:
1.点击组合控件任何一处触发点击事件改变checkbox的选中状态
下载链接:
http://download.csdn.net/detail/wang725/8735039
1.新建一个组合控件对应的ui:SettingComponentView.java
package com.example.selfDefine;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class SettingComponentView extends RelativeLayout {
private TextView etTitle;
private TextView etDescr;
private CheckBox cbChoice;
/*自定义的属性*/
private String title1;
private String descrOn;
private String descrOff;
public SettingComponentView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public SettingComponentView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
title1 = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.selfDefine", "title1");
descrOn = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.selfDefine", "descr_on");
descrOff = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.example.selfDefine", "descr_off");
etTitle.setText(title1);
}
public SettingComponentView(Context context) {
super(context);
/*初始化*/
initView(context);
}
/**
* 初始化
* @param context
*/
private void initView(Context context) {
View view = View.inflate(context, R.layout.setting_componet_view, this);
etTitle = (TextView) view.findViewById(R.id.tv_title);
etDescr = (TextView) view.findViewById(R.id.tv_descr);
cbChoice = (CheckBox) view.findViewById(R.id.cb_choice);
}
/**
* 组合控件是否选中
* @return
*/
public boolean isChoice() {
return cbChoice.isChecked();
}
/**
* 设置组合控件的状态
*/
public void setChoice(boolean checked) {
if(checked) {
setDescr(descrOn);
} else {
setDescr(descrOff);
}
cbChoice.setChecked(checked);
}
/**
* 设置组合控件描述信息
*/
public void setDescr(String descr) {
etDescr.setText(descr);
}
}
2.布局activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:itheima="http://schemas.android.com/apk/res/com.example.selfDefine"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.selfDefine.SettingComponentView
itheima:title1="能坚持吗?"
itheima:descr_on="坚持坚持继续坚持"
itheima:descr_off="勾选去坚持兄弟"
android:id="@+id/scv_setting_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
3.组合控件item布局setting_componet_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip" >
<TextView
android:id="@+id/tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="能坚持吗?"
android:textSize="20sp"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dip"/>
<TextView
android:id="@+id/tv_descr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="勾选去坚持兄弟"
android:textSize="16sp"
android:textColor="#88000000"
android:layout_marginLeft="5dip"
android:layout_below="@+id/tv_title"/>
<CheckBox
android:id="@+id/cb_choice"
android:clickable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dip"/>
<View
android:id="@+id/v_line"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#55000000"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"/>
</RelativeLayout>
4.属性 attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TextView">
<attr name="title1" format="string"/>
<attr name="descr_on" format="string"/>
<attr name="descr_off" format="string"/>
</declare-styleable>
</resources>
5.MainActivity.java
package com.example.selfDefine;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity {
private SettingComponentView scvItem;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scvItem = (SettingComponentView) findViewById(R.id.scv_setting_item);
sp = getSharedPreferences("config", MODE_PRIVATE);
/* 取出保存的勾选状态,若已选择,则一打开就使checkbox为已勾选状态,并显示对应的描述*/
boolean isChoice = sp.getBoolean("isChoice", false);
if(isChoice) {
scvItem.setChoice(true);
} else {
scvItem.setChoice(false);
}
/*组合控件的点击事件*/
scvItem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor editor = sp.edit();
if(scvItem.isChoice()) {
scvItem.setChoice(false);
editor.putBoolean("isChoice", false);
} else {
scvItem.setChoice(true);
editor.putBoolean("isChoice", true);
}
editor.commit();
}
});
}
}