开发者

Image Change On Button Click In Android

开发者 https://www.devze.com 2023-04-06 20:30 出处:网络
I\'m new to android and am making a simple app.I\'m trying to change the image (in an imageview) on a button click.

I'm new to android and am making a simple app. I'm trying to change the image (in an imageview) on a button click.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    frown = (ImageView)findViewById(R.id.imageView1);


}
public void action(View view)
{
    Toast.makeText(getApplicationContext(), buttontest, Toast.LENGTH_SHORT).show();
    frown.setImageResource(R.drawable.chimpy);

}

"action" is being called via XML with the "android:onClick"[insert met开发者_StackOverflow社区hod here]" for my button

The button works fine and I get my toast, but the image stays the same.


Try changing the drawable to something standard e.g. android.R.drawable.btn_default. Does it chnage Now? I am sure you are having some issues with R.drawable.chimpy.


You must use .png image and you can go with the following code snippet:

frown.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            frown.setBackgroundDrawable(R.id.chimpy);
        }
});

If it is not working, just tell me...!

0

精彩评论

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