点击效果、多个按钮单选效果(RadioButton)

目录

图片点击变暗效果

textview选中效果

多个按钮,单选效果


图片点击变暗效果

xml中直接调用<ClickImageView />

public class ClickImageView extends AppCompatImageView {

    public ClickImageView(@NonNull Context context) {
        super(context);
    }

    public ClickImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ClickImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                this.setColorFilter(0x99000000);
                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                this.setColorFilter(null);
                break;
        }
        return super.onTouchEvent(event);
    }

}

textview选中效果

// textview选中
tvDanger.setSelected(true);
// textview未选中
tvDanger.setSelected(false);

多个按钮,单选效果

效果图:

代码:

bg_btn_left:

btn_text_color:

<RadioGroup
    android:id="@+id/radioGroupHw"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/rdoHwDay30"
        android:layout_width="65dp"
        android:layout_height="35dp"
        android:background="@drawable/bg_btn_left"
        android:button="@null"
        android:checked="true"
        android:gravity="center"
        android:text="@string/day30"
        android:textColor="@drawable/btn_text_color" />

    <RadioButton
        android:id="@+id/rdoHwDay7"
        android:layout_width="65dp"
        android:layout_height="35dp"
        android:layout_marginStart="-1dp"
        android:background="@drawable/bg_btn_center"
        android:button="@null"
        android:checked="false"
        android:gravity="center"
        android:text="@string/day7"
        android:textColor="@drawable/btn_text_color" />

    <RadioButton
        android:id="@+id/rdoHwDay1"
        android:layout_width="65dp"
        android:layout_height="35dp"
        android:layout_marginStart="-1dp"
        android:background="@drawable/bg_btn_right"
        android:button="@null"
        android:checked="false"
        android:gravity="center"
        android:text="@string/day1"
        android:textColor="@drawable/btn_text_color" />
</RadioGroup>
注:RadioGroup、RadioButton要设置id,否则单选效果不生效。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值