简体   繁体   English

更改切换按钮图片onclick

[英]Change toggle button image onclick

I know it has already been asked and answered here and here . 我知道这里这里已经有人问过并回答 I have tried both, but none of them is working right for me. 我都尝试过,但没有一个适合我。

I have a favorite button, If it is pressed I set the item to favorite in database and replace the image of the toggle button , and vice versa. 我有一个收藏夹按钮,如果按下它,则将项目设置为database收藏夹,并替换toggle button的图像,反之亦然。 Here is how I am doing it: 这是我的做法:

<ToggleButton
   android:id="@+id/btnFavorite"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textOn=""
   android:textOff=""
   android:layout_marginRight="5dp"
   android:background="@drawable/favorite_btn_style" />

Here is my favorite_btn_style.xml : 这是我的favorite_btn_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/favourit_blue_btn" android:state_checked="true"/>
 <!-- pressed -->

    <item android:drawable="@drawable/favourit_dark_btn"/>
 <!-- default/unchecked -->

</selector>

In oncreate I check if the item is already set to favorite, then setchecked to true : oncreate我检查项目是否已设置为收藏,然后将其setcheckedtrue

if (movieObj.getIsFav().intValue() == 1) {
            btnFav.setChecked(true);
        }

Here is my onclicklistener on the button: 这是我在按钮上的onclicklistener

btnFav.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (!btnFav.isChecked()) {
                    btnFav.setChecked(true);
                    // set favorite
                    dbHelper.updateMovieFavorite(movieObj.getId().intValue(), 1);
                } else {
                    btnFav.setChecked(false);
                    // set favorite
                    dbHelper.updateMovieFavorite(movieObj.getId().intValue(), 0);
                }
            }
        });

Function gets called, and executed fine, but no change in image.. What I am doing wrong? 函数被调用并执行良好,但是图像没有变化。我在做什么错?

Delete both btnFav.setChecked(true) and btnFav.setChecked(false) in your OnClick method. 在您的OnClick方法中删除btnFav.setChecked(true)btnFav.setChecked(false) It is a togglebutton which toggles the setChecked on its own by every click and you reset it to the old value. 这是一个切换按钮,每次单击即可单独切换setChecked,并将其重置为旧值。 So in your case it always has the same value(the start value). 因此,在您的情况下,它始终具有相同的值(起始值)。
I would suggest you rather use setOnCheckedChangeListener instead of onClickListener . 我建议你还是用setOnCheckedChangeListener代替onClickListener

Create a file button_toggle.xml in your res/drawable folder 在res / drawable文件夹中创建一个button_toggle.xml文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="false"
        android:drawable="@drawable/ic_slide_switch_off" />
    <item
        android:state_checked="true"
        android:drawable="@drawable/ic_slide_switch_on" />
</selector>

Try to use android:button="@drawable/favorite_btn_style" and android:background="@android:color/transparent" combination. 尝试使用android:button =“ @ drawable / favorite_btn_style”android:background =“ @ android:color / transparent”组合。 To customize the checkbox, radio and toggle button you should use android:button instead of android:background. 要自定义复选框,单选和切换按钮,您应该使用android:button而不是android:background。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM