java—从edittext向arraylist添加值

s4n0splo  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(268)

我是新来编程,所以请原谅,如果这是愚蠢的问题。我有两个钮扣。首先将edittext添加到活动中,然后将输入的值保存到数组中。问题是它只保存第一个edittext的值。因此,如果我想对数组中的所有值求和,我将得到第一个值乘以添加的edittext的数量。先谢谢你。

ArrayList<EditText> editText2ArrayList = new ArrayList<EditText>();
int j = 0;

添加编辑文本的按钮

public void addButtonMethod(View view){
        ImageButton addButton = findViewById(R.id.addButton);
        ImageButton confirmButton = findViewById(R.id.confirmButton);
        LinearLayout itemList = findViewById(R.id.AddLayout);
        View item = LayoutInflater.from(this)
                .inflate(R.layout.add_textview, itemList, false);
        itemList.addView(item);
        confirmButton.setVisibility(View.VISIBLE);
        addButton.setBackgroundResource(R.drawable.gray_round_button);
        addButton.setClickable(false);
        EditText addEditText2 = findViewById(R.id.add_edit_text_2);   // this is EditText from R.layout.add_textview
        editText2ArrayList.add(j, addEditText2);
    }

确认输入文本的按钮

public void confirmButtonMethod(View view){
        ImageButton addButton = findViewById(R.id.addButton);
        ImageButton confirmButton = findViewById(R.id.confirmButton);
        EditText currentAddEditText2 = editText2ArrayList.get(j);
        j++;
        if(currentAddEditText2.getText().toString().trim().length() > 0){
            String priceString = currentAddEditText2.getText().toString();
            Float price = Float.parseFloat(priceString);
            productPrices.add(price);
            confirmButton.setVisibility(View.INVISIBLE);
            addButton.setBackgroundResource(R.drawable.round_button);
            addButton.setClickable(true);
        }
        else{
            Toast.makeText(this, "Please enter the price", Toast.LENGTH_SHORT).show();
        }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题