简体   繁体   中英

Radio button drag and drop, Android

I have custom radio button in android xml and now it looks like this:

1

In xml I have two selectors (one for agree another for disagree):

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

<item android:drawable="@drawable/agreechecked" android:state_checked="true"/>
<item android:drawable="@drawable/agreechecked" android:state_focused="true"/>
<item android:drawable="@drawable/agreeunchecked" android:state_pressed="false"/>
<item android:drawable="@drawable/agreeunchecked" android:state_checked="false"/>

</selector>

and this:

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

<item android:drawable="@drawable/disagreechecked" android:state_checked="true"/>
<item android:drawable="@drawable/disagreechecked" android:state_focused="true"/>
<item android:drawable="@drawable/disagreeunchecked" android:state_pressed="false"/>
<item android:drawable="@drawable/disagreeunchecked" android:state_checked="false"/>

</selector>

I want to be able to click on the agree radio and drag the white circle to disagree and make it disagree. It does not have to be any special animation I just need to make it possible. So far it is working just by clicking on either agree or disagree. I need drag and drop. Thanks.

you can use AnimationDrawable for this. Create an xml file and put it in res/drawable folder. Write below code in this xml file.

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/radio0" android:duration="100" />
    <item android:drawable="@drawable/radio1" android:duration="100" />
    <item android:drawable="@drawable/radio2" android:duration="100" />
    <item android:drawable="@drawable/radio3" android:duration="100" />
    <item android:drawable="@drawable/radio4" android:duration="100" />
    <item android:drawable="@drawable/radio5" android:duration="100" />
</animation-list>

Here radio0, radio1, and soon are images with white circle in the left in radio0, slightly right in radio1, little more towards right in radio2 and soon and in the right in radio5.

Then this is what you have to right to start this animation.

ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);

// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

// Start the animation (looped playback by default).
frameAnimation.start();

On the basis of the duration you set in your xml file, you can start a timer accordingly (here 600) to stop the animation. So that animation stops when you reaches the last image. Same your can do in reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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