【问题标题】:I'm interested in how to disable to change to click on another radiobutton?我对如何禁用更改以单击另一个单选按钮感兴趣?
【发布时间】:2013-02-09 06:48:30
【问题描述】:

点击Android活动中的一些单选按钮后如何禁用单选按钮组?

这是我的 XML 代码

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tPitanje1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1. Neposredno regulisanje saobracaja na putevima vrse:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tPitanje1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="62dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="uniformisani komunalni policajci" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="unoformisani policijski sluzbenici" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="inspektori za drumski saobracaj" />
    </RadioGroup>

</RelativeLayout>

这是我的 Java 代码

   package com.example.autoskola;

import android.app.Activity;
import android.os.Bundle;

public class obs1 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.obs1);
    }

}

我对如何禁用更改为单击另一个单选按钮感兴趣?

【问题讨论】:

  • 我对你的最后一句话有点困惑,但听起来你想在用户检查一个按钮后禁用一个组。如果是这样,您确定要这样做吗?如果他们不小心选错了怎么办?

标签: java android


【解决方案1】:

如果我理解正确,您希望在单击某个 RadioButton 后禁用 RadioGroup。只需添加一个OnCheckedChangedListener,如果单击的RadioButton 的ID 是正确的,则使用setEnabled(false) 禁用该组。当然,您无法重新启用 RadioGroup,除非您的代码中有其他逻辑可以重新启用。

RadioGroup radioGroup = (RadioGroup)findViewById (R.id.radioGroup1);
radioGroup.setOnCheckedChangedListener (new OnCheckedChangedListener(){
public void onCheckedChanged(RadioGroup group, int checkedId) 
{
  if (checkedId == R.id.radio0)
    group.setEnabled(false);
}
});

编辑:setEnabled() 似乎不起作用,所以您应该尝试更改您的代码,以便在检查 radio0 时更改为 loops through each RadioButton

if (checkedId == R.id.radio0){
   for(int i = 0; i < group.getChildCount(); i++){
            ((RadioButton)rg1.getChildAt(i)).setEnabled(false);
        }
}

【讨论】:

  • 对不起,当我看到这项工作是合乎逻辑的,但是当我尝试在我的代码中使用它时显示他们仍然可以更改 RadioGroup 中的值。
  • @Muskolo 所以你的意思是 RadioGroup 没有被禁用?
  • 是的 RadioGroup 没有被禁用它仍然可以改变 RaddioButton 我也尝试了 isClickable 但没有效果。
  • @Muskolo 似乎setEnabled() 不起作用,您应该循环遍历像this 这样的孩子的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多