How to toggle background image on button click?

2019-03-04 22:00发布

问题:

I have this code:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        button1.setBackgroundResource(R.drawable.detailpressed);
        Chapter_sync.add(chapid);
    }

What I am trying to do is toggle all the methods called in the following clicklistener.

Eg first time when I Click this button the setBackgroundResource(R.drawable.detailpressed) is called and on the next click same metod is called with different drawable.

Something like toggle button. Someone good at this plz help?

回答1:

you can take a variable

int i=0;

it will increase with every click.

if(i%2==0)
   set one image
else 
   set another image


回答2:

How about creating an array of the drawable's IDs and saving an index:

private final int[] myDrawables = {R.drawable.detailpressed, R.drawable.detailpressed1, ...};
//...
button1.setOnClickListener(new OnClickListener() {
    int index = 0;
    @Override
    public void onClick(View v) {
        button1.setBackgroundResource(myDrawables[index++ % myDrawables.length]);
        Chapter_sync.add(chapid);
    }
}


回答3:

declare variable as

boolean isOddClicked = true;

And update your click listener as

button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //Do stuff here for chnaging background of button
            if(isOddClicked) {
                button1.setBackgroundResource(R.drawable.detailpressed);
                isOddClicked = false;
            } else {
                button1.setBackgroundResource(R.drawable.detailpressed_SECOND_IMAGE);
                isOddClicked = true;
            }

            //Do your task
            Chapter_sync.add(chapid);
        }

NOTE: If your requirement moves between two images then you can use toggle button and customize it. It will work for same as your requirement.



回答4:

if xml as the following

    <code>

        <Button
            android:id="@+id/btnListView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/list_view"
            android:onClick="switchToListView"
            android:visibility="visible"
            />

        <Button
            android:id="@+id/btnGridView"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:background="@drawable/grid_view"
            android:onClick="switchToGridView"
            android:visibility="gone"
            />
    <code>


handling Code will be like

<code>
public void switchToListView(View view) {
    (Button) findViewById(R.id.btnListView).setVisibility(View.GONE);
    (Button) findViewById(R.id.btnGridView).setVisibility(View.VISIBLE);
}
public void switchToGridView(View view) {
    (Button) findViewById(R.id.btnGridView).setVisibility(View.GONE);
    (Button) findViewById(R.id.btnListView).setVisibility(View.VISIBLE);

}

</code>