关于selector和shape的简单例子

刚才想自定义一个彩票的球,于是想通过textView来搞定,不过发现textView只能点击切换颜色,而不能像CheckBox那样有选中状态。所以想到设置textView的setSelected()属性来做。

下面放上一些代码

</pre><p>1.定义的背景的状态选择器</p><p><pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  
    <item android:state_selected="true">
        <shape android:shape="oval" android:useLevel="false">
            <solid android:color="#ff0000" />

            <stroke android:width="2dp" />
        </shape></item>
    <item android:state_selected="false"><shape android:shape="oval" android:useLevel="false">
            <solid android:color="#fff" />

            <stroke android:width="2dp" />
             />
</shape></item>

</selector>

2.定义的文字颜色变化的选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true" android:color="#FFFfff"/>
    <item android:state_selected="true" android:color="#FFFfff"></item>
    <item android:state_pressed="true" android:color="#FFFfff"></item>
    <item android:color="#FF0000"></item>

</selector>

3.在xml文件中使用

 <com.example.timer_shaft.ViewBall
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/bg_ball_selector"
        android:gravity="center"
        android:text="55"
        android:textColor="@drawable/text_ball_selector" />
4.我是自己定义的TextView因为不想再每个地方都通过点击事件来设置textView的选中状态,这么写报警告了,还没了解为啥,有知道朋友的求教育

@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			this.setSelected(!this.isSelected());
			break;
		}
		return super.onTouchEvent(event);
	}
这样基本就完成了一个彩票的球的效果了





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值