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

15

在我的应用程序中,我希望从用户那里获取调查答案。因此,我决定放置笑脸图片作为单选按钮。在Android上是否可行?

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

示例:类似这样

提前感谢!


2
这可能会对您有所帮助:https://dev59.com/Tmct5IYBdhLWcg3wvf_K - adalpari
5个回答

10

这个问题之前已经有答案了,下面是来自 @Benito-Bertoli 的回答:

RadioButton - how to use a custom drawable?

给你的单选按钮应用一个自定义样式:

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

自定义按钮单选框.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>

用你自己的可绘制对象替换这些。


8

试试这样做

  <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>

输出:(边距和内边距由您自行处理)

在此输入图片描述


3
实际上,您只需要处理这两种状态,就可以正常工作了。
<RadioButton
   android:id="@+id/paragraphRadioButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:button="@drawable/btn_paragraph"
   android:padding="14dp" />

btn_paragraph.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_paragraph_selected" android:state_checked="true" />
    <item android:drawable="@drawable/ic_paragraph" android:state_checked="false" />
</selector>

3
我可能有点晚了。使用android:button"@btmImage link"
<RadioButton
                    android:id="@+id/radio0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:button="@drawable/ic_launcher"
                    android:text="male"
                    android:textColor="#90999d" />

2
我认为这些第三方库可以帮助您实现此功能。
  1. SimpleRatingBar

  2. SmileBar

它们也非常易于使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接