问题
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