单击单选组Android Studio中的单选按钮后启用EditText

时间:2019-04-24 01:45:28

标签: android

我有一个RadioGroup,里面有6个单选按钮。如果用户单击第六个单选按钮,将启用EditText进行键入。否则,EditText将保持禁用状态。我在此网站上尝试了许多解决方案,但仍然出现一些错误。最常见的错误是EditText保持禁用,尽管我将其设置为在单击第6个单选按钮时启用。下面是XML的代码:

 <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="20dp"
            android:layout_marginRight="25dp">

            <RadioGroup
                android:id="@+id/rgSuggestWill"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/colorAccent"
                android:textStyle="bold"
                android:checkedButton="@+id/rbSaveCost">

                <RadioButton
                    android:id="@+id/rbSaveCost"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:checked="false"
                    android:focusable="auto"
                    android:text="Save Cost"
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"/>

                <RadioButton
                    android:id="@+id/rbRevenue"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:checked="false"
                    android:focusable="auto"
                    android:text="Increase Revenue"
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"/>

                <RadioButton
                    android:id="@+id/rbQuality"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:checked="false"
                    android:focusable="auto"
                    android:text="Improve Quality"
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"/>

                <RadioButton
                    android:id="@+id/rbWorkProcess"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:checked="false"
                    android:focusable="auto"
                    android:text="Improve Work Process Efficiency"
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"/>

                <RadioButton
                    android:id="@+id/rbSafety"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:checked="false"
                    android:focusable="auto"
                    android:text="Improve Safety"
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"/>

                <RadioButton
                    android:id="@+id/rbOwnValue"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:checked="false"
                    android:focusable="auto"
                    android:text="Specify your own value: "
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:textStyle="bold"/>
            </RadioGroup>
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="10dp"
            android:layout_marginRight="25dp">

            <EditText
                android:id="@+id/etOwnValue"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:padding="10dp"
                android:textColor="@color/colorAccent"
                android:textStyle="bold"
                android:background="@layout/rounded_border_edittext"
                android:ems="10"
                android:hint="Others" />
        </TableRow>

1 个答案:

答案 0 :(得分:0)

这可能会帮助您:

// getting the groug
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);  

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
if(checkedId==6) {
// if the id of the checkedId equels to the id of the 6 button than make the editText enable 

myEditText.setEnable(true)
} else {
// else (wich mean that other redio button been clicked) make it disable.
myEditText.setEnable(false)
}
        }
    });

,然后在xml中确定您的陈述状态(启用或禁用)