将动作传递给下一个按钮

时间:2015-01-07 21:55:43

标签: java android performance

我有5个按钮。 当前按钮,按钮1闪烁。其他人不活跃。用户单击按钮1后,按钮1上的动画停止,按钮2变为活动状态并闪烁 问题...第一个按钮闪烁,但它没有跳到按钮2.我的case语句设置错了吗? 这是我到目前为止所得到的......

非常感谢任何帮助。

private int activeButton =1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_workout_one);

    //setting up the buttons
    Button Button1 = (Button) findViewById(R.id.button_1);
    Button Button2 = (Button) findViewById(R.id.button_2);
    Button Button3 = (Button) findViewById(R.id.button_3);
    Button Button4 = (Button) findViewById(R.id.button_4);
    Button Button5 = (Button) findViewById(R.id.button_5);

    //method to switch between buttons
    buttonClickHandler(Button1, Button2, Button3, Button4, Button5);
}

private void buttonClickHandler(Button a, Button b, Button c, Button d, Button e){

    switch(activeButton++){
        case 1:
            //method to make the button animate etc
            makeButtonActive(a);
            break;
        case 2:

            makeButtonActive(b);
            break;
        case 3:

            makeButtonActive(c);
            break;
        case 4:

            makeButtonActive(d);
            break;
        case 5:

            makeButtonActive(e);
            break;
    }
}

private void makeButtonActive(Button bu){
    //setting up the animation
    Animation mAnimation = new AlphaAnimation(1, 0);
    mAnimation.setDuration(1000);
    mAnimation.setInterpolator(new LinearInterpolator());
    mAnimation.setRepeatCount(Animation.INFINITE);
    mAnimation.setRepeatMode(Animation.REVERSE);
    bu.startAnimation(mAnimation);
    bu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //providing the after click stuff
            v.clearAnimation();
            v.setBackgroundColor(0xFF00FF00);
            v.setFocusable(false);
            v.setFocusableInTouchMode(false);
            v.setClickable(false);
            v.setEnabled(false);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

它的发生是因为启动动画的方法只被调用一次。单击其他按钮时需要调用它。

请致电

buttonClickHandler(Button1, Button2, Button3, Button4, Button5);
onClick()

并定义班级的所有按钮

修改

private int activeButton =1;
Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_workout_one);

    //setting up the buttons
    Button1 = (Button) findViewById(R.id.button_1);
    Button2 = (Button) findViewById(R.id.button_2);
    Button3 = (Button) findViewById(R.id.button_3);
    Button4 = (Button) findViewById(R.id.button_4);
    Button5 = (Button) findViewById(R.id.button_5);
    buttonClickHandler(Button1, Button2, Button3, Button4, Button5);
}

private void buttonClickHandler(Button a, Button b, Button c, Button d, Button e){ 
    switch(activeButton++){
        case 1:
            //method to make the button animate etc
            makeButtonActive(a);
            break;
        case 2:
            makeButtonActive(b);
            break;
        case 3:
            makeButtonActive(c);
            break;
        case 4:
            makeButtonActive(d);
            break;
        case 5:
            makeButtonActive(e);
            break;
    }
}

private void makeButtonActive(Button bu){
    //setting up the animation
    Animation mAnimation = new AlphaAnimation(1, 0);
    mAnimation.setDuration(1000);
    mAnimation.setInterpolator(new LinearInterpolator());
    mAnimation.setRepeatCount(Animation.INFINITE);
    mAnimation.setRepeatMode(Animation.REVERSE);
    bu.startAnimation(mAnimation);
    bu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //providing the after click stuff
            v.clearAnimation();
            v.setBackgroundColor(0xFF00FF00);
            v.setFocusable(false);
            v.setFocusableInTouchMode(false);
            v.setClickable(false);
            v.setEnabled(false);
            buttonClickHandler(Button1, Button2, Button3, Button4, Button5);
        }
    });
}

注意:您不应该使用大写字母

启动变量名称