android按钮在播放动画时不可点击
我有一个按钮,当该按钮播放动画时,我无法单击按钮。我已经设置了单击侦听器和触摸侦听器,但在调试模式下它没有进入 OnClick 和 onTouch 方法。你知道为什么吗?谢谢
编辑:我尝试过类似的方法:
AsyncTask task = new AsyncTask() {
@Override
protected Object doInBackground(Object... objects) {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
toast.show();
}
});
return null;
}
;
};
task.execute(button1);
但它不起作用
编辑:这是完整的源代码
I have a button and while this button is playing an animation, I'm not able to click on button. I've set click listener and touch listener but in debug mode it's not entering in OnClick and in onTouch methods. Do you know why? Thanks
edit: I've tried something like:
AsyncTask task = new AsyncTask() {
@Override
protected Object doInBackground(Object... objects) {
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast toast = Toast.makeText(MyActivity.this, button1.getText(), Toast.LENGTH_SHORT);
toast.show();
}
});
return null;
}
;
};
task.execute(button1);
but it's not working
Edit: here is the full source code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 Android 3.0 之前,使用任何动画类只会更改视图的绘制位置 - 它不会在动画期间(或之后)调整其可触摸边界:
http://android-developers.blogspot.com/2011/02/animation-in- honeycomb.html
您可以:
Before Android 3.0, using any of the animation classes only changes where the view is drawn - it won't adjust its touchable-bounds during (or after) the animation:
http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
You could:
您面临着与我最近相同的问题...当您在按钮或任何其他视图上应用动画并使用 setFillAfter(True) 时,这意味着视图的图像被移动而不是实际的视图,这就是为什么它不监听您的点击侦听器因为它只是视图的图像而不是您的实际视图,您必须根据您的情况执行类似我在回答我自己的问题时解释的操作...意味着您还必须将实际视图移动到动画的结束位置并使用 setFillAfter (假)这样当您在动画后单击时,它应该是一个实际的视图,而不仅仅是android用于动画目的的图像,
请检查此链接...
EditText 在动画后卡住并重新滚动......?
在您的代码中使用 setFillafter(false) 并通过某种方式(例如设置边距)将按钮实际放置在动画的结束位置或根据您的布局使用适当的用于放置的属性。通过应用这些更改,您的点击侦听器将完美运行。
==>如果您尝试让按钮的点击监听器在移动(动画)时工作,那么据我所知这是不可能的,因为android仅使用您视图的图像来执行动画而不是实际的。
you are facing same issue that i was recently ... when you apply animation on button or any other view and use setFillAfter(True) it means that the image of view is moved not the actual view thats why its not listening to your click listener because its just image of view not your actual view you have to do something like that i explained in answer to my own question according to your situation... means you have to also move the actual view on the end place of animation and use setFillAfter(false) so that when you click after anmation then it should be an actual view not just image used for animation purpose by android
check this link....
EditText stucks after animation and alive back on scrolling......?
In your code use setFillafter(false) and actually place your button at end position of animation by somehow like setting margin or according to your layout use appropriate properties for placement. By Applying these changes your click listener will work perfectly.
==> if you are trying that your button's click listener work while its moving (being animate) then as far as i know its not possible because android uses just image of your view to perform animation not the actual.
这可能是线程问题。您应该阅读事件调度线程以及如何通过阅读无痛线程 并查看 AsyncTask JavaDoc。
简而言之:主线程不应该被阻塞,因为它用于响应 UI 事件,例如按下按钮。因此,每当您执行的操作需要超过几毫秒时,就应该在另一个线程中异步执行。
This might be a threading problem. You should read Event dispatch thread and how to do thing in android async by reading painless threading and take a look at AsyncTask JavaDoc.
In short: the main thread should not be blocked, since it is used for responing to UI events, such as button presses. Therefore, whenever you do something that take more than some milliseconds, you should do it asynchroniously in another thread.
1)您可以一次为一个对象处理一个事件。就像动画在按钮上播放一样,这意味着您无法单击该按钮,直到一项工作完成(据我所知,动画位于按钮上而不是屏幕的其他部分)
2)其次,如果您在屏幕的其余部分上播放并且您想在单击按钮时停止它。那么我认为这是焦点问题。一旦将 onfoucslistener 设置为按钮并检查当您单击它时它是否获得焦点?
1)You can handle one event at one time for one object.Like animation is playing on you button that means you can not click on that untill one work get completed(As i understand you animation is on button not on other part of screen)
2)Second if you have playing on rest part of screen and you want to stop it on click of button.Then i think its a problem of focus.once set onfoucslistener to button and check that when you are clicking it Is it getting focus?
我会将动画放入异步任务中,然后我认为按钮单击应该在主线程上正常处理。因为你现在的做法是:动画启动并阻塞主线程。这意味着点击事件无法在异步任务中执行
I would put the animation into the async task and then the button click should be handled normally on the main thread I think. Because the way you do it at the moment is: The animation starts and blocks the main thread. This means that the click event can't be excecuted in the async task
您必须尝试使用 AsyncTask 。如果没有它,Android 编程将会是严重的可用性问题。
请参阅此链接了解如何使用异步任务。
这里有一个例子:
MODIFIED
你必须始终扩展 Asynctask Activity ,因为你试图通过它传递参数,所以你必须在扩展类的头部定义它们:
然后第一个是 doInBAckground 参数,接下来是 onPreExecute 参数,最后一个是 onPostExecute 参数。这样你就可以像按钮一样发送参数。
您可以在此处或在我的第一篇文章中了解更多信息关联。
You must try to use AsyncTask . Programming Android without it would be and horrible usability serious problem.
See this link for how use an asynctask.
Here an example:
MODIFIED
You must always extend the Asynctask Activity , since you are trying to pass params through it , you must define them in the head of your extended class :
Then first is the doInBAckground param , next is the onPreExecute param , and the last is the onPostExecute param . That way you can send the params like a Button.
You can learn more about here or in my first link.