How to check if “Radiobutton” is checked?

前端 未结 10 430
萌比男神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:45

    This worked for me:

       Espresso.onView(ViewMatchers.withId(R.id.radiobutton)).check(ViewAssertions.matches(isChecked()))
    
    0 讨论(0)
  • 2020-12-06 06:46
            if(jRadioButton1.isSelected()){
                jTextField1.setText("Welcome");
            }
            else if(jRadioButton2.isSelected()){
                jTextField1.setText("Hello");
            }
    
    0 讨论(0)
  • 2020-12-06 06:47

    You can also maintain a flag value based on listener,

     radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    
                    //handle the boolean flag here. 
                      if(arg1==true)
                             //Do something
    
                    else 
                        //do something else
    
                }
            });
    

    Or simply isChecked() can also be used to check the state of your RadioButton.

    Here is a link to a sample,

    http://www.mkyong.com/android/android-radio-buttons-example/

    And then based on the flag you can execute your function.

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

    Simple Solution

    radioSection.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(RadioGroup group1, int checkedId1) {
                  switch (checkedId1) {
                      case R.id.rbSr://radiobuttonID
                          //do what you want
                          break;
                      case R.id.rbJr://radiobuttonID
                          //do what you want
                          break;
                  }
              }
          });
    
    0 讨论(0)
提交回复
热议问题