重置后没有动作onRadioButton单击

问题描述 投票:1回答:1

我是Android初学者,我有一点问题。

我有2个radioButtons标题为“是”和“否”,4个editTexts被禁用,button标题为“全部重置”。

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

以下是我的代码:

main activity.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>
android android-radiobutton
1个回答
1
投票

最好使用RadioGroup.OnCheckedChangeListener,首先,摆脱android:onClick属性并为RadioGroup中的XML指定一个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();
© www.soinside.com 2019 - 2024. All rights reserved.