按钮单击以更改单击按钮的背景图像

时间:2011-10-24 15:42:21

标签: android button if-statement onclick

我正在尝试更改单击按钮的背景图像。我尝试切换其图像的按钮与单击的按钮相同。我最终希望程序测试当前的背景图像,并根据测试结果将其更改为另一张图片。

final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //toggle picture
        if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
            testButton.setBackgroundResource(R.drawable.alternatepicture);
        }   

        else {  
            testButton.setBackgroundResource(R.drawable.fakpicture);
        }
    }//end void onClick

});//end test button on click listener

5 个答案:

答案 0 :(得分:1)

testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));

然而,ToggleButton可能更适合你的情况。

答案 1 :(得分:1)

正如其他人所说,你的equals方法是将按钮本身与图像进行比较,但你需要比较背景可绘制的内容。

我建议加载你想要使用的图像drawables,然后再使用它们的引用使事情变得更清晰,如下所示:

    final Drawable first = getResources().getDrawable(
            android.R.drawable.arrow_up_float);
    final Drawable second = getResources().getDrawable(
            android.R.drawable.arrow_down_float);

    final Button testButton = (Button) findViewById(R.id.toggleButton);
    testButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (testButton.getBackground().equals(first)) {
                testButton.setBackgroundDrawable(second);
            } else {
                testButton.setBackgroundDrawable(first);
            }
        }
    });

答案 2 :(得分:1)

正如其他朋友所回答的那样,最好使用Android中的ToggleButton

在您的情况下,如果您想保留您的代码,那么您的方法应该是这样的:

final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 ) 
testButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //toggle picture
        if (status == 0) {
            testButton.setBackgroundResource(R.drawable.alternatepicture);
            status=1 ; // change the status to 1 so the at the second clic , the else will be executed
        }   

        else {  
            testButton.setBackgroundResource(R.drawable.fakpicture);
            status =0;//change the status to 0 so the at the second clic , the if will be executed
        }
    }//end void onClick

});//end test button on click listener

答案 3 :(得分:0)

你可以简单地使用ToggleButton:Android ToggleButton并使用StateList来更改背景:StateList使用check属性。

答案 4 :(得分:0)

您可以使用按钮或图像按钮..

private ImageButton mod1,mod2;

mod1        = (ImageButton) findViewById(R.id.mod1);
mod2        = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);



public void onClick(View v) {

    mod1.getDrawable().clearColorFilter();
    mod2.getDrawable().clearColorFilter();

    switch (v.getId()) {
        case R.id.mod1:
            mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
            break;
        case R.id.mod2:
            mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
            break;
    }
}
相关问题