我有一個Button
,它增加了一行EditText
到TableLayout
。我需要找出每個添加行的EditText
中的輸入。如何從動態創建的EditTexts獲取輸入?
例如:有人增加了3行,這意味着有3 EditText
。在每個EditText
中,他們都將數字設置爲3.我需要存儲已添加到佈局的EditText
的值,並通過單擊按鈕將它們全部添加。
我會使用一個列表,然後迭代該列表?我不確定我會如何做到這一點。
這是到目前爲止我的代碼...
int count = 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSavedState();
Button buttonAdd = (Button) findViewById(R.id.button1);
Button buttonDel = (Button) findViewById(R.id.button2);
Button buttonCalc = (Button) findViewById(R.id.button3);
buttonAdd.setOnClickListener(this);
buttonDel.setOnClickListener(this);
}
public void onClick(View v) {
TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
List<EditText> allEd = new ArrayList<EditText>();
switch(v.getId()){
case R.id.button1:
if(count != 16){
count++;
// Create the row only when the add button is clicked
TableRow tempRow = new TableRow(MainActivity.this);
EditText tempText1 = new EditText(MainActivity.this);
EditText tempText2 = new EditText(MainActivity.this);
TextView tempTextView = new TextView(MainActivity.this);
EditText editText1 = (EditText) findViewById(R.id.editText1);
EditText editText2 = (EditText) findViewById(R.id.editText2);
TextView textView3 = (TextView) findViewById(R.id.textView3);
tempTextView.setText(count + ".");
tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tempText1.setLayoutParams(editText1.getLayoutParams());
tempText2.setLayoutParams(editText2.getLayoutParams());
tempTextView.setLayoutParams(textView3.getLayoutParams());
tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
tempText2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
tempText2.setId(count);
allEd.add(tempText2);
tempRow.addView(tempTextView);
tempRow.addView(tempText1);
tempRow.addView(tempText2);
tableLayout1.addView(tempRow);
}
else {
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Error");
alertDialog.setMessage("You can only have 10 rows!");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
break;
case R.id.button2:
if(count != 1){
count--;
tableLayout1.removeView(tableLayout1.getChildAt(count));
}
else {
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Error");
alertDialog.setMessage("You must have at least one row!");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
break;
case R.id.button3:
String[] strings = new String[allEd.size()];
for(int i = 0; i < allEd.size(); i++) {
strings[i] = allEd.get(i).getText().toString();
int input = Integer.parseInt(strings[i]);
final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
alertDialog.setTitle("Your calculated GPA");
alertDialog.setMessage("Your calculated GPA is: " + input/count);
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
break;
}
我爲onClick方法添加了一個「計算」按鈕,並通過將onList移出ArrayList,計算按鈕正確顯示了每個單獨的輸入。那麼我將如何去添加每個輸入? – user1460211
您的意思是,將這些值加在一起?你需要遍歷'ArrayList'的所有內容,獲取值並將它們加在一起。 – wattostudios
是的。我添加了我的button3 onClick到我發佈的問題。我想獲取每個引用的值,添加它,然後將其吐出給用戶。 – user1460211