簡體   English   中英

Android:多個可選按鈕

[英]Android: Multiple selectable buttons

我最近開始構建android應用,我想知道是否真的有可能在android中創建一個可選按鈕,如下面鏈接中的圖像所示?

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

我正在嘗試列出可以進行多次檢查的數字列表,但是,我不確定是否必須使用復選框或其他小部件。 如果我能在這里聽到專業人士的提示,那就太好了!

這實際上取決於您要具有的邏輯。 這些數字之間是否存在邏輯聯系? 有條件嗎

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

樣式: Android切換按鈕自定義外觀

然后是一個問題,您要多少個按鈕? 也許數量可變? 然后,您可以將GridViewToggleButton一起使用。

也可以使用RadioButton ,這取決於您。

試試這個樣本

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

使用此方法,您無需維護按鈕狀態,復選框本身也可以維護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM