尽管我只想选择一个,但可以选择两个单选按钮

时间:2019-06-04 08:19:22

标签: android radio-button

我正在制作一个android应用程序,在该应用程序中,两个单选按钮可以一次选择。但我只想选择一个。我应该怎么编码?

public class ProfileManagement extends AppCompatActivity {
    private Button update;
    private EditText uname, dob, pass;
    private RadioButton male, female;
    private DBHelper dbHelper;
    private String userId;

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

        dbHelper = new DBHelper(this);
        Intent intent = getIntent();
        userId = intent.getStringExtra("id");

        uname = findViewById(R.id.user);
        dob = findViewById(R.id.date);
        pass = findViewById(R.id.word);
        update = findViewById(R.id.btnUpdate);
        male = findViewById(R.id.radioMale);
        female = findViewById(R.id.radioFe);

        ArrayList<User> list =  dbHelper.readAllInfo(userId, null);

        for (User u : list){

            uname.setText(u.getUserName());
            pass.setText(u.getPassword());
            dob.setText(u.getDateOfBirth());

这是我检查单选按钮的方式。这样可以吗?

if(u.getGender() != null){
        if(u.getGender().equals("Male")){

                male.setChecked(true);
            }
            else
            {
                female.setChecked(true);
            }      } }
    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(ProfileManagement.this, EditProfile.class);
            intent.putExtra("id", userId);
            startActivity(intent);
        }          });      }  }

2 个答案:

答案 0 :(得分:1)

像这样利用广播组:

<RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:id="@+id/myRadioGroup"
        android:background="#abf234"
        android:checkedButton="@+id/sound" >

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/sound"
            android:text="@string/Sound" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/vibration"
            android:text="@string/Vibration" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/silent"
            android:text="@string/Silent" />

    </RadioGroup>

然后使用以下代码检查单击了哪个按钮:

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup rGroup, int checkedId) {

                int radioBtnID = rGroup.getCheckedRadioButtonId();

                View radioB = rGroup.findViewById(radioBtnID);

                int position = group.indexOfChild(radioB);
            }
        });

答案 1 :(得分:0)

更新后的代码段如下。或者,您可以尝试SpinnersRadioGroup

        if(u.getGender().equals("Male")){

            male.setChecked(true);
            female.setChecked(false); //uncheck female
        }
        else
        {
            female.setChecked(true);
            male.setChecked(false); //uncheck male
        }