繁体   English   中英

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

[英]Button click to change background image of clicked button

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

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

尝试

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

但是,ToggleButton可能更适合您的情况。

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

我建议加载要使用的图像可绘制对象,然后在以后使用它们的引用使内容更清晰,如下所示:

    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);
            }
        }
    });

正如其他朋友回答的那样,最好在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

您可以使用“按钮”或“图像”按钮。

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;
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM