gpt4 book ai didi

java - 从android中动态创建的EditBox中检索数据

转载 作者:行者123 更新时间:2023-12-02 12:46:02 25 4
gpt4 key购买 nike

我在“添加”按钮上创建了动态 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());
}

最佳答案

试试这个,您可以简单地创建循环并获取动态添加的所有数据,如下所示:

更改您的布局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.
}

关于java - 从android中动态创建的EditBox中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44777444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com