gpt4 book ai didi

android - 为多个按钮设置相同的动画

转载 作者:行者123 更新时间:2023-11-30 01:55:34 24 4
gpt4 key购买 nike

我在按钮的 ACTION_DOWNACTION_UP 事件中使用自定义动画。但是,现在,我正在手动设置 OnTouchListener 到每个 Button 手动使用如下代码:

    myButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View yourButton, MotionEvent theMotion) {
Animation animation;
switch (theMotion.getAction()) {
case MotionEvent.ACTION_DOWN:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_down);
myButton.startAnimation(animation);
break;
case MotionEvent.ACTION_UP:
animation = AnimationUtils.loadAnimation(NickNameScreenActivity.this, R.anim.button_up);
myButton.startAnimation(animation);
break;
}
return false;
}
});

这很麻烦,因为我必须对所有按钮重复此操作。

有没有一种方法可以为所有按钮自动设置它,或者有一种整洁的 XML 方式来做到这一点?

最佳答案

您可以编写自定义 View 扩展 Button 并在 xml 中使用它。也许您可以将 NickNameScreenActivity.this 替换为 getContext()

关于android - 为多个按钮设置相同的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296541/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com