簡體   English   中英

Android TableLayout組動態單選按鈕

[英]Android TableLayout group dynamic radioButtons

如何將所有動態創建的單選按鈕分組?

while (cursor.moveToNext()) {
    TableRow row = new TableRow(this);
     // CheckBox chk = new CheckBox(this);
      RadioButton  radioBtn =  new RadioButton(this);
     // chk.setText(cursor.getString(cursor.getColumnIndex("from_text")));
      radioBtn.setText(cursor.getString(cursor.getColumnIndex("from_text")));
      radioBtn.
      row.addView(radioBtn);
      table.addView(row);
}

Xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

        <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/myTable"/>         
</LinearLayout>

要將單選按鈕作為一個組進行處理,必須將它們放置在RadioGroup布局中。 如果要將單選按鈕放在單獨的表格單元中,則必須自己實現組行為。

您可以使用RadioButton.SetOnCheckedChangeListener()來注冊一個響應已檢查事件的偵聽器。

您可以查看RadioGroup源代碼,以了解如何使用RadioGroup布局完成此操作。

暫無
暫無

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

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