如何在Android中将图像制作为单选按钮

2022-09-04 00:54:25

在我的应用中,我想从用户那里获得调查答案。因此,我决定将笑脸图像用作单选按钮。在安卓上可能吗?

例如,当用户触摸图像时,我将显示笑脸图像,它将像单选按钮一样被激活。一次,他们只能选择一个。如果有人能指导我,我将不胜感激。

例:Like this

提前致谢!


答案 1

这个问题已经回答了,下面来自@Benito-Bertoli

RadioButton - 如何使用自定义可绘制对象?

为单选按钮指定自定义样式:

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:button">@drawable/custom_btn_radio</item>
</style>

custom_btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:state_window_focused="false"
      android:drawable="@drawable/btn_radio_on" />
   <item android:state_checked="false" android:state_window_focused="false"
      android:drawable="@drawable/btn_radio_off" />

   <item android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/btn_radio_on_pressed" />
   <item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/btn_radio_off_pressed" />

   <item android:state_checked="true" android:state_focused="true"
      android:drawable="@drawable/btn_radio_on_selected" />
   <item android:state_checked="false" android:state_focused="true"
      android:drawable="@drawable/btn_radio_off_selected" />

   <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
   <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

将可绘制的可绘制对象替换为您自己的可绘制对象。


答案 2

试试这个

  <RadioGroup
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <RadioButton
        android:button="@null"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioButton
        android:button="@null"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RadioButton
        android:button="@null"
        android:background="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RadioGroup>

输出:(边距和填充自行)

enter image description here


推荐