【发布时间】:2020-04-29 18:12:26
【问题描述】:
我创建了多个动态 EditText 对象(取决于从前一个片段收到的计数)作为 TableRow 的一部分。我想将一个侦听器(或多个侦听器,以可行的为准)附加到每个 EditText 并将用户添加的数据读入 String 数组。但是,当我运行程序时,我只能在最顶层 TableRow 的一个 EditText 中输入文本并阅读该文本。我无法在其他 EditText 中输入任何文本或阅读其内容。我究竟做错了什么?当我尝试创建一个 customTextWatcher 类、尝试简单的 editText.getText().toString()、尝试使用 ArrayList(而不是 String 数组)并通过它重复到 String 数组时,知道这会很有帮助。但是,这个错误继续困扰我!有人可以帮忙吗?将多个 EditText 读入 String 数组的最佳方法是什么?
public class ForStackOF extends Fragment {
private String[] globalplNames, localplNames, individualRoundScores;
private int[] globalplScores, localplScores;
private Bundle USBundle;
private TableLayout tempTable;
private TextWatcher editWatcher;
private int globalCount, localCount, counter;
private TextView tempNameView, tempScoreView, firstUpdateView;
//private EditText tempNewScoreEdit;
//List<EditText> allEds = new ArrayList<EditText>();
private EditText[] ed;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
setRetainInstance(true);
USBundle = this.getArguments();
globalCount = USBundle.getIntArray(SCORE).length;
localCount = globalCount + 1;
globalplNames = new String[globalCount];
globalplNames = USBundle.getStringArray(PLAYERS_LIST);
localplNames = new String[localCount];
globalplScores = new int[globalCount];
globalplScores = USBundle.getIntArray(SCORE);
localplScores = new int[localCount];
individualRoundScores = new String[localCount];
ed = new EditText[localCount];
for (counter = 0, localCount = 0; counter < globalCount; counter++) {
localplNames[localCount + 1] = globalplNames[counter];
localplScores[localCount + 1] = globalplScores[counter];
localCount++;
}
localCount = globalCount + 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.update_score_fragment, parent, false);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams namecellParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.5f);
TableRow.LayoutParams liveScorecellParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.25f);
TableRow.LayoutParams newScorecellParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.25f);
tempTable = (TableLayout) v.findViewById(R.id.scoreTables);
for (counter = 0; counter < localCount; counter++) {
TableRow row = new TableRow(getContext());
row.setLayoutParams(rowParams);
row.setWeightSum(1f);
tempNameView = new TextView(getContext());
tempNameView.setTextSize(16);
tempNameView.setLayoutParams(namecellParams);
tempScoreView = new TextView(getContext());
tempScoreView.setTextSize(16);
tempScoreView.setLayoutParams(liveScorecellParams);
tempScoreView.setGravity(android.view.Gravity.CENTER);
ed[counter] = new EditText(getContext());
ed[counter].setTextSize(16);
ed[counter].setLayoutParams(newScorecellParams);
ed[counter].setInputType(InputType.TYPE_CLASS_NUMBER);
ed[counter].setHint("+/-");
ed[counter].setFocusableInTouchMode(true);
ed[counter].setGravity(android.view.Gravity.CENTER);
//allEds.add(tempNewScoreEdit);
//tempNewScoreEdit.setId(counter);
firstUpdateView = new TextView(getContext());
firstUpdateView.setLayoutParams(newScorecellParams);
if (counter == 0) {
tempNameView.setText("Player");
tempScoreView.setText("Existing");
//firstUpdateView.setGravity(Gravity.CENTER);
firstUpdateView.setText("This Round");
row.addView(tempNameView);
row.addView(tempScoreView);
row.addView(firstUpdateView);
tempTable.addView(row, counter);
} else {
tempNameView.setText(localplNames[counter]);
tempScoreView.setText(String.valueOf(localplScores[counter]));
ed[counter].addTextChangedListener(new CustomTextWatcher(ed[counter], counter));
row.addView(tempNameView);
row.addView(tempScoreView);
row.addView(ed[counter]);
tempTable.addView(row, counter);
}
}
return v;
}
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
private int editi;
public CustomTextWatcher(EditText e, int i) {
mEditText = e;
editi = i;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//mEditText.setFocusable(true);
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
for (int k = 0; k < ed.length; k++) {
if (s.hashCode() == ed[k].getText().hashCode()) {
individualRoundScores[editi] = ed[k].getText().toString();
}
}
}
}
}
【问题讨论】:
标签: java android android-edittext