【问题标题】:How to make image as a radio button in android如何在android中将图像制作为单选按钮
【发布时间】:2020-03-18 17:50:22
【问题描述】:

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

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

例如:

提前致谢!

【问题讨论】:

标签: java android


【解决方案1】:

这个问题之前已经回答过下面来自@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>

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>

用你自己的替换drawables。

【讨论】:

    【解决方案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>
    

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

    【讨论】:

      【解决方案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" />
      

      【讨论】:

        【解决方案4】:

        我认为这些 3rd 方库将帮助您实现此功能。

        1. SimpleRatingBar

        2. SmileBar

        它们也很容易使用。

        【讨论】:

          【解决方案5】:

          其实你只需要处理这两种状态,就已经可以了。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-09-25
            • 2013-09-03
            • 1970-01-01
            • 2015-11-04
            • 2017-08-22
            相关资源
            最近更新 更多