我在 Android 中有一个单选按钮组。
选择项目时我会收到事件。目前为止还正常。
但如果用户单击已选择的项目,我不会收到该事件。
有没有办法知道(接收事件)用户何时点击单选按钮,无论它是否被选择?
多谢。
我不明白为什么当单击已选中的单选按钮时会收到事件,
但是如果您想通过单击单选按钮来取消选择它(如果它已经被选中)!
检查此代码它可以帮助您:
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.rg);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{ //The first codition check if we have clicked on an already selected radioButton
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}
Hope this wil help
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)