gpt4 book ai didi

java - 从动态创建的 editText 获取值输入

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

我创建了一个带有 4 个字符串输入和 double 据类型的 LinearLayout,带有两个按钮(“添加更多”和“继续”)。

当我单击“添加更多”按钮时,根据单击的次数,在第一个输入字段下方添加另一组输入字段(我已经完成了此操作)

当我点击“继续”按钮时,* 我想根据下面添加 View 的次数获取动态字段上提供的值。我想与前 4 个字段一起进行计算,然后将这些值传递到下一个 Activity 以进行进一步处理

动态字段

<EditText
android:id="@+id/product_name_main"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:hint="Product Name"
android:inputType="text"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textSize="14sp" />

<EditText
android:id="@+id/product_number_main"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"

android:ems="10"
android:hint="Product Number"
android:inputType="number"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textSize="14sp" />

<EditText
android:id="@+id/product_price_main"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"

android:ems="10"
android:hint="Product Price"
android:inputType="number"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textSize="14sp" />

<EditText
android:id="@+id/product_quantity_main"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"

android:ems="10"
android:hint="Product Quantity"
android:inputType="number"
android:paddingStart="20dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingEnd="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textSize="14sp" />

继续按钮

public void btnProceed(View view) {

Intent intent = new Intent(view.getContext(), DisplayActivity.class);


// ******** making sure the inputs gets values before proceeding to
the next phase
if(productName.getText().length() == 0) {
Toast.makeText(this, "You need to fill out the fields to
continue", Toast.LENGTH_LONG).show();
} else if(productNumber.getText().length() ==0) {
Toast.makeText(this, "fill out the second field to continue",
Toast.LENGTH_LONG).show();
}else if (productPrice.getText().length() == 0) {
Toast.makeText(this, "Please fill out the third field",
Toast.LENGTH_LONG).show();
}else if (productQuantity.getText().length() == 0) {
Toast.makeText(this, "Please fill out the last field",
Toast.LENGTH_LONG).show();
}else{
// getting text from the user using the standard fields
pName = productName.getText().toString();
pNumber = Double.parseDouble(productNumber.getText().toString());
pPrice = Double.parseDouble(productPrice.getText().toString());
pQuantity =
Double.parseDouble(productQuantity.getText().toString());

//for the dynamically added textViews
SharedPreferences.Editor editor =
getPreferences(Context.MODE_PRIVATE).edit();
for (EditText editText : allEds) {
editor.putString("key" + editText.getTag().toString(),
editText.getText().toString());
}
editor.commit();

ArrayList<String> allTexts = new ArrayList<>();
for (EditText e : allEds) {
allTexts.add(e.getText().toString());

}

//sending the standard recieved texts to the next activity
intent.putExtra("name", pName);
intent.putExtra("number", pNumber);
intent.putExtra("price", pPrice);
intent.putExtra("quantity", pQuantity);

//sending the dynamically received values
intent.putExtra("Text", (Serializable) allTexts);
startActivity(intent);
finish();

}
}

*已编辑*

添加更多按钮

public void onAddField(View view) {
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
//adding the new row before the add field button.
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() -
1);
}

接收 Activity

    productName = findViewById(R.id.product_name_main);
productNumber = findViewById(R.id.product_number_main);
productPrice = findViewById(R.id.product_price_main);
productQuality = findViewById(R.id.product_quantity_main);

//GETTING THE DYNAMIC DATA
others = findViewById(R.id.other_items);
Intent collectDynamicData = getIntent();
ArrayList <String> allTexts = (ArrayList<String>)
collectDynamicData.getSerializableExtra("text");
others.setText((CharSequence) allTexts);


//getting the first items from the first activity
getName = getIntent().getExtras().getString("name");
getNumber = getIntent().getExtras().getDouble("number");
getPrice = getIntent().getExtras().getDouble("price");
getQuantity = getIntent().getExtras().getDouble("quantity");

//formatting the numbers
DecimalFormat formate = new DecimalFormat();
String resultNumber, resultPrice, resultQuantity;
resultNumber = formate.format(getNumber);
resultPrice = formate.format(getPrice);
resultQuantity = formate.format(getQuantity);


//SETTING THE OUTPUT
productName.setText(getName);
productNumber.setText(resultNumber);
productPrice.setText(resultPrice);
productQuality.setText(resultQuantity);
}

我预计会将所有输入数据发送到下一个 Activity ,但它只发送前四 (4) 个输入字段中的数据,该字段不是动态的,并且没有发送动态数据

最佳答案

发生这种情况是因为您有多个具有相似 id 的 View ,因此系统仅查找具有指定 id 的第一个 View 。您需要将布局添加到数组中,并为每个动态布局分配唯一的 ID(例如:View.generateViewId())。之后,您需要迭代布局数组并在每个布局内执行 findViewById() (例如: layouts [i] .findViewById (R.id.edit_text) )。

关于java - 从动态创建的 editText 获取值输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57372744/

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