目录
图片点击变暗效果
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,否则单选效果不生效。