How to toggle background image on button click?

前端 未结 4 975
无人及你
无人及你 2021-01-26 04:54

I have this code:

button1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
          


        
相关标签:
4条回答
  • 2021-01-26 05:24

    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>
    
    0 讨论(0)
  • 2021-01-26 05:32

    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.

    0 讨论(0)
  • 2021-01-26 05:40

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-26 05:44

    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
    
    0 讨论(0)
提交回复
热议问题