ImageButton背景不以编程方式切换

时间:2017-10-29 04:14:55

标签: java android android-imagebutton

我有两个buttons,它们是以编程方式编码的(我没有XML文件。我正在尝试切换背景onClick,但它并没有切换。

This对我不起作用,因为它已被弃用,this无效。我知道如何用XML来做,但正如我所说,我没有。如何在点击时来回切换ImageButton背景?当我点击buttons时,背景会发生变化,但保持这种状态,直到我再次点击它为止。我希望它立即切换回原来的状态。

buttons的实例化(其间有很多代码。只显示相关的button代码):

static final int leftFireButtonID = 3;
static final int rightFireButtonID = 4;

private boolean isLeftButtonClicked = false;
private boolean isRightButtonClicked = false;

leftFireButton = new ImageButton(this);
rightFireButton = new ImageButton(this);

leftFireButton.setImageResource(R.drawable.red_button);
rightFireButton.setImageResource(R.drawable.red_button);

听众代码:

leftFireButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(soundID,1.0f,0.5f,1,0,1.0f); //Pew pew gunfire

            if(view.getId() == leftFireButtonID) {
                isLeftButtonClicked = !isLeftButtonClicked;
                view.setBackgroundResource(isLeftButtonClicked ? R.drawable.red_button_pressed : R.drawable.red_button); //stays...needs to change back
            }
        }
    });
    rightFireButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            soundPool.play(soundID, 0.5f, 1.0f, 1, 0, 1.0f); //Pew pew gunfire

            if(view.getId() == rightFireButtonID) {
                isRightButtonClicked = !isRightButtonClicked;
                view.setBackgroundResource(isRightButtonClicked ? R.drawable.red_button_pressed : R.drawable.red_button); //stays....needs to change back
            }
        }
    });

使用XML选择器非常容易。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

让自己匆忙。没有做得好。设置onTouchListener以获取ACTION.DOWNACTION.UP。 : - )

leftFireButton.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                leftFireButton.setBackgroundResource(R.drawable.red_button_pressed);
                soundPool.play(soundID,1.0f,0.5f,1,0,1.0f); //pew pew gunfire
                return true;
            case MotionEvent.ACTION_UP:
                leftFireButton.setBackgroundResource(R.drawable.red_button);
                return true;
        }
        return false;
    }
});
rightFireButton.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch(motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                rightFireButton.setBackgroundResource(R.drawable.red_button_pressed);
                soundPool.play(soundID,1.0f,0.5f,1,0,1.0f); //pew pew gunfire
                return true;
            case MotionEvent.ACTION_UP:
                rightFireButton.setBackgroundResource(R.drawable.red_button);
                return true;
        }
        return false;
    }
});