Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将同一动画设置为多个按钮_Android_Animation_Button - Fatal编程技术网

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。谢谢,请将第二个作为答案发布,以便我可以接受。