单选按钮点击&安培; reclick [英] radiobutton click & reclick
问题描述
我在Android的单选按钮组。
我收到的事件时,被选中的项目。正常为止。
但是,如果用户点击一个已选定的项目我没有得到该事件。
有没有办法知道(接收事件),当用户点击一个单选按钮或者如果选择了没有?
非常感谢。
I have a radio button group in Android. I receive events when the items are selected. Normal so far. But I don't get the event if the user clicks on an already selected item. Is there a way to know (to receive an event) when the users hits a radiobutton either if it is selected or not? Thanks a lot.
推荐答案
我不明白为什么在已经检查的单选按钮点击后,你会得到一个事件,
但是如果你想就可以了点击取消选择一个单选按钮,如果它已经被选中!
检查这个code,它可以帮助你:
i don't understand why you would get an event when clicked on an already checked radio button, but if you want to unselect a radio button by a click on it if it is already selected!! check this code it can help you:
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
这篇关于单选按钮点击&安培; reclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!