how to work with a ToggleButton android

断了今生、忘了曾经 提交于 2019-12-11 05:56:07

问题


I have the following ToggleButton in xml:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="46dp"
        android:gravity="center" >

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="ToggleButton"
            android:textOff="Inactive"
            android:textOn="Active" />

        <TextView
            android:id="@+id/label_note"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_marginRight="38dp"
            android:layout_marginLeft="2dip"
            android:layout_toRightOf="@+id/check_note"
            android:text="@+id/label"
            android:textSize="25px" />
        ...
</RelativeLayout>

In my class that extends a listview I would like to programatically set the togglebutton on active/inactive based on some criterias. I would like to specify that I do not what the user to be able to click on the togglebutton and set it on active or inactive. How to do that? Need some help. Appreciate!


回答1:


to make the button not clickable, just disable the toggle button:

mToggleButton.setEbabled(false);

or set it not to be clickable:

mToggleButton.setClickable(false);

and for programatically change the button state, use:

mToggleButton.setActivated(true); 
mToggleButton.setActivated(false);



回答2:


The answer with ToggleButton.setActivated didn't work for me. I used the following to get it to work:

 ToggleButton.setChecked(false);

Hope this helps anyone else with the same issues.



来源:https://stackoverflow.com/questions/10352133/how-to-work-with-a-togglebutton-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!