android中的radiobutton要点击2下才能选中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android中的radiobutton要点击2下才能选中相关的知识,希望对你有一定的参考价值。

背景选中器设置了,press的时候会变背景,check的时候也会变背景,我看到radiobutton确实是点击了,但是没有check,我的radiobutton是跟radiogroup一起用的,求高人解释为什么,解决办法

android 中的radiobutton点击2下才能选中的原因,因为一开始都有一个默认选中的item,类似于下面的代码:


for (int j = 0; j < newList.get(position).getList().size(); j++) 
RadioButton radioButton = new RadioButton(context);
radioButton.setTextSize(9);  
radioButton.setText(newList.get(position).getList().get(j)
.get("dishname").toString());
radioButton.setTag(newList.get(position).getList().get(j)
.get("dishid").toString());
radioGroup.addView(radioButton, j);

if (j==0) 
radioButton.setCheck(true); 
  

就是中给radioButton设置为选中,这样如果存在两个radiogroup就会出现点击两次才能选中的状态原因是:不需要设置RadioButton的默认选中, 这样会使RadioButton一直处于选中状态. 

解决的方式:

应该给RadioGroup 设置选中的RadioButton   ,也就是说    

把  if (j==0)
 radioButton.setCheck(true); 
 

更改为

 if (j==0)
radioGroup.check(radioButton.getId()); 
 


遇到很多棘手的问题,建议查看api和接口方法的使用说明,很多官方的英文详细的列举了可能发生的问题。

参考技术A 切换到哪一个的时候设置((RadioButton) findViewById(R.id.radiobutton)).setChecked(true);你试试看,或者判断一下
switch (radiogroup.getCheckedRadioButtonId())
case R.id.radio1:
if (radioButton1.isChecked())
radioGroup2.clearCheck();
break;
case R.id.radio2:
if (radioButton2.isChecked())
radioGroup1.clearCheck();
break;
追问

试过了,没有用。。

追答

我也在做那个,没有出现你说的问题,你可以把代码贴出来

追问

试过了,没有用。。

关键是点击radiobutton他没有check,只是press了,点击第2下才check,我就算每一个button都设置clickListener也是点击第2下才会触发Listener

 

追答

你是想两个按钮切换的时候执行不同的操作?那两个按钮是不需要加监听的,我写了个demo,你可以参考下

本回答被提问者采纳
参考技术B 切换到哪一个的时候设置((RadioButton) findViewById(R.id.radiobutton)).setChecked(true);你试试看,或者判断一下
switch (radiogroup.getCheckedRadioButtonId())
case R.id.radio1:
if (radioButton1.isChecked())
radioGroup2.clearCheck();
break;
case R.id.radio2:
if (radioButton2.isChecked())
radioGroup1.clearCheck();
break;
参考技术C 切换到哪一个的时候设置((RadioButton) findViewById(R.id.radiobutton)).setChecked(true);你试试看,或者判断一下
switch (radiogroup.getCheckedRadioButtonId())
case R.id.radio1:
if (radioButton1.isChecked())
radioGroup2.clearCheck();
break;
case R.id.radio2:
if (radioButton2.isChecked())
radioGroup1.clearCheck();
break;
参考技术D Android是基于Linux内核开发的操作系统。主要面向手机及手持移动设备。

单击即指点中或触碰某程序或软件,单击后将会触发该程序运行。

而长按软件可以实现选中,单出可以选择的菜单。
对文件或者程序进行操作,例如:移动、删除、拖拽等····
第5个回答  2016-01-06 点击1下就可以,点击2下可能是你没点好。

Windows 窗体 RadioButton 控件为用户提供由两个或多个互斥选项组成的选项集。android中的RadioButton 也是一个意识,只不过需要使用一个RadioGroup包含起来,代表一组。
第6个回答  2016-03-30 看看监听器的设置是否有问题,单个RadioButton在选中后,通过点击无法变为未选中

Android:如何更改 RadioButton 的大小

【中文标题】Android:如何更改 RadioButton 的大小【英文标题】:Android: How to change the Size of the RadioButton 【发布时间】:2011-03-02 04:07:33 【问题描述】:

我的应用中有很多RadioButtons。 RadioButtons 对我来说太大了。有没有办法让它们变小?

【问题讨论】:

我想您可以编写自己的控件,但请记住,控件太小可能会使某些用户在触摸模式下更难准确地点击控件。这会降低您应用的可用性。 我找到了这个解决方案,在这里查看我的答案https://***.com/a/44310577/6112256 【参考方案1】:

有另一种方法可以更改大小。 从矢量资产中选中和取消选中导入单选按钮。

创建三个可绘制对象

radio_button_check

指定任意宽度和高度

radio_button_uncheck

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:
            android:
            android:drawable="@drawable/ic_radio_button_unchecked" />
    </layer-list>

custom_radio_button

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

单选按钮

在单选按钮中

<RadioButton                                       
    android:layout_
    android:layout_                                       
    android:button="@drawable/custom_radio_button"
    android:paddingLeft="10dp"
    android:textSize="39dp"
    android:text="1" />

如果要更改文本大小,请使用“android:textSize”属性

在图片中,第一个是自定义单选按钮,第二个是默认单选按钮

【讨论】:

【参考方案2】:

目前,Android Vector Asset Studio 有未选中、选中单选按钮的图标。 您可以导入此向量,然后通过在向量 xml 中更改 android:tint 来更改颜色

然后设置backgroundRadioButtonlike

bg_radio_selector.xml

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

..xml

<androidx.appcompat.widget.AppCompatRadioButton
        android:layout_
        android:layout_
        android:background="@drawable/bg_radio_selector"
        android:button="@null" />

现在您可以将RadioButton 的大小更改为您想要的任何大小,但可能很难点击,因为点击区域非常小。 为了增加点击区域,我们可以使用这个函数(注意,填充不起作用,因为我们使用RadioButtonbackground属性)(来自How to increase hit area of Android button without scaling background?的解决方案)

fun View.increaseTouchArea(pixel: Int) 
    val parent = parent as View
    parent.post 
        val rect = Rect()
        getHitRect(rect)
        rect.top -= pixel
        rect.left -= pixel
        rect.bottom += pixel
        rect.right += pixel
        parent.touchDelegate = TouchDelegate(rect, this)
    

yourRadioButton.increaseTouchArea(context.resources.getDimensionPixelOffset(R.dimen.dp_12))

【讨论】:

【参考方案3】:

我将RadioButton 替换为ToggleButton 并应用了相同的样式。我确实覆盖了背景和按钮。

    <ToggleButton
        android:id="@+id/btnToggle1"
        android:layout_
        android:layout_
        android:layout_weight="1"
        android:checked="true"
        style="@style/ToggleButtonStyle"
        android:button="@null"
        android:textOn="@string/btnTitle"
        android:textOff="@string/btnTitle"/>

和风格:

<style name="ToggleButtonStyle">
    <item name="android:background">@drawable/background_radiobutton</item>
    <item name="android:textColor">@color/selector_text_radiobutton</item>
    <item name="android:textAppearance">@null</item>
</style>

对我有用 - 看起来一样,但具有自定义高度。

如果您的 RadioButton 在 RadioGroup 中,您将需要自定义监听器,检查 Android: How to get a radiogroup with togglebuttons?

【讨论】:

【参考方案4】:

您可以使用 scalex 和 scaley 属性,然后使用 translationX 和 translationY 将其放在单选按钮窗口中。

<RadioButton
            android:id="@+id/rbtnfkilo"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:scaleX="1.4"
            android:scaleY="1.4"
            android:text="Kilogram"
            android:textColor="#fff"
            android:textSize="18sp"
            android:translationX="24dp" />

【讨论】:

【参考方案5】:

一个快速的hacky解决方案是缩小按钮:

<RadioButton
  android:scaleX="0.5"
  android:scaleY="0.5" />

这非常适合变小。

为了变得更大,这往往会导致容器视图出现一些剪裁,因此您可能必须对 RadioGroup 的高度/宽度进行硬编码以适应缩放按钮。可绘制的按钮也可能会随着您的移动而显着像素化,因此如果您想要 3 倍大的东西,这并不是很好......

【讨论】:

并添加 android:textSize="[size]sp" 也会缩放单选按钮标签,但匹配按钮和标签大小需要更多硬代码... 缩小按钮大小,但在它们之间留出更大的空间【参考方案6】:

不能,单选按钮是一个内置的控件组件,因此它的大小是固定的。

【讨论】:

做起来可能不容易,但有可能;见***.com/a/4787260/3063884 请不要回复esp。带有“做不到”之类的负面评论,但不确定!看,Michael 有一个简单可行的解决方案! 有另一种方法。通过创建自定义单选按钮。见***.com/a/68220214/5039264【参考方案7】:
<RadioGroup android:layout_               
            android:layout_           
            android:orientation="horizontal"         
            android:checkedButton="@+id/first">  

 <RadioButton android:id="@+id/first"        
      android:        
      android:        
      android:button="@drawable/button_radio"/> 

   <RadioButton android:id="@+id/second"        
      android:     
      android:     
      android:button="@drawable/button_radio"/>

   <RadioButton android:id="@+id/third"
      android:
      android:
      android:button="@drawable/button_radio"/>

   <RadioButton android:id="@+id/fourth"                                          
      android:              
      android:           
      android:button="@drawable/button_radio"/>           
</RadioGroup>

【讨论】:

ths 不能缩放内部单选按钮样式【参考方案8】:

可以完成,但不像设置 Layout_Width 和 Layout_height 那样简单,如 EditTexts、Buttons 等。要修改复选框/单选按钮等视图的大小/外观,请使用“ Background”和“Button”属性来指定你自己的drawables。

这是一个较旧的页面,现在位置不同了,但这会给你一个想法: http://www.anddev.org/tutorial_change_look_of_checkbox-t4553.html

【讨论】:

这是一个损坏的链接。

以上是关于android中的radiobutton要点击2下才能选中的主要内容,如果未能解决你的问题,请参考以下文章

Android控件之RadioButton

Android:如何更改 RadioButton 的大小

android radiobutton点击后一直是选中状态,怎么再点击后取消选中

在 Android 中的 RadioButton 与其标签之间添加边距?

关于Android中fragment的管理

android中RadioButton的setChecked(true)无效或点击状态无反应问题