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

标签 java android

点击 Android Activity 中的某个单选按钮后如何禁用单选按钮组?

这是我的 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);
    }

}

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

最佳答案

如果我理解正确,您希望在单击某个 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() 不起作用,因此您应该尝试更改代码,以便它 loops through each RadioButton检查radio0后:

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

关于java - 我对如何禁用更改为单击另一个单选按钮感兴趣?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15044726/

相关文章:

android - BroadcastReceiver 的 Manifest 和 Programmatic 注册的主要区别

调用 Fragment 的 onCreateView 时出现 Android OutOfMemoryError

java - 无法创建 WifiConfiguration

android - ArrayOutOfBounds 将 addHeaderView() 与 EndlessAdapter 一起使用时

Java Arrays.asList(columnes).contains() 返回 false

java - 在格式化 CSV 的开头添加空行

java - 如何使用 admob 在 android 中集成奖励视频广告?

java - SONAR 要求使 "UserProfile"可序列化或不将其存储在 session 中

java - 自动施放 Spring Bean

java - 将插入符号放置在焦点上的 JTextField 末尾的最佳方法是什么?