开发者

change button image in android

开发者 https://www.devze.com 2023-02-09 21:50 出处:网络
I want to change the image of a button in my code. I found this can be done in xml: please check this link

I want to change the image of a button in my code. I found this can be done in xml:

please check this link

but the image will not stay on after I rel开发者_如何学Ceased the button. I want to click the button and the button change to a new image. Can this be done?


in onClick method you need to change the Button's image, this way..

public void onClick(View v) {
    if(v==buttonName){
        buttonName.setBackgroundResource(R.drawable.imageName);
    }       
}


Assuming an ImageButton... You can change the background image in an onClick listener, similar to the following:

myButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //set button image
                myButton.setImageBitmap(myBitmapFile)
            }
        });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号