[英]How to check if "Radiobutton" is checked?
我想用条件 (if-else) RadioButton
创建一个结构
I want that when the Radiobutton RB1 is selected, this function is active:
regAuxiliar = ultimoRegistro;
选择RadioButton RB2时,此功能处于活动状态:
regAuxiliar = objRegistro;
对不起我的英语,我是巴西人。
就像使用 CheckBox 一样
RadioButton rb;
rb = (RadioButton) findViewById(R.id.rb);
rb.isChecked();
if(jRadioButton1.isSelected()){
jTextField1.setText("Welcome");
}
else if(jRadioButton2.isSelected()){
jTextField1.setText("Hello");
}
您还可以根据侦听器维护一个标志值,
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
}
});
或者简单的isChecked()
也可以用于检查 RadioButton 的状态。
这是一个示例的链接,
http://www.mkyong.com/android/android-radio-buttons-example/
然后根据标志,您可以执行您的功能。
radiobuttonObj.isChecked()
会给你布尔值
if(radiobuttonObj1.isChecked()){
//do what you want
}else if(radiobuttonObj2.isChecked()){
//do what you want
}
如果您需要进行浓缩咖啡测试,解决方案如下:
onView(withId(id)).check(matches(isChecked()));
再见,
如果选择 RadioButton 按钮, radioButton.isChecked()
函数返回 true,否则返回 false。
您可以像这样使用 switch:
XML 布局
<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>
和JAVA活动
switch (RG.getCheckedRadioButtonId()) {
case R.id.R1:
regAuxiliar = ultimoRegistro;
case R.id.R2:
regAuxiliar = objRegistro;
default:
regAuxiliar = null; // none selected
}
您还需要使用按钮或 setOnCheckedChangeListener 函数实现 onClick 函数以获得所需的功能。
简单的解决方案
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;
}
}
});
检查它们是否使用el.checked
属性进行了检查。
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>
这对我有用:
Espresso.onView(ViewMatchers.withId(R.id.radiobutton)).check(ViewAssertions.matches(isChecked()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.