我在“添加”按钮上创建了动态EditText框和微调框,但我不知道如何从中检索数据。 The whole code is available on this website.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
mText=(EditText) findViewById(R.id.number_edit_text);
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
// Add the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
parentLinearLayout.getChildAt(counter);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
答案 0 :(得分:1)
试试这个,您可以简单地创建循环并获取动态添加的所有数据,如下所示:
更改布局activity_main.xml,因为在演示中他们为布局做了错误的代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/parent_linear_layout"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"/>
<Spinner
android:id="@+id/type_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:entries="@array/types"
android:gravity="right" />
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#555"
android:layout_gravity="center"
android:onClick="onAddField"
android:textColor="#FFF"
android:text="Add Field"
android:paddingLeft="5dp"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/result"/>
</LinearLayout>
获取编辑文本的所有值的代码:
int count = parentLinearLayout.getChildCount();
for(int i=0; i<count; i++) {
LinearLayout linearLayout = (LinearLayout)parentLinearLayout.getChildAt(i);
EditText editText = (EditText)linearLayout.getChildAt(0);
String result = editText.getText().toString();
//You can result get data from edit text.
}
对于微调器值:
int count = parentLinearLayout.getChildCount();
for(int i=0; i<count; i++) {
LinearLayout linearLayout = (LinearLayout)parentLinearLayout.getChildAt(i);
Spinner spinnerField = (Spinner)linearLayout.getChildAt(1);
String result = spinnerField.getSelectedItem().toString();
//You can result get data from spinner value.
}
答案 1 :(得分:0)
mText=(EditText) view.findViewById(R.id.number_edit_text);
将它放在onAddField方法中。
如果您想从ediText获取文本,则需要调用
mText.getText().toString()