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

hik*_*koo 8 java android

在我的应用程序中,我想从用户那里获得调查答案.所以,我决定将笑脸图像作为单选按钮.在Android上有可能吗?

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

示例:像这样

提前致谢!

小智 7

像这样尝试

  <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>
Run Code Online (Sandbox Code Playgroud)

输出:(你自己的边距和填充)

在此处输入图片说明


BR8*_*R89 6

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

RadioButton-如何使用自定义绘图?

给您的单选按钮自定义样式:

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
    <item name="android:button">@drawable/custom_btn_radio</item>
</style>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

用您自己的绘画替换。