Android 将同一动画设置为多个按钮
我在按钮的Android 将同一动画设置为多个按钮,android,animation,button,Android,Animation,Button,我在按钮的ACTION\u DOWN和ACTION\u UP事件中使用自定义动画。 但是,现在,我正在手动设置每个按钮的OnTouchListener,代码如下: myButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View yourButton, MotionEvent theMotion) { Anim
ACTION\u DOWN
和ACTION\u UP
事件中使用自定义动画。
但是,现在,我正在手动设置每个按钮的OnTouchListener
,代码如下:
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方法可以做到这一点 您可以编写一个自定义视图,扩展按钮
,并在xml中使用它。也许你可以用getContext()
替换昵称屏幕活动。如何将匿名类重构为扩展视图的内部类。OnTouchListener
类并将其用于所有按钮?谢谢,这是一个好主意。在这种情况下,我似乎无法避免为每个按钮创建一个变量。那么,编写一个扩展按钮的自定义视图,并在xml中使用它如何?也许你可以用getContext()
替换昵称ScreenActivity。谢谢,请将第二个作为答案发布,以便我可以接受。