android - 重置后单击 RadioButton 无操作

标签 android android-radiobutton

我是一名 Android 初学者,但遇到了一些小问题。

我有 2 个标题为“Yes”和“No”的 radioButton,4 个被禁用的 editText 和一个标题为“Reset”的 button所有”。

现在,当我选择“否”radioButton 时,editText 的 1 和 2 将启用并且 1 获得焦点。这是期望的行为。但是当我选择“全部重置”并再次重复该过程时,只有 editText 2 被启用并且“否”radioButton 仍未选中。

以下是我的代码:

MainActivity.java

    package com.example.chris.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {

    EditText e1;
    EditText e2;
    EditText e3;
    EditText e4;

    RadioButton yes;
    RadioButton no;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1 = (EditText)findViewById(R.id.e1);
        e2 = (EditText)findViewById(R.id.e2);
        e3 = (EditText)findViewById(R.id.e3);
        e4 = (EditText)findViewById(R.id.e4);
        yes = (RadioButton)findViewById(R.id.yes);
        no = (RadioButton)findViewById(R.id.no);
    }




    public void onClickreset(View v){
        yes.setChecked(false);
        no.setChecked(false);
        e1.setEnabled(false);
        e2.setEnabled(false);
        e3.setEnabled(false);
        e1.setText("");
        e2.setText("");
        e3.setText("");
        e4.setText("");

    };

    public void onRadioButtonClicked(View v)
    {
        boolean checked = ((RadioButton) v).isChecked();

        switch(v.getId()){
            case R.id.yes:
                if(checked)
                    e1.setEnabled(false);
                    e2.setEnabled(false);
                    e3.setEnabled(true);
                    e3.requestFocus();
                break;

            case R.id.no:
                if(checked)
                    e1.setEnabled(true);
                    e2.setEnabled(true);
                    e2.requestFocus();
                    e2.clearFocus();
                    e3.setEnabled(false);
                break;


        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LL"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical"
tools:context="com.example.chris.myapplication.MainActivity">

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*">

    <!-- Row 1 Starts From Here -->


    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:gravity="center"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/yes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:checked="false"
                android:onClick="onRadioButtonClicked"
                android:text="yes" />

            <RadioButton
                android:id="@+id/no"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dp"
                android:checked="false"
                android:onClick="onRadioButtonClicked"
                android:text="no" />
        </RadioGroup>

    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">


        <TextView
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="1:"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/e1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:enabled="false"
            android:inputType="numberDecimal"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">


        <TextView
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="2:"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/e2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:enabled="false"
            android:inputType="numberDecimal"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">


        <TextView
            android:id="@+id/t3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="3:"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/e3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:enabled="false"
            android:inputType="numberDecimal"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">


        <TextView
            android:id="@+id/t4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="10dp"
            android:text="4:"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/e4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:enabled="false"
            android:inputType="numberDecimal"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="30dp" />


    <Button

        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_span="3"
        android:gravity="center"
        android:onClick="onClickreset"
        android:text="Reset"
        android:textAllCaps="false"
        android:textColor="#000000"
        android:textStyle="normal" />
</TableLayout>

</LinearLayout>

最佳答案

最好用RadioGroup.OnCheckedChangeListener ,首先,摆脱android:onClick属性并在XML中为RadioGroup分配一个id:

<RadioGroup
    android:id="@+id/yes_no_radio_group" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_span="3"
    android:gravity="center"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:checked="false" 
        android:text="yes" />

    <RadioButton
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:checked="false"
        android:text="no" />
</RadioGroup>

然后让您的 Activity 实现 RadioGroup.OnCheckedChangeListener 并将代码从 onRadioButtonClicked(View v) 方法移动到那里:

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
     //Some activity code

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {

        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.yes:            //Not needed to                     
                e1.setEnabled(false); //which RadioButton clicked
                e2.setEnabled(false);
                e3.setEnabled(true);
                e3.requestFocus();
                break;

            case R.id.no:
                e1.setEnabled(true);
                e2.setEnabled(true);
                e2.requestFocus();
                e2.clearFocus();
                e3.setEnabled(false);
                break;
        }
    }
}

然后,为 OnCheckedChangeListener 分配 RadioGroup:

yesNoGroup = findViewById(R.id.radio_group);
yesNoGroup.setOnCheckedChangeListener(this);


并改变:

yes.setChecked(false);
no.setChecked(false);

yesNoGroup.clearCheck();

关于android - 重置后单击 RadioButton 无操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48005530/

相关文章:

android - 位图大小超出了 Android 上的 VM 预算错误

android - 使用内部存储中的 Intent 打开图像

android - 结合 layout_weight 和 maxHeight?

android - 如何在垂直方向的android单选按钮之间提供间距

Android - 将填充设置为 RadioButton pin

Android:BitmapFactory.decodeResource(r,id) - 显示 NPE 以从可绘制对象解码图像

android - 使用 setpts 和 atempo 选项的 FFmpeg 命令不匹配音频和视频

android - 禁用单击 RadioGroup 内部布局

android从文本中直接设置单选按钮