动态复选框创建

2024-02-10

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

这对我来说效果很好。

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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动态复选框创建 的相关文章

  • Firebase ANR - 甚至不使用 firebase

    随着 GMS 的最新更新 引入了新的 ANR 如果您退出应用程序 然后返回 则 ANR 就会初始化 线程跟踪似乎指向 Firebase 但我什至从未开始使用 Firebase 我猜它一定是新的 GMS 包的一部分 06 11 00 34 0
  • Java 8 中函数式接口的使用

    这是来自的后续问题Java 8 中的 双冒号 运算符 https stackoverflow com questions 20001427 double colon operator in java 8其中 Java 允许您使用以下方式引用
  • 导航抽屉默认片段

    我是一名新手开发人员 我正在将导航抽屉与 android support v7 集成到我的应用程序中 我有一个问题 当我启动应用程序时 主要布局是这样的
  • Git 无法识别重命名和修改的包文件

    我有一个名为的java文件package old myfile java 我已经通过 git 提交了这个文件 然后我将我的包重命名为new所以我的文件在package new myfile java 我现在想将此文件重命名 和内容更改 提交
  • C#动态支持吗?

    看完之后这个帖子 https stackoverflow com questions 2674906 when should one use dynamic keyword in c sharp 4 0k和链接 我还有 2 个问题 问题 1
  • Cloudfoundry:如何组合两个运行时

    cloundfoundry 有没有办法结合两个运行时环境 我正在将 NodeJS 应用程序部署到 IBM Bluemix 现在 我还希望能够执行独立的 jar 文件 但应用程序失败 APP 0 bin sh 1 java not found
  • 为什么Android应用程序在发布到市场后尺寸会增加?

    我最近在 Android 市场上发布了我的应用程序 显示应用程序大小为 5 4MB 而实际 apk 大小为 2 8MB 为什么显示多出2MB 我应该如何限制我的应用程序大小 请帮我 您的应用程序大小会增加 因为您使用了复制保护选项ON在发布
  • 在 Spring Boot Actuator 健康检查 API 中启用日志记录

    我正在使用 Spring boot Actuator APIproject https imobilenumbertracker com 拥有一个健康检查端点 并通过以下方式启用它 management endpoints web base
  • Dispatcher-servlet 无法映射到 websocket 请求

    我正在开发一个以Spring为主要框架的Java web应用程序 特别使用Spring core Spring mvc Spring security Spring data Spring websocket 像这样在 Spring 上下文
  • android httprequest java.net.UnknownHostException

    我想用android发出http请求 是使用这个 void testHTTP HttpClient httpClient new DefaultHttpClient HttpUriRequest request new HttpPost h
  • 如何在 Quartz 调度程序中每 25 秒运行一次?

    我正在使用 Java 的 Quartz Scheduling API 你能帮我使用 cron 表达式每 25 秒运行一次吗 这只是一个延迟 它不必总是从第 0 秒开始 例如 序列如下 0 00 0 25 0 50 1 15 1 40 2 0
  • 如何在Java中正确删除数组[重复]

    这个问题在这里已经有答案了 我刚接触 Java 4 天 从我搜索过的教程来看 讲师们花费了大量精力来解释如何分配二维数组 例如 如下所示 Foo fooArray new Foo 2 3 但我还没有找到任何解释如何删除它们的信息 从内存的情
  • Java的-XX:+UseMembar参数是什么

    我在各种地方 论坛等 看到这个参数 并且常见的答案是它有助于高并发服务器 尽管如此 我还是找不到 sun 的官方文档来解释它的作用 另外 它是Java 6中添加的还是Java 5中存在的 顺便说一句 许多热点虚拟机参数的好地方是这一页 ht
  • 直接使用从密钥库加载的 SecretKey 时,密钥用户未经过身份验证

    我正在尝试使用 Cipher 和在 KeyStore 中加载的 SecretKey 来加密数据 但总是收到此错误 导致 android security KeyStoreException 关键用户未经过身份验证 我尝试自己创建 Secre
  • 按字母顺序过滤 Firestore 数据以对 Google Cloud 中的文档读取进行分类/减少

    基于这样的事实Cloud Firestore 不支持全文搜索 https firebase google com docs firestore solutions search到目前为止 我决定问这个question https stack
  • Java &= 运算符应用 & 或 && 吗?

    Assuming boolean a false 我想知道是否这样做 a b 相当于 a a b logical AND a is false hence b is not evaluated 或者另一方面 这意味着 a a b Bitwi
  • JAXB - 列表<可序列化>?

    我使用 xjc 制作了一些课程 public class MyType XmlElementRefs XmlElementRef name MyInnerType type JAXBElement class required false
  • Android:如何检测手机设置中的语言已更改

    我如何检测我的手机语言是否已更改 例如 Facebook 应用程序将向我们宣布 please wait we preparing your language i used myString Locale getDefault getDisp
  • Android 和 Java 中绘制椭圆的区别

    在Java中由于某种原因Ellipse2D Double使用参数 height width x y 当我创建一个RectF在Android中参数是 left top right bottom 所以我对适应差异有点困惑 如果在 Java 中创
  • 当ScrollView滚动到底部时加载更多数据

    我有一个带有动态加载内容的滚动视图 有时可能会有很多内容 所以我想在用户滚动到底部时加载更多内容 我搜索了合适的方法 发现了两种 onScrollChanged and getScrollY 但我不知道如何将它用于我的目的 请给我一些建议

随机推荐