自定义控件

6 篇文章 0 订阅
6 篇文章 0 订阅

1.      新建com.example.demo.ui.SwitchUi类

public class SwitchUi extends RelativeLayout {
		private TextView tv_tilte;
		private TextView tv_desc;
		private CheckBox cb_option;
		private String desc_on;
		private String desc_off;
		private String title;
		SwitchUi (Context context, AttributeSet attrs){
			View v=View.inflate(context, R.layout.ui_switch, this);
			tv_title=v.findViewById(R.id.tv_title);
			tv_desc=v.findViewById(R.id.tv_desc);
			cb_option=v.findViewById(R.id.cb_option);
			title=attrs.getResourceValue(“com.example.demo.ui.SwitchUi”, “title”);
			desc_on=attrs.getResourceValue(“com.example.demo.ui.SwitchUi”, “desc_on”);
			desc_off=attrs.getResourceValue(“com.example.demo.ui.SwitchUi”, “desc_off”);
			tv_title.setText(title);
			tv_desc.setText(desc_off);
			return v;
}
private boolean isChecked(){
	return cb_option.isChecked();
}
		private boolean setChecked(boolean b){
	if (b){
		tv_desc.setText(desc_on);
		cb_option.setChecked(true);
} else {
	tv_desc.setText(desc_off);
	cb_option.setChecked(false);
}
}
}

2.res/layout内新建ui_switch.xml文件

<RelativeLayout
android:layout_height=”wrap_content”
android:layout_width=”match_parent>
		<TextView
			android:layout_id=”@+id/tv_title”
			android:layout_marginTop=”5dip”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
android:layout_textSize=”20sp”
android:layout_textColor=”#000000”/>

		<TextView
			android:layout_id=”@+id/tv_desc”
			android:layout_marginTop=”2dip”
			android:layout_below=”@id=tv_title”
android:layout_height=”wrap_content”
android:layout_width=”match_content”
android:layout_textSize=”14sp”
android:layout_textColor=”#99880000”/>

		<CheckBox
			android:layout_align_bottom_to_parent=”true”
			android:layout_align_right_to_parent=”true”
			android:layout_id=”@+id=cb_option”
android:layout_height=”wrap_content”
android:layout_width=”wrap_content”
/>
</RelativeLayout>

3. res/values内新建attrs.xml

<resources>
		<declare-styleable>
			<attr name=”SwitchUi”>
				< item name=”desc_on” type=”string”/>
				< item name=”desc_off” type=”string”/>
				< item name=”title” type=”string”/>
			</attr>
		</declare-styleable>
</resources>

4.新建com.examlpe.demo.activities.MainActivity

public class MainActivity extends activity extends OnClickListener{
		private SwitchUi su_light;
		private SwitchUi su_computer;
oncreat(){
			setContentView(R.layout.activity_main);
			su_light=findViewById(R.id.su_light);
			su_computer=findViewById(R.id.su_computer);
}
@override
OnClick(View v){
	switch(v.getId()){
	case R.id.su_light:
		if (su_light.isChecked()){
			su_light.setChecked(false);
} else {
	su_light.setChecked(true);
}
			break;
			case R.id.su_computer:
				if (su_computer.isChecked()){
					su_computer.setChecked(false);
} else {
	su_computer.setChecked(true);
}
			break;
}
}
}

5.  layout中新建activity_main.xml

<xmlns:example=android….res/com.example.demo>
<LinearLayout>
		<com.example.demo.ui.SwitchUi
			android:layout_id=”@+id/su_light”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
example:title=”this is a light”
example:desc_on=”light on !”
example:desc_off=”light off!”>
		</com.example.demo.ui.SwitchUi>
		<com.example.demo.ui.SwitchUi
			android:layout_id=”@+id/su_computer”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”
example:title=”this is a computer”
example:desc_on=”computer start !”
example:desc_off=”computer poweroff !”>
		</com.example.demo.ui.SwitchUi>
</LinearLayout>

6.manifest中配置

<activity name=”com.example.demo.MainActivity”></activity>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值