动态复选框创建 [英] dynamic checkbox creation

查看:189
本文介绍了动态复选框创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid应用程序运行时动态地创建一组复选框。当应用程序运行时没有显示除了按钮。什么我忘记了?在此先感谢!

 公共类DataNotificationSurvey延伸活动{
    私人data时间戳;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.datanotifylayout);
        最终按钮notifySubmitButton =(按钮)findViewById(R.id.notifySubmitButton);
        TableLayout T1 =(TableLayout)findViewById(R.id.notificationTableLayout);

        的for(int i = 0;我小于5;我++){
            的TableRow TR =新的TableRow(本);
            复选框CHK =新的复选框(本);
            chk.setText(Integer.toString(ⅰ));
            tr.addView(CHK);
            t1.addView(TR);
        }

        notifySubmitButton.setOnClickListener(
                新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        完();
                    }
                });
}
 


  //我的XML布局,将在以后以一个贴在下面,看看它的工作原理改变这一点。
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    机器人:ID =@ + ID / notificationLinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT>
  < TableLayout
    机器人:ID =@ + ID / notificationTableLayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
        <的TableRow
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT>

            <按钮的android:文本=静态按钮/>
        < /的TableRow>
  < / TableLayout>
  <按钮
  机器人:layout_height =WRAP_CONTENT
  机器人:layout_width =WRAP_CONTENT
  机器人:ID =@ + ID / notifySubmitButton
  机器人:文本=提交>< /按钮>
< / LinearLayout中>
 

解决方案

这对我来说工作得很好。

 公共类DynamicTableRowWithCheckBox扩展活动
{
  @覆盖
  公共无效的onCreate(包savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    最终按钮notifySubmitButton =(按钮)findViewById(R.id.myButton);
    TableLayout表=(TableLayout)findViewById(R.id.myTable);

    的for(int i = 0;我3;;我++)
    {
      的TableRow行=新的TableRow(本);
      复选框CHK =新的复选框(本);
      chk.setText(Integer.toString(ⅰ));
      row.addView(CHK);
      table.addView(行);
    }

    notifySubmitButton.setOnClickListener(
        新View.OnClickListener()
        {
          @覆盖
          公共无效的onClick(视图v)
          {
            完();
          }
        });
  }
}  

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/你好/>
    <按钮
        机器人:ID =@ + ID / myButton的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=关闭/>
    < TableLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =FILL_PARENT
        机器人:ID =@ + ID / myTable的/>
< / 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>

解决方案

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>

这篇关于动态复选框创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆