Android GUI :: 连续点击事件挂起
你好 我有一个带有单个图像的 ImageButton。我已经添加了 onclick 事件,通常它工作正常。单击 Imagebutton 时,我的添加条目功能可以工作。
现在,当用户连续单击我的图像按钮时,我的添加条目函数会使用相同的数据执行多次。我想阻止它。这样,在我的函数完全执行之前,Imagebutton 不应该排队处理下一个单击事件。
我尝试在 OnClick 事件执行后立即执行 myImageButton.setEnable(false)
。 在我的数据输入函数之后执行
myImageButton.setEnable(true)
。
我还尝试将此代码放入 myImageButton.isEnabled()
但没有成功。
如何忽略此类排队的点击事件?有没有其他方法(除了 setEnable()
)来忽略/吃掉点击处理?
我已经通过放置 println 语句来检查每个单击事件是否同步......意味着所有代码都按顺序执行。
编辑
private OnClickListener m_AddClickHandler = new OnClickListener()
{
public void onClick(View v)
{
if(m_bDoAdd)
{
m_bDoAdd = false;
new AddTask().execute();
}
else
logData("Add::OnClick::not clicked");
}
};
private class AddTask extends AsyncTask<Void, Void, Integer>
{
@Override
protected Integer doInBackground(Void... params)
{
logData("doinbg, start"+m_bDoAdd);
int iStatus =Add(m_data);
logData("doinbg, start end, status="+iStatus+"flag="+m_bDoAdd);
return iStatus;
}
@Override
protected void onPostExecute(Integer result)
{
logData("onpostExec, start"+m_bDoAdd);
int iStatus = result;
if (iStatus == 0)
{
Toast.makeText(getApplicationContext(), R.string.strAdded, Toast.LENGTH_SHORT).show();
}
else if (iStatus == 1)
{
Toast.makeText(getApplicationContext(), R.string.strAlreadyExists, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), R.string.strAddFailed, Toast.LENGTH_SHORT).show();
}
m_bDoAdd = true;
logData("onpostExec, end"+m_bDoAdd);
}
}
void Add()
{
// Add info to db (takes few msecs)
}
我仍然没有在日志中收到“Add::OnClick::not clicked”。
还有进一步的线索吗?
Hello
I have one ImageButton with single image. I have added onclick event and normally it works fine.When Imagebutton is clicked my add entry function works.
Now when user continously clicks my imagebutton, My add entry functions executes that many times with same data. I want to prevent it. Such that, Imagebutton should not queue up to process next click event until my function gets executed completely.
I tried to myImageButton.setEnable(false)
as soon as OnClick event executes. And Doing
myImageButton.setEnable(true)
after my data entry function.
I also tried to put this code in myImageButton.isEnabled()
but didnt work.
How to ignore such queued click events? Is there any other way (than setEnable()
) to ignore/eat click processing?
I have checked by putting println statements that each click event is in sync...means all code executes in order.
EDIT
private OnClickListener m_AddClickHandler = new OnClickListener()
{
public void onClick(View v)
{
if(m_bDoAdd)
{
m_bDoAdd = false;
new AddTask().execute();
}
else
logData("Add::OnClick::not clicked");
}
};
private class AddTask extends AsyncTask<Void, Void, Integer>
{
@Override
protected Integer doInBackground(Void... params)
{
logData("doinbg, start"+m_bDoAdd);
int iStatus =Add(m_data);
logData("doinbg, start end, status="+iStatus+"flag="+m_bDoAdd);
return iStatus;
}
@Override
protected void onPostExecute(Integer result)
{
logData("onpostExec, start"+m_bDoAdd);
int iStatus = result;
if (iStatus == 0)
{
Toast.makeText(getApplicationContext(), R.string.strAdded, Toast.LENGTH_SHORT).show();
}
else if (iStatus == 1)
{
Toast.makeText(getApplicationContext(), R.string.strAlreadyExists, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(), R.string.strAddFailed, Toast.LENGTH_SHORT).show();
}
m_bDoAdd = true;
logData("onpostExec, end"+m_bDoAdd);
}
}
void Add()
{
// Add info to db (takes few msecs)
}
I am still not getting "Add::OnClick::not clicked" in log.
Any further clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 onClick 处理程序中尝试一下:
更新:
这是事件在 Android 中的工作方式:
问题的解决方案:
backgroundWorkRunning
)并在完成后清除它。backgroundWorkRunning
标志。如果标志已设置,则不执行任何操作。Try this inside onClick handler:
Update:
This is how events work in Android:
A solution to your problem:
backgroundWorkRunning
) and clear it when done.backgroundWorkRunning
flag. Do nothing if flag is already set.也许您可以使用全局标志,例如默认设置为 false 的布尔变量,当它为 false 时,将执行 addd 条目函数。进入该函数后,将标志设置为 true。希望这有帮助。
May be you can use a global flag like a boolean variale that is set to false by default and when it is false the addd entry function is executed. on entering the function set the flag to true. Hope this helps.
您可以使用静态布尔变量,在其单击事件处理中将其设置为 true。当您单击该按钮时,将其标记为 true,并应用 if 块来检查天气变量是否为 true。当您的任务完成并且代码流即将存在 onClickListener 时,您可以再次将该布尔变量设置为 false。
you can use a static boolean variable which you cab set true in its click event handling. when you click on that button mark it true and also apply an if block which checks weather the variable is true or not. when your task is done and the code flow is about to exist the onClickListener, you can again set that boolean variable to false.