从动态创建的editTexts获取值输入

时间:2019-08-06 09:15:43

标签: java android android-edittext

我用两个按钮(添加更多和继续)创建了一个包含4个字符串输入和double数据类型的LinearLayout。

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

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

动态字段

<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);
}

接收活动

    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);
}

我希望它将所有输入数据发送到下一个活动,但是它仅从前四(4)个输入字段发送了数据,该字段不是动态的,并且没有发送动态数据

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

更新- 尝试使用动态创建的EditText,而不是使用您自己的视图,

public void onAddField(View view) {
    //Create dynamic EditText
    EditText dynEditText = new EditText(this);
    //Add dyn edittext to layout
    parentLinearLayout.addView(dynEditText, parentLinearLayout.getChildCount() - 
    1);
    //Add the EditText to allEds
    allEds.add(dynEditText);
}

您似乎没有将动态创建的EditText添加到allEds

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);

    //Find the EditText in rowView
    EditText dynEditText = rowView.findViewById(R.id.dynEditText);
    //Add the EditText to allEds
    allEds.add(dynEditText);
}

您还在接收活动中使用键“文本”而不​​是“文本”。

//ArrayList <String> allTexts = (ArrayList<String>) collectDynamicData.getSerializableExtra("text");
//Should be
ArrayList <String> allTexts = (ArrayList<String>) collectDynamicData.getSerializableExtra("Text");


您的代码在我测试时似乎运行良好,您的问题可能出在其他地方。您可以检查的几件事是

  1. intent.putExtra("Text", (Serializable) allTexts);行放置一个断点,然后可以使用调试器检查EditText的值是否正确获取。
  2. 检查是否将动态创建的EditText添加到allEds
  3. 确保在第二个ArrayList中正确地从意图中获取了Activity
    Intent intent = getIntent();
    ArrayList<String> allTexts = (ArrayList<String>) intent.getSerializableExtra("Text");