【问题标题】:Working with Multiple Radio Group values in Android在 Android 中使用多个 Radio Group 值
【发布时间】:2019-07-08 03:43:03
【问题描述】:

我有两个单选组,每个组包含两个单选按钮。我想等到每个组中的一个按钮被选中,然后显示一个 Toast。 (例如,如果选中了第 1 组中的单选按钮 1 和第 2 组中的单选按钮 3,则显示 Toast 消息。我没有收到任何错误消息,但没有显示 Toast:

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="92dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.508"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="@string/rb1" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="@string/rb2" />
    </RadioGroup>


    <RadioGroup
        android:id="@+id/rg2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="228dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="@string/rb3" />

        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="onClick"
            android:text="@string/rb4" />
    </RadioGroup>
</android.support.constraint.ConstraintLayout>

JAVA 代码:

    package com.example.rgapp;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;

    public class MainActivity extends Activity {



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

        }


        public void onClick(View view) {

            boolean checked = ((RadioButton) view).isChecked();


            if (view.getId() == (R.id.rb1) & (view.getId() == (R.id.rb3)))
                Toast.makeText(getApplicationContext(), "1 and 3 selected",
                        Toast.LENGTH_SHORT).show();

        }
    }

【问题讨论】:

  • 在你的java代码中实现OnClickListener
  • 首先您应该定义您的单选组和单选按钮,然后通过 onClickListener() 使用它们。
  • @Android 他/她已经在所有 XML 中实现了onClick。检查RadioButton
  • 1) 不要在 XML 中设置点击监听器,迟早你会因此而头疼的。 2) &amp; 不是您要在这里检查AND 的操作员。 3) view.getId() 不能同时是 R.id.rb1R.id.rb3

标签: java android button radio


【解决方案1】:

如果同时选择了无线电组,请使用此代码来实现您的功能,然后您将获得结果

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

        RadioGroup rg1,rg2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rg1 =(RadioGroup)findViewById(R.id.rg1);
            rg2 =(RadioGroup)findViewById(R.id.rg2);

            rg1.setOnCheckedChangeListener(this);
            rg2.setOnCheckedChangeListener(this);
        }
        int radioId1 = 0,radioId2 = 0;
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {

            if (radioGroup == rg1){

                radioId1 = i;
            }
            if (radioGroup == rg2){
                radioId2 = i;
            }

            if (radioId1 != 0 && radioId2 != 0){
                RadioButton radioButton1 = (RadioButton) findViewById(radioId1);
                RadioButton radioButton2 = (RadioButton) findViewById(radioId2);
                Toast.makeText(this,radioButton1.getText()+" and "+radioButton2.getText()+" is selected",Toast.LENGTH_LONG).show();
            }
        }
    }

【讨论】:

    【解决方案2】:

    在xml和java下面试试这个

    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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RadioGroup
            android:id="@+id/rg1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="92dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.508"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <RadioButton
                android:id="@+id/rb1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onCustomClick"
                android:text="rb1" />
    
            <RadioButton
                android:id="@+id/rb2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onCustomClick"
                android:text="rb2" />
        </RadioGroup>
    
    
        <RadioGroup
            android:id="@+id/rg2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="228dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <RadioButton
                android:id="@+id/rb3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onCustomClick"
                android:text="rb3" />
    
            <RadioButton
                android:id="@+id/rb4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="onCustomClick"
                android:text="rb4" />
        </RadioGroup>
    </android.support.constraint.ConstraintLayout>
    

    MainActivity.java :

    public class MainActivity extends Activity {
    
        RadioButton rb1, rb3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            rb1 = (RadioButton) findViewById(R.id.rb1);
            rb3 = (RadioButton) findViewById(R.id.rb3);
    
            rb1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onCustomClick(v);
                }
            });
        }
    
        public void onCustomClick(View view) {
            boolean checked = ((RadioButton) view).isChecked();
            if (rb1.isChecked() && rb3.isChecked())
                Toast.makeText(getApplicationContext(), "1 and 3 selected",
                        Toast.LENGTH_SHORT).show();
    
        }
    }
    

    如果选择了组 1 中的 radiobutton1 和组 2 中的 radiobutton3,它将显示 toast。

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 2020-12-01
      • 1970-01-01
      • 2016-04-09
      • 2022-07-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2018-08-07
      相关资源
      最近更新 更多