当所有字段以及RadioButton和Spinner之一都是Android Studio中的检查和文件时,如何启用按钮保



我有3个editText,对于性别,我有RadioGroup,我有一个带有4个选项的微调器。当所有字段都被归档,并且选择了RadioButton之一和微调器之一时,我想启用保存按钮。我用一个Watcher为三个editText制作了setOnCheckListener,但RadioButton函数isChecked(例如female.isChecked(不起作用。我该怎么做?

如果没有代码示例,我只能提供一个通用答案。

单选按钮应位于单选组内。给无线电组一个id,假设id是gender_Radio_Group

然后给每个单选按钮一个id,假设id是male_Radio_Buttonfemale_Radio_ButtonTR

现在,在该视图的代码中,您需要使用以下代码:

RadioGroup genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);
genderRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) 
{
//Get the id of the checked radio button in the group
int radioButtonID = group.getCheckedRadioButtonId();
//Get the radio button view from the group using the above id
View radioButton = group.findViewById(radioButtonID);
//Get whether the button is checked or not
boolean checked = ((RadioButton) radioButton).isChecked();
switch(radioButton.getId()) {
case R.id.male_radio_button:
if (checked)
//Do some logic if the male radio button is checked
break;
case R.id.female_radio_button:
if (checked)
//Do some logic if the female radio button is checked
break;
}
}
});

还有另一种方法可以做到这一点。您可以在Radio Buttons官方Android文档中找到更多详细信息。

将无线电组检查状态指定为"int",并在检查逻辑中使用它。当然,您还必须确保微调器也有一个监听器。参见:

RadioGroup genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);
genderRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) 
{
//Get the id of the checked radio button in the group
int radioButtonID = group.getCheckedRadioButtonId();
//Get the radio button view from the group using the above id
View radioButton = group.findViewById(radioButtonID);
//Get whether the button is checked or not
boolean checked = ((RadioButton) radioButton).isChecked();
switch(radioButton.getId()) {
case R.id.male_radio_button:
if (checked)
int checkedChecker=1;
//Do some logic if the male radio button is checked
break;
case R.id.female_radio_button:
if (checked)
int checkedChecker=1;
//Do some logic if the female radio button is checked
break;
}
}
});

if(editText1.getText.toString.isempty&& 
editText1.getText.toString.isempty&&
editText1.getText.toString.isempty&&
spinnerValue.isempty&&
intChecker>0){
saveButton.setEnabled(true);
}else{
saveButton.setEnabled(false);
}

最新更新