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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38266387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:16:29  来源:igfitidea点击:

How to make image as a radio button in android

javaandroid

提问by hikoo

In my app, I want to get survey answers from the user. So, I've decided put smiley images to act as a radio buttons. Is it possible on android?

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

For example, I will show smiley images when user touches the image and it will be activated like a radio button. At a time, they will be allowed to only choose one. If anyone could guide me, I would appreciate it.

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

Example :Like this

例子 :像这样

Thanks in advance!

提前致谢!

回答by BR89

This question has been answered before Below is from @Benito-Bertoli

这个问题之前已经回答过以下来自@Benito-Bertoli

RadioButton - how to use a custom drawable?

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

Give your radiobutton a custom style:

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

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

custom_btn_radio.xml

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>

Replace the drawables with your own.

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

回答by Cgx

Try like this

像这样尝试

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

output:(margin and pading by yourself)

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

enter image description here

在此处输入图片说明

回答by ENOENT

I might be a bit late. Use android:button"@btmImage link"

我可能有点晚了。利用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" />

回答by Rohan Sood

I think these 3rd party libraries will help you achieve this functionality.

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

  1. SimpleRatingBar

  2. SmileBar

  1. 简单评级栏

  2. 微笑条

They are pretty easy to use also.

它们也很容易使用。

回答by Morgan Koh

Actually you just need to handle these two states, and it will work already.

实际上你只需要处理这两个状态,它就已经可以工作了。

<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

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>