How to check if “Radiobutton” is checked?

前端 未结 10 429
萌比男神i
萌比男神i 2020-12-06 06:01

I would like to make a structure with the condition (if-else) RadioButton

I want that when the Radiobutton RB1 is selected, this function is ac

相关标签:
10条回答
  • 2020-12-06 06:27

    Check if they're checked with the el.checked attribute.

    Docs or this Question

    let radio1 = document.querySelector('.radio1');
    let radio2 = document.querySelector('.radio2');
    let output = document.querySelector('.output');
    
    function update() {
      if (radio1.checked) {
        output.innerHTML = "radio1";
      }
      else {
        output.innerHTML = "radio2";
      }
    }
    
    update();
    <div class="radios">
      <input class="radio1" type="radio" name="radios" onchange="update()" checked>
      <input class="radio2" type="radio" name="radios" onchange="update()">
    </div>
    <div class="output"></div>

    0 讨论(0)
  • 2020-12-06 06:28

    You can use switch like this:

    XML Layout

    <RadioGroup
                android:id="@+id/RG"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
    
                <RadioButton
                    android:id="@+id/R1"
                    android:layout_width="wrap_contnet"
                    android:layout_height="wrap_content"
                    android:text="R1" />
    
                <RadioButton
                    android:id="@+id/R2"
                    android:layout_width="wrap_contnet"
                    android:layout_height="wrap_content"
                    android:text="R2" />
    </RadioGroup>
    

    And JAVA Activity

    switch (RG.getCheckedRadioButtonId()) {
            case R.id.R1:
                regAuxiliar = ultimoRegistro;
            case R.id.R2:
                regAuxiliar = objRegistro;
            default:
                regAuxiliar = null; // none selected
        }
    

    You will also need to implement an onClick function with button or setOnCheckedChangeListener function to get required functionality.

    0 讨论(0)
  • 2020-12-06 06:34

    Just as you would with a CheckBox

    RadioButton rb;
    
    rb = (RadioButton) findViewById(R.id.rb);
    
    rb.isChecked();
    
    0 讨论(0)
  • 2020-12-06 06:38

    radiobuttonObj.isChecked() will give you boolean

    if(radiobuttonObj1.isChecked()){
    //do what you want 
    }else if(radiobuttonObj2.isChecked()){
    //do what you want 
    }
    
    0 讨论(0)
  • 2020-12-06 06:41

    If you need for espresso test the solutions is like this :

    onView(withId(id)).check(matches(isChecked()));
    

    Bye,

    0 讨论(0)
  • 2020-12-06 06:42

    radioButton.isChecked() function returns true if the Radion button is chosen, false otherwise.

    Source

    0 讨论(0)
提交回复
热议问题