简体   繁体   English

如何在启用Android Studio中的按钮之前从复选框的广播组获取答案

[英]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: 澄清:

  • You can disable button by default in the XML layout 您可以默认在XML布局中禁用按钮
  • I don't see how you declare 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 我不看你怎么声明listenerradiobuttons ,但我猜它可以通过有效的元素才会触发这就是为什么我启用button总是不中case语句
  • If you want to use button or radiobutton s several times you can declare it as class attributes and get object in onCreate or onStart steps 如果要多次使用buttonradiobutton ,则可以将其声明为类属性,并在onCreateonStart步骤中获取对象

UPDATE: 更新:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM