开发者

change button image in android

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜