我正在练习一本书上的练习,它要求我先创建一个复选框,然后是三个无线电按钮。如果未选中复选框,则无线电按钮应显示为禁用/灰色/不可点击。但是,一旦您选中复选框,无线电按钮就应该显示为工作和可点击。我为复选框和无线电按钮创建了布局(无线电按钮是无线电组的一部分),但我很难按照练习要求的方式连接它们。我将如何在主体活动中写出这些?
发布于 2022-04-12 02:05:37
您可以使用if语句检查复选框是否已单击。先将按钮设置为禁用/灰色,然后再使用if语句检查复选框,如果复选框已单击“可用”按钮。
您的代码将如下所示
Button button1, button2, button3;
Checkbox checkbox;
button1 = findViewById (yourid);
button2 = findViewById (yourid);
button3 = findViewById (yourid);
checkbox = findViewById (yourid);
//Disable your button first
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
if(checkbox.isChecked()){
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
}
else{
Toast.makeText(getApplicationContext(), "Checkbox Haven't clicked",
Toast.LENGTH_SHORT).show();
}https://stackoverflow.com/questions/71834745
复制相似问题