简体   繁体   English

复选框在带有动画的android中不起作用

[英]Checkbox not working in android with animation

I just created this checkbox with animation following this article https://android.jlelse.eu/using-statelistanimators-to-create-instagram-like-animation-db2676c9391 and it's not working when I call checkbox.setChecked(true) from my java code!我刚刚按照这篇文章https://android.jlelse.eu/using-statelistanimators-to-create-instagram-like-animation-db2676c9391创建了这个带有动​​画的复选框,当我从我的代码! I want that when I click on like button!当我点击喜欢按钮时,我想要那个! If the dislikeButton was checked true then it must be check false.如果 dislikeButton 被检查为真,那么它必须被检查为假。 And when I click dislike button, if like was checked true then it must be check false!当我点击不喜欢按钮时,如果喜欢被选中为真,那么它必须被选中为假! Here is the checkBox XML code:这是复选框 XML 代码:

<CheckBox
                android:layout_toLeftOf="@+id/save_btn"
                android:id="@+id/dislike_btn"
                android:layout_marginHorizontal="12dp"
                android:background="@drawable/dislike_icon"
                android:alpha="0.8"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:clickable="true"
                android:button="@null"
                android:stateListAnimator="@animator/like_animation"

                android:focusable="true"
                />
            <CheckBox
                android:layout_toLeftOf="@+id/dislike_btn"
                android:id="@+id/like_btn"
                android:layout_marginHorizontal="12dp"
                android:background="@drawable/like_icon"
                android:alpha="0.8"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:clickable="true"
                android:button="@null"
                android:stateListAnimator="@animator/like_animation"
                android:focusable="true"
                />

and here is like_icon and dislike_icon code:这是 like_icon 和 dislike_icon 代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
        android:drawable="@drawable/dislike_icon_red"/>
    <item android:state_checked="false"
        android:drawable="@drawable/dislike_icon_black"/>

</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
        android:drawable="@drawable/thumps_up_red"/>
    <item android:state_checked="false"
        android:drawable="@drawable/thumps_up_black"/>

</selector>

and the animation code:和动画代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <set xmlns:android="http://schemas.android.com/apk/res/android">
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.525"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1.525"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="4dp"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.0"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="1.0"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="0dp"
                android:valueType="floatType" />

        </set>

    </item>

    <item>
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.525"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1.525"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="4dp"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.0"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="1.0"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="0dp"
                android:valueType="floatType" />
        </set>
    </item>
</selector>

and this is what I am calling from my java code!这就是我从我的 Java 代码中调用的!

like.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (like.isChecked()){
            Log.i("ELA","like check called");
            dislike.setChecked(false);
        }
    }
});
dislike.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (dislike.isChecked()){
            like.setChecked(false);
        }
    }
});

I tried .setOnClickListener too!我也试过 .setOnClickListener !

Set the checked and unchecked states in the animator.在动画制作器中设置选中和未选中状态。 The below example has checked state.以下示例已检查状态。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <set>
            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:valueTo="1.525"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:valueTo="1.525"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:valueTo="4dp"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleX"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="1.0"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="scaleY"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="1.0"
                android:valueType="floatType" />

            <objectAnimator
                android:duration="@android:integer/config_shortAnimTime"
                android:propertyName="translationZ"
                android:startOffset="@android:integer/config_shortAnimTime"
                android:valueTo="0dp"
                android:valueType="floatType" />
        </set>
    </item>
</selector>

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

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