在一个活动中,我有一个datePicker(旋转器),它允许用户选择他们的出生日期,在此之下,用户还必须选择他们的性别。我有一个按钮,一旦设置了两个值(日期和性别),就会启用该按钮。尽管首先更改了值,但我现在面临的问题是启用该按钮。当前,用户必须选择其出生日期,然后选择其性别,才能激活该按钮。我希望无论首先更改哪个值都可以启用该按钮。
这是我的代码:
final RadioGroup rGroup = findViewById(R.id.gender_radio_group); RadioButton female_radio_button = rGroup.findViewById(rGroup.getCheckedRadioButtonId()); RadioButton male_radio_button = rGroup.findViewById(rGroup.getCheckedRadioButtonId()); RadioButton non_binary_radio_button = rGroup.findViewById(rGroup.getCheckedRadioButtonId()); //Check if datePicker and gender radiobutton has been used, if true enable button calendar.setTimeInMillis(System.currentTimeMillis()); datePicker_birthday.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) { // This overrides the radiogroup onCheckListener rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // This will get the radiobutton that has changed in its check state RadioButton checkedRadioButton = group.findViewById(checkedId); // This puts the value (true/false) into the variable boolean isChecked = checkedRadioButton.isChecked(); // If the radiobutton that has changed in check state is now checked... if (isChecked) { continue_register_birthday_gender_button.setEnabled(true); continue_register_birthday_gender_button.setBackground(ContextCompat.getDrawable(RegisterBirthDateAndGenderActivity.this, R.drawable.button_active)); } } }); } });
您的代码很少有问题。
如果用户首先选择了单选按钮选择,则不会触发rGroup.setOnCheckedChangeListener()。需要重新放置在onDateChanged()之外。
重定位后,它也将无法工作,因为无线电组侦听器不知道datePicker条目是否也被更改。
用户可以选择先更新性别,然后选择日期,反之亦然。可能的解决方案如下:
查看fixme评论。
fixme
// fixme: Add class variables (booleans) to keep track of changed ui items private Boolean isDatePickerOnDateChanged = false; private Boolean isRadioGroupOnCheckedChanged = false; // fixme: Re-locate rGroup.setOnCheckedChangeListener(..) to here. RadioButton non_binary_radio_button = rGroup.findViewById(rGroup.getCheckedRadioButtonId()); rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { //... isRadioGroupOnCheckedChanged = true; if (isDatePickerOnDateChanged && isRadioGroupOnCheckedChanged) { continue_register_birthday_gender_button.setEnabled(true); } // fixme: Remove previous rGroup.setOnCheckedChangeListener(..) inside onDateChanged() to above datePicker_birthday.init(... { @Override public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) { //... isDatePickerOnDateChanged = true; if (isDatePickerOnDateChanged && isRadioGroupOnCheckedChanged) { continue_register_birthday_gender_button.setEnabled(true); }
isRadioGroupOnCheckedChanged = true; if (isDatePickerOnDateChanged && isRadioGroupOnCheckedChanged) { continue_register_birthday_gender_button.setEnabled(true); }
可以简化为
isRadioGroupOnCheckedChanged = true; if (isDatePickerOnDateChanged) { continue_register_birthday_gender_button.setEnabled(true); }
因为&& isRadioGroupOnCheckedChanged前面的陈述总是正确的isRadioGroupOnCheckedChanged = true;。
&& isRadioGroupOnCheckedChanged
isRadioGroupOnCheckedChanged = true;