Android切换按钮 - 绿色到红色

时间:2012-08-16 15:35:26

标签: android android-button

如何按下按钮变为绿色,按键再次变为红色。

1 个答案:

答案 0 :(得分:1)

@Override
    public boolean onTouch(final View view, MotionEvent event) {

    final int action = event.getAction();

        if(view.getId()==R.id.yourButton){
            if(action == MotionEvent.ACTION_DOWN)
                  yourButton.setBackgroundResource(R.drawable.ic_button_green);
            if(action == MotionEvent.ACTION_UP)
                  yourButton.setBackgroundResource(R.drawable.ic_button_red);
        }

 }