有这样的一个需求,RadioGroup包含若干个RadioButton。默认每个RadioButton都有点击变色的效果。但是指定某一个RadioButton没有点击变色的效果,但是依旧能接收到点击事件。先看下布局
<RadioGroup
android:id="@+id/game_count_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/home_today_game_count_rb"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/home_bet_type_bg_selector"
android:button="@null"
android:checked="true"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:textColor="@color/bet_type_text_color"
android:textSize="@dimen/sp_12"
tools:text="今日\n619" />
<RadioButton
android:id="@+id/home_roll_ball_count_rb"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/home_bet_type_bg_selector"
android:button="@null"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:textColor="@color/bet_type_text_color"
android:textSize="@dimen/sp_12"
tools:text="滚球\n619" />
<RadioButton
android:id="@+id/home_morning_trade_count_rb"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/home_bet_type_bg_selector"
android:button="@null"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:textColor="@color/bet_type_text_color"
android:textSize="@dimen/sp_12"
tools:text="早盘\n619" />
<RadioButton
android:id="@+id/home_serial_pass_count_rb"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/home_bet_type_bg_selector"
android:button="@null"
android:gravity="center"
android:lineSpacingMultiplier="1.2"
android:textColor="@color/bet_type_text_color"
android:textSize="@dimen/sp_12"
tools:text="串关\n619" />
</RadioGroup>
bet_type_text_color文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/color_white" android:state_selected="true" />
<item android:color="@color/color_white" android:state_checked="true" />
<item android:color="@color/color_655b7d" />
</selector>
home_bet_type_bg_selector文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_icon_today_bg" android:state_selected="true" />
<item android:drawable="@drawable/home_icon_today_bg" android:state_checked="true" />
<item android:drawable="@drawable/home_icon_roll_ball_bg" />
</selector>
home_icon_today_bg和home_icon_roll_ball_bg是两张图片
代码中的实现
sportRollBallCountTv.setClickable(false);
sportRollBallCountTv.setOnTouchListener((v, event) -> {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downTime=System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
if (System.currentTimeMillis()-downTime<200){
clickListener.onBetTypeViewClick(TYPE_ROLL_BALL);
}
break;
}
return true;
});
sportRollBallCountTv就是ID为home_roll_ball_count_rb的控件。点击运行可以发现,有点击事件但home_roll_ball_count_rb没有改变背景颜色