【问题标题】:How to check checkbox if radio button is checked如果选中单选按钮,如何检查复选框
【发布时间】:2019-02-07 21:51:11
【问题描述】:

我有一个对话框。我想更改选中的属性,但我不能。如果选中单选按钮,我想选中复选框。

是activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="336dp"
        android:text="TextView"
        android:textSize="40sp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/start" />

    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

activity_dialog.xml 提供了我的自定义对话框。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/start"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <RadioButton
            android:id="@+id/singleA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:checked="false"
            android:onClick="tytClick"
            android:text="TYT" />

        <LinearLayout
            android:id="@+id/linearTYT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp"
            android:orientation="vertical"
            android:visibility="visible"
            tools:context=".MainActivity">

            <CheckBox
                android:id="@+id/multi1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:checked="false"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:text="Maths" />

            <CheckBox
                android:id="@+id/multi2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:checked="false"
                android:text="Turkish"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

            <CheckBox
                android:id="@+id/multi3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="5dp"
                android:checked="false"
                android:clickable="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:text="English" />

        </LinearLayout>

        <RadioButton
            android:id="@+id/singleB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="5dp"
            android:checked="false"
            android:onClick="aytClick"
            android:text="AYT" />

    </RadioGroup>
</android.support.constraint.ConstraintLayout>

并且是 MainActivity.class

public class MainActivity extends AppCompatActivity {
final Context context = this;
Button start;
TextView textView;
CheckBox multi1, multi2,multi3;
RadioButton singleA, singleB;
LinearLayout linearTYT;
RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    start = findViewById(R.id.start);
    textView = findViewById(R.id.textView);

    multi1 = findViewById(R.id.multi1);
    multi2 = findViewById(R.id.multi2);
    multi3 = findViewById(R.id.multi3);
    singleA = findViewById(R.id.singleA);
    singleB = findViewById(R.id.singleB);
    linearTYT = findViewById(R.id.linearTYT);
    radioGroup = findViewById(R.id.radioGroup);

    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.activity_dialog);
            dialog.setTitle("Choose");

            dialog.show();

        }

    });
    if (singleA.isChecked()) {
        multi1.setChecked(true);
        multi2.setChecked(true);
        multi3.setChecked(true);
        linearTYT.setVisibility(View.VISIBLE);
    } else {
        multi1.setChecked(false);
        multi2.setChecked(false);
        multi3.setChecked(false);
        linearTYT.setVisibility(View.GONE);
    }
}}

我还尝试 View.GONE 单选按钮下方的线性布局,但它崩溃了。 我想我不能在出现对话框后改变它的视图。或者我做错了。

【问题讨论】:

    标签: java android checkbox radio-button


    【解决方案1】:

    欢迎来到 StackOverflow。

    看来您是初学者。我正在提供解决方案,但可能需要进行一些修改才能完美运行。

    您需要修改您的 OnClickListener。

    首先,您在对话框中使用了您的 activity_dialog.class(我相信是 activity_dialog.xml)。因此,您根本无法通过 Activity 类的findViewById 方法获得它的视图。有一个setOnCheckChangeListener 方法可以实现你想要的。类似于Button.setOnClickListener()

    OnClickListener -

    start.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.activity_dialog);
                dialog.setTitle("Choose");
                dialog.show();
                multi1 = dialog.findViewById(R.id.multi1);
                multi2 = dialog.findViewById(R.id.multi2);
                multi3 = dialog.findViewById(R.id.multi3);
                singleA = dialog.findViewById(R.id.singleA);
                singleB = dialog.findViewById(R.id.singleB);
                singleA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            multi1.setChecked(true);
                            multi2.setChecked(true);
                            multi3.setChecked(true);
                        }else{
                            multi1.setChecked(false);
                            multi2.setChecked(false);
                            multi3.setChecked(false);
                        }
                    }
                });
                singleB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            multi1.setChecked(false);
                            multi2.setChecked(false);
                            multi3.setChecked(false);
                        }else{
                            multi1.setChecked(true);
                            multi2.setChecked(true);
                            multi3.setChecked(true);
                        }
                    }
                });
            }
    
        });
    

    需要根据您的需要进行一些调整。祝你好运!!

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 2021-01-25
      • 2017-05-26
      • 2012-03-08
      相关资源
      最近更新 更多