A B*_*A B 4 android button radio
该应用程序是一个步进音序器应用程序,具有16个无线电组,每组有8个按钮.除非我使用我创建的清除按钮清除所有放射线组,否则除非一个组选择了一个按钮,否则它可以完美地工作.我想要添加的是一些代码,表示当再次选择所选单选按钮时,它会像切换一样关闭.我尝试使用切换,但随后出现了其他问题.下面是两次尝试,但两个都只是阻止我使用按钮
final RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.RadioGroup1);
RadioButton D1 = (RadioButton)findViewById(R.id.RadioButtonD1);
Button D1 = (Button)findViewById(R.id.RadioButtonD1);
D1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PdBase.sendFloat("D1", 74);
int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
RadioButton D1 = (RadioButton) findViewById(selectedTypeId);
if(D1 != null) // This will be null if none of the radio buttons are selected
radioGroup1.clearCheck();
PdBase.sendFloat("D1", 0);
}
});
RadioButton lC1 = (RadioButton)findViewById(R.id.RadioButtonlowC1);
lC1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int selectedTypeId = radioGroup1.getCheckedRadioButtonId();
RadioButton lC1 = (RadioButton) findViewById(R.id.RadioButtonlowC1);
if (selectedTypeId == -1){
PdBase.sendFloat("lC1", 72);
}
else if (selectedTypeId == R.id.RadioButtonlowC1) {
radioGroup1.clearCheck();
PdBase.sendFloat("lC1", 0);
}
}
});
Run Code Online (Sandbox Code Playgroud)
spa*_*y21 18
我最近有同样的需求 - 有一个无线电组,通过再次点击它可以取消选择所选项目.我发现我无法做到,使用监听器,但我是能够做到这一点使用自定义的RadioButton,是这样的...
public class ToggleableRadioButton extends RadioButton {
// Implement necessary constructors
@Override
public void toggle() {
if(isChecked()) {
if(getParent() instanceof RadioGroup) {
((RadioGroup)getParent()).clearCheck();
}
} else {
setChecked(true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,该按钮以不同的方式切换,具体取决于其当前状态 - 即,呼叫setChecked(true)按钮与呼叫clearCheck()组.如果setChecked()在两种情况下使用,则无法立即重新选择刚取消选择的按钮 - 逻辑RadioGroup似乎立即取消选择它.
要使用此按钮,只需在布局XML中替换您的<RadioButton>标签即可<your.package.ToggleableRadioButton>.
我只是用了@ spaaarky21的答案
我的完整代码看起来像这样,并且运行良好!
Java类
public class ToggleableRadioButton extends RadioButton {
public ToggleableRadioButton(Context context) {
super(context);
}
public ToggleableRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public ToggleableRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void toggle() {
if(isChecked()) {
if(getParent() instanceof RadioGroup) {
((RadioGroup)getParent()).clearCheck();
}
} else {
setChecked(true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于XML布局
<com.smart_dent.adapters.ToggleableRadioButton android:id="@+id/tejido_blando_perfil_convexo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tejido_blando_convexo_label" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您只需要更改程序包,我就很容易找到它,它位于Java Class Flie的顶部(如果您是从Android Studio创建的,则位于该位置)
实际上,可以使用侦听器来完成此操作,但是可以使用OnTouchListener,在按钮状态更改之前触发,而不是通常OnClickListener。以下对我有用:
View.OnTouchListener radioButtonOnTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (((RadioButton) v).isChecked()) {
// If the button was already checked, uncheck them all
radioGroup.clearCheck();
// Prevent the system from re-checking it
return true;
}
return false;
}
};
radioButton1.setOnTouchListener(radioButtonOnTouchListener);
radioButton2.setOnTouchListener(radioButtonOnTouchListener);
Run Code Online (Sandbox Code Playgroud)
和radioGroup的父母在哪里radioButton1radioButton2
| 归档时间: |
|
| 查看次数: |
11460 次 |
| 最近记录: |