动态复选框创建

发布于 2024-10-30 07:41:57 字数 1959 浏览 0 评论 0原文

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

I am wanting to create a set of checkboxes dynamically during run time in my android application. When the application runs nothing shows up except the button. What am i forgetting? Thanks in advance!

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-11-06 07:41:58

这对我来说效果很好。

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>

This works fine for me.

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>
蒲公英的约定 2024-11-06 07:41:58

将 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 的高度和宽度设置,因为 "TableRow 的子级不需要在 XML 文件中指定布局宽度和布局高度属性。TableRow 始终强制这些值分别为 MATCH_PARENT 和 WRAP_CONTENT。"

Change your XML to:

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

This removes the layout_height of your TableLayout to wrap_content, and it removes the height and width settings of your TableRow, because, "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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文