点击 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/