[英]How to un-check radio button without radio group?
I have used the following code snippet to set one radio button to unchecked if the other is selected,but when I run the application and select both radio buttons the code doesn't work as both stay selected. 我已经使用以下代码片段来设置一个单选按钮以取消选中另一个单选按钮,但是当我运行应用程序并选择两个单选按钮时,代码不起作用,因为两者都保持选中状态。
I'm using a relative layout so I can't use any other solutions with a radio group,I'm just using two seperate radio buttons. 我正在使用相对布局,所以我不能使用任何其他解决方案与无线电组,我只是使用两个单独的单选按钮。
Can someone point out where I could be going wrong somewhere as I can't see a flaw in the logic,any ideas? 有人可以指出我在哪里可能出错了,因为我看不出逻辑中的缺陷,任何想法?
Anyways this is the solution I'm trying to implement but its not working in application: 无论如何,这是我试图实现的解决方案,但它不适用于应用程序:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radBtnMM:
if (checked)
//set inch button to unchecked
radioInch.setChecked(false);
break;
case R.id.radBtnInch:
if (checked)
//set MM button to unchecked
radioMM.setChecked(false);
break;
}
}
When is Your public void onRadioButtonClicked(View view)
called? 什么时候您的
public void onRadioButtonClicked(View view)
调用? Is it from a listener? 是听众吗?
This will work: 这将有效:
layout.xml: layout.xml:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnMM"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radBtnInch"
/>
YourActivity.java: YourActivity.java:
public class MainActivity extends Activity implements OnCheckedChangeListener {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main);
rb1 = (RadioButton) findViewById(R.id.radBtnInch);
rb1.setOnCheckedChangeListener(this);
rb2 = (RadioButton) findViewById(R.id.radBtnMM);
rb2.setOnCheckedChangeListener(this);
return true;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.radBtnInch) {
rb2.setChecked(false);
}
if (buttonView.getId() == R.id.radBtnMM) {
rb1.setChecked(false);
}
}
}
Try this... 试试这个...
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.radioBtnMM:
// set inch button to unchecked
radioBtnInch.setChecked(!checked);
break;
case R.id.radioBtnInch:
// set MM button to unchecked
radioBtnMM.setChecked(!checked);
break;
}
}
Try This: 试试这个:
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.radBtnMM:
if (checked)
{
radioInch.setChecked(false);
}
else
{
radioInch.setChecked(true);
}
break;
case R.id.radBtnInch:
if (checked)
{
radioMM.setChecked(false);
}
else
{
radioMM.setChecked(true);
}
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.