Android复选框可在更改前监听点击 [英] Android checkbox listen for click before change
问题描述
我有一个要求,其中显示用于特定设置的复选框.当用户点击复选框时,我要显示一个警报对话框.然后,仅当用户点击确认按钮(或类似按钮)时,复选框才应更改.
我的意思是,仅在复选框更改状态后才触发 OnCheckedChanged
侦听器,而我想在其更改状态之前侦听单击.
,您可以使用 onTouchListener
并拦截 ACTION_DOWN
事件以显示警报.用户选择时,将以编程方式更改选中状态的复选框.
示例:
checkbox.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(查看v,MotionEvent事件){if(event.getAction()== MotionEvent.ACTION_DOWN){//显示警报返回true;//这将防止复选框更改状态}返回false;}});
然后在用户选择是或否时调用 checkbox.setChecked(true);
或 checkbox.setChecked(false);
.
I have a requirement where a checkbox is displayed for a specific setting. When the user taps on the checkbox, I want to display an alert dialog. The checkbox should then only change if the user taps on the confirm button (or similar).
My point is that the OnCheckedChanged
listener only fires after the checkbox has changed state, whereas I want to listen for the click before it changes state.
you can use onTouchListener
and intercept ACTION_DOWN
event for showing alert.
on users choice change checked state of you checkbox programatically.
example:
checkbox.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//show alert
return true; //this will prevent checkbox from changing state
}
return false;
}
});
then call checkbox.setChecked(true);
or checkbox.setChecked(false);
as user selects yes or no.`
这篇关于Android复选框可在更改前监听点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!