【问题标题】:dynamic checkbox creation动态复选框创建
【发布时间】:2011-04-05 04:02:03
【问题描述】:

我想在运行时在我的 android 应用程序中动态创建一组复选框。当应用程序运行时,除了按钮之外什么都没有显示。我忘记了什么?提前致谢!

public class DataNotificationSurvey extends Activity {
    private Date timeStamp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.datanotifylayout);
        final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton);
        TableLayout t1 = (TableLayout)findViewById(R.id.notificationTableLayout);

        for(int i = 0; i < 5; i++) {
            TableRow tr = new TableRow(this);
            CheckBox chk = new CheckBox(this);
            chk.setText(Integer.toString(i));
            tr.addView(chk);
            t1.addView(tr);
        }

        notifySubmitButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
}    

//My xml layout, will be changing this later to the one posted below to see if it works.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/notificationLinearLayout"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TableLayout
    android:id="@+id/notificationTableLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <Button android:text="Static Button"/>
        </TableRow>
  </TableLayout>
  <Button
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:id="@+id/notifySubmitButton"
  android:text="Submit"></Button>
</LinearLayout>

【问题讨论】:

  • 请同时发布您的layout.xml...代码看起来没问题,所以问题可能就在那里。
  • @harpo:添加了 xml,我稍后会更改以反映作为答案发布的 xml。如果它有效,我会告诉你。但是,如果您在 xml 或其他任何地方发现问题,请告诉我。谢谢

标签: java android dynamic checkbox


【解决方案1】:

这对我来说很好。

public class DynamicTableRowWithCheckBox extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final Button notifySubmitButton = (Button) findViewById(R.id.myButton);
    TableLayout table = (TableLayout) findViewById(R.id.myTable);

    for (int i = 0; i < 3; i++)
    {
      TableRow row = new TableRow(this);
      CheckBox chk = new CheckBox(this);
      chk.setText(Integer.toString(i));
      row.addView(chk);
      table.addView(row);
    }

    notifySubmitButton.setOnClickListener(
        new View.OnClickListener()
        {
          @Override
          public void onClick(View v)
          {
            finish();
          }
        });
  }
}
<?xml version="1.0" encoding="utf-8"?>
<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" />
    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close" />
    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/myTable" />
</LinearLayout>

【讨论】:

  • 明天试试这个,让你知道结果。我的 xml 与您的不同,请更改以查看它是否有效。谢谢。
  • 发布了我当前的 xml,当我有机会时,我会尝试你的 xml,看看会发生什么。但是,如果您发现我到目前为止的内容有任何问题,请告诉我。我可能要到今晚晚些时候才有机会尝试您的 xml。谢谢
  • 这对您显示了什么?因为它只为我显示两个按钮。 for 循环中没有表示“i”值的行和字符串。
  • 这个 xml 文件有效。我没有 android 方向,所以他们都试图在同一行显示。只是简单地忽略了该功能。感谢您的帮助。
【解决方案2】:

将您的 XML 更改为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/notificationLinearLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TableLayout
        android:id="@+id/notificationTableLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TableRow>
            <Button
                android:text="Static Button" />
        </TableRow>
    </TableLayout>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/notifySubmitButton"
        android:text="Submit"></Button>
</LinearLayout>

这会将 TableLayout 的 layout_height 移除到 wrap_content,并移除 TableRow 的高度和宽度设置,因为,"The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT."

【讨论】:

  • 这仍然没有像我想要的那样显示行。它只显示静态按钮和提交按钮。这就是它所显示的全部内容。我也需要它来显示行。
  • 这个xml文件需要设置orientation:vertical值。
  • 啊,是的。 LinearLayout 的默认方向是水平的。
猜你喜欢
  • 2013-01-25
  • 2016-06-16
  • 2011-04-06
  • 2018-05-17
  • 2015-04-16
  • 2010-10-26
  • 2018-09-20
相关资源
最近更新 更多