how to change the image on click of imagebutton?

2019-09-21 19:01发布

问题:

I am make a image button .I need to change the image when I click on image button .Actually it change the background image but only for few seconds .why ?

here is my code

 <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/start"
        android:background="#00ffffff"
        />

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/on" /> <!-- pressed -->

    <item android:drawable="@drawable/off" /> <!-- default -->
</selector>

I need to show on image when I click on image button ..? can I write on java side ? can I write on click listener of image button ?

回答1:

That's why you are using a selector for your button background and the image changes depending on the state of the button. If it is pressed the image will be "on" and in its normal state (no pressed and no focused) the image will be "off".

EDIT:

public class MainActivity extends AppCompatActivity {

ImageButton btn;
boolean isPressed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (ImageButton) findViewById(R.id.btn);
    btn.setBackgroundResource(R.drawable.normal);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPressed){
                v.setBackgroundResource(R.drawable.normal);
            }else{
                v.setBackgroundResource(R.drawable.pressed);
            }
            isPressed = !isPressed; // reverse
        }
    });


}


  <ImageButton
    android:id="@+id/btn"
    android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


回答2:

Actually it change the background image but only for few seconds .why ?

I think it's obvious,those items in drawable are for states that ImageButton is pressed by user or in normal mode.

can I write on java side ? can I write on click listener of image button ?

Yes, and yes again :D if you want to change it when user presses it, just do it like this :

XML layout:

<ImageButton android:id="@+id/favorite" android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/off" android:background="#00ffffff" />

Java side (eg. in you onCreate method)

ImageButton favorite;
boolean isFav = false;
public void onCreate(Bundle savedInstanceState){
    ///...
    favorite = findViewById(R.id.favorite);
    favorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            isFav = !isFav;
            if (isFav){
                favorite.setImageResource(R.drawable.on);
            }
            else{
                favorite.setImageResource(R.drawable.off);
            }
        }
    });
    ///...
}


回答3:

change the source of the image by providing an absolute path or an internet url dynamically in the onClickListener of the image.

image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Here before changing the image you can check if the appropriate                                    //image is set to the view.
                image.setImageResource(R.drawable.on);

        }
});

and also can be like this

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isPressed){
                v.setBackgroundResource(R.drawable.normal);
            }else{
                v.setBackgroundResource(R.drawable.pressed);
            }
            isPressed = !isPressed; // reverse
        }
    });