如何使用ToggleButton android

时间:2012-04-27 14:03:29

标签: android listview togglebutton

我在xml中有以下ToggleButton:

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

在我的扩展listview的类中,我想根据一些标准以编程方式将togglebutton设置为active / inactive。我想指出我不知道用户能够点击togglebutton并将其设置为活动或非活动状态。怎么做?需要一些帮助。欣赏!

2 个答案:

答案 0 :(得分:3)

要使按钮无法点击,只需禁用切换按钮:

mToggleButton.setEbabled(false);

或将其设为不可点击:

mToggleButton.setClickable(false);

并以编程方式更改按钮状态,请使用:

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

答案 1 :(得分:1)

ToggleButton.setActivated的答案对我不起作用。我使用以下内容使其工作:

 ToggleButton.setChecked(false);

希望这可以帮助其他人解决同样的问题。