Android:多个可选按钮

时间:2015-03-03 09:30:19

标签: java android

我最近开始构建Android应用程序,我想知道它是否真的可以在android中创建一个可选按钮,就像下面链接中的图像一样?

https://lh4.ggpht.com/ouHPcTcFzsdYrTU09pStGBicxgX_cki613g5Eq3loYCh2TOXzqpfeyWnOdlLuc8eNS0=h900-rw

我正在尝试列出一些可以进行多项检查的数字,但是,我不确定是否必须使用复选框或其他小部件。如果我能听到专业人士的一些提示,那就太好了!

2 个答案:

答案 0 :(得分:1)

这实际上取决于你想要的逻辑。这些数字之间是否有任何逻辑联系?任何条件?

您还可以使用ToggleButtonhttp://developer.android.com/guide/topics/ui/controls/togglebutton.html

样式: Android toggle button custom look

然后问题是你想要多少个按钮?也许是一个可变数额?然后,您可以使用GridViewToggleButton s。

还有可能使用RadioButton,这取决于你。

答案 1 :(得分:0)

试试这个样本

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view"/>

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view"/>

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view" />

    <CheckBox
        android:id="@+id/checkBox5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:button="@drawable/button_change_view" />
</LinearLayout>

</RelativeLayout>

button_change_view.xml是: -

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/location_hide"
      android:state_checked="true" />
<item android:drawable="@drawable/location_show"
      android:state_checked = "false" />
</selector>

使用此功能,您不需要维护按钮状态,复选框可以自行维护。