【问题标题】:how to work with a ToggleButton android如何使用 ToggleButton android
【发布时间】:2012-05-08 06:58:06
【问题描述】:

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

在我扩展列表视图的类中,我想根据一些标准以编程方式将切换按钮设置为活动/非活动状态。我想指定我没有什么用户能够单击切换按钮并将其设置为活动或非活动。怎么做?需要一些帮助。欣赏!

【问题讨论】:

    标签: android listview togglebutton


    【解决方案1】:

    要使按钮不可点击,只需禁用切换按钮:

    mToggleButton.setEbabled(false);
    

    或将其设置为不可点击:

    mToggleButton.setClickable(false);
    

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

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

    【讨论】:

    • 谢谢!我会试试看。有没有办法禁用 xml 布局中的按钮?
    • 是的,您也可以在 xml 文件上执行此操作 - 只需使用属性 - android:clickable="false" 或 android:enabled="false"
    • 在我的切换按钮列表中没有 setActivated
    【解决方案2】:

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

     ToggleButton.setChecked(false);
    

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

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-05
      • 1970-01-01
      • 2014-02-01
      • 2012-04-21
      • 1970-01-01
      相关资源
      最近更新 更多