[英]how to get an answer from a radiogroup of checkboxes before enabling a button in android studio
//this is my code below. //这是我的以下代码。 i want to be able to get the user to tick one of the checkboxes 'left, 'centre', right' before they are able to go to "TEST 3".
我希望能够让用户在“测试3”之前勾选“左,中,右”复选框之一。 the button "NEXT" should not be pressed until one checkbox is ticked basically
在基本勾选一个复选框之前,不应按下按钮“ NEXT”
public class Test2 extends AppCompatActivity {
private static Button button102;
private CheckBox checkBox1, checkBox2, checkBox3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox3 = (CheckBox) findViewById(R.id.checkBox3);
OnClickButtonListener102();
}
public void onCheckboxClicked(View view) {
switch(view.getId()) {
case R.id.checkBox1:
checkBox2.setChecked(false);
checkBox3.setChecked(false);
break;
case R.id.checkBox2:
checkBox3.setChecked(false);
checkBox1.setChecked(false);
break;
case R.id.checkBox3:
checkBox1.setChecked(false);
checkBox2.setChecked(false);
break;
}
}
//by clicking 'next' you reset the page
public void OnClickButtonListener102() {
button102 = (Button) findViewById(R.id.button1002);
button102.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.Test2");
startActivity(intent);
}
}
);
}
}
Declare the button as disabled 将按钮声明为禁用状态
@Override
protected void onCreate(Bundle savedInstanceState) {
button102 = (Button) findViewById(R.id.button1002);
button102.setEnabled(false);
}
And enable the button when a radio button is clicked with Button.setEnabled(boolean)
: 并在通过
Button.setEnabled(boolean)
单击单选按钮时启用该按钮:
button102 = (Button) findViewById(R.id.button1002);
button102.setEnabled(true);
switch(view.getId()) {
case R.id.checkBox1:
checkBox2.setChecked(false);
checkBox3.setChecked(false);
break;
case R.id.checkBox2:
checkBox3.setChecked(false);
checkBox1.setChecked(false);
break;
case R.id.checkBox3:
checkBox1.setChecked(false);
checkBox2.setChecked(false);
break;
}
Clarifications: 澄清:
XML
layout XML
布局中禁用按钮 listener
for radiobuttons
but I'm guessing it can be only triggered by valid elements thats why I enable button
always and not inside case
statements listener
的radiobuttons
,但我猜它可以通过有效的元素才会触发这就是为什么我启用button
总是不中case
语句 button
or radiobutton
s several times you can declare it as class attributes and get object in onCreate
or onStart
steps button
或radiobutton
,则可以将其声明为类属性,并在onCreate
或onStart
步骤中获取对象 there is one more problem, if i tick the box, and then untick it, it allows me to go to the next activity as the button is not dissabled anymore...how can i fix that?
还有一个问题,如果我勾选该框,然后取消选中它,由于按钮不再可用,它使我可以进行下一个活动...我该如何解决?
FAST WAY? 快路? don't allow deselection if other is not selected.
如果未选择其他,则不允许取消选择。
button102 = (Button) findViewById(R.id.button1002);
button102.setEnabled(true);
switch(view.getId()) {
case R.id.checkBox1:
checkBox1.setChecked(true);
checkBox2.setChecked(false);
checkBox3.setChecked(false);
break;
case R.id.checkBox2:
checkBox3.setChecked(false);
checkBox2.setChecked(true);
checkBox1.setChecked(false);
break;
case R.id.checkBox3:
checkBox1.setChecked(false);
checkBox2.setChecked(false);
checkBox3.setChecked(true);
break;
}
But if you want to select just ONE checkbox, you can make the logic like in this answer , but I would strongly recommend to change your CheckBox elements per RadioButtons that are designed for this, 但是,如果您只想选择一个复选框,则可以使逻辑类似于此答案 ,但是我强烈建议您针对每个为此设计的RadioButton更改CheckBox元素,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.