安卓:Button.performClick的视觉版 [英] Android: Visual version of Button.performClick
问题描述
我知道我可以通过调用performClick手动触发OnClickListener.onClick一个按钮,在code,但是这似乎并没有让它在视觉上出现,因为它是被点击。我正在寻找一种方式来手动做一个按钮,看起来好像它被点击。我是否需要手动更改背景绘制和无效(然后在Handler.postDelayed回调再次更改),或者是有这样做的更框架-Y方式?
I know that I can trigger the OnClickListener.onClick of a Button manually in code by calling performClick, but that doesn't seem to make it visually appear as it's been clicked. I'm looking for a way to manually make a button appear as if it's been clicked. Do I need to manually change the background drawable and invalidate (and then change it back again on a Handler.postDelayed call), or is there a more framework-y way of doing this?
修改
我知道如何使按钮有不同的可绘制出现pressed当用户启动preSS。问题是这样的:
I know how to make the button have different drawables to appear pressed when the user initiates the press. The question is this:
的有一个简单的方法,使一个按钮出现$ P $编程pssed时没有物理$ P $用户pssed?的
SOLUTION
我刚子类按钮,并认识到它的按钮是正常的背景为StateListDrawable和绘制对象被用作pressed状态。我公开该手动设置背景绘制了pssed $ P $的方法,我用Handler.postAtTime使其返回到它的正常的背景,因此它可以被再次用作常规按钮时,我完成了。
I just subclassed Button and made the button aware of it's normal background as a StateListDrawable and the Drawable that is used as the pressed state. I expose a method that manually sets the background to the "pressed" drawable, and I use Handler.postAtTime to have it return to it's normal background so it can be used as a regular button again when I'm done.
推荐答案
虽然这个问题很老了,我想我还是会回答。你不需要子类化视图。第一个电话 performClick()
,那么你可以通过 view.set pressed设置按钮的pressed状态(真);
,然后重新设置几毫秒以后这样的。
Although this question is very old, I figured I'll still answer it. You don't need to subclass the View. First call performClick()
, then you can set the button's pressed state via view.setPressed(true);
and then reset it a couple of milliseconds later like this.
handler.postDelayed(new Runnable() {
@Override
public void run() {
view.setPressed(false);
}
}, 100);
这篇关于安卓:Button.performClick的视觉版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!