自定义单选框选中(只能选中一个)

<script type="text/javascript">
        $(function(){var checked = document.getElementsByClassName('J_checked')
             for (var i = 0; i < checked.length; i++) {
                 checked[i].onclick = function(){
                   for (var i = 0; i < checked.length; i++) {
                       checked[i].style.background = 'url(./商品详情/shopping_xuanze@2x.png) no-repeat';
                       checked[i].style.backgroundSize = '100%';
                   }
                this.style.background = 'url(./商品详情/shopping_xuanze_click_img@2x.png';
                this.style.backgroundSize = '100%';
                 }
             }
        });
    </script>

 

转载于:https://www.cnblogs.com/chenmiaosong/p/8327688.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义一个单选框,可以继承自 Android 的 View 类,并实现一些必要的方法。下面是一个简单的例子: ```java public class MyRadioButton extends View { private boolean isChecked = false; private Paint paint = new Paint(); public MyRadioButton(Context context) { super(context); init(); } public MyRadioButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MyRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { // 设置画笔的颜色和样式 paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 设置 view 的大小,这里设置为 50x50 setMeasuredDimension(50, 50); } @Override protected void onDraw(Canvas canvas) { // 画一个圆形 canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 5, paint); // 如果被选中,画一个小圆点 if (isChecked) { canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 4, paint); } } @Override public boolean onTouchEvent(MotionEvent event) { // 监听点击事件,如果被点击,就改变选中状态 if (event.getAction() == MotionEvent.ACTION_DOWN) { isChecked = !isChecked; invalidate(); } return super.onTouchEvent(event); } } ``` 这个自定义单选框只是一个简单的例子,可以根据需要添加更多的功能。要使用这个自定义单选框,只需要在布局文件中添加一个 MyRadioButton 即可: ```xml <com.example.myapplication.MyRadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值