Android - 访问活动启动后添加的对象

时间:2013-04-12 10:55:14

标签: java android layout dynamic layout-inflater

我是Android开发的初学者,非常感谢您对此的帮助。

我有一个Android活动,当用户点击按钮时会创建一个Checkbox和一个EditText。用户可以随意添加他喜欢的Checkboxes / EditTexts。

这是要添加到活动的视图的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10" >

            <requestFocus />
        </EditText>

    </LinearLayout>

</LinearLayout>

这是java代码:

public void onclickplus (View view){

        LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View view1 = inflate.inflate(R.layout.row_checkitem, null);
        mainLayout.addView(view1);
        count = count +1;

}

我的问题是,我们如何在实际的java代码中访问这些复选框和EditTexts,以便我可以将数据保存到我的数据库中。

如果这些对象在预定的地方我会做,例如:

EditText text1;
text1 = (EditText) findViewById(R.id.editText1);
text1.getText().toString();

等等,访问editText的值。

但是,在这种情况下,启动活动时这些对象不存在。

你能救我吗? 感谢。

2 个答案:

答案 0 :(得分:0)

您可以查看此解决方案:

Add EditText(s) dynamically and retrieve values – Android

他们所做的是动态添加多个EditText字段并将其保存在列表中以便以后访问它们。你只需要重写它也可以添加复选框。

答案 1 :(得分:0)

由于D.Wonnik的帮助,我设法实现了我想做的事。

我放弃了LayoutInflater方法,而是使用了TableLayout和TableRows。

这样,用户可以通过单击“+”按钮添加包含复选框和EditText的TableRows,并且还可以通过单击“ - ”按钮将其删除。我也可以通过这种方式检索它们的值。

以下是代码:

LinearLayout mainLayout;
    TableLayout tablelayout;
    TableRow[] tableRow;
    int count;
    EditText[] ed;
    CheckBox[] cb;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mainLayout = (LinearLayout)findViewById(R.id.LinearLayout1);
        tablelayout = new TableLayout(this);
        mainLayout.addView(tablelayout);
        ed = new EditText[10];
        cb = new CheckBox[10];
        tableRow = new TableRow[10];

    }

public void onclickplus (View view){


        ed[count] = new EditText(this);
        ed[count].setId(count);
        cb[count] = new CheckBox(this);
        cb[count].setId(count);
        tableRow[count] = new TableRow(this);

        tablelayout.addView(tableRow[count]);

        tableRow[count].addView(cb[count]);
        tableRow[count].addView(ed[count]);

        count = count +1;

}

public void onclickminus (View view){


        if (count>0){

            count = count -1;
            tablelayout.removeView(tableRow[count]);
    }

    }

感谢D.Wonnik指出我正确的方向!