我已经创建了多个动态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();
}
}
}
}
}
我在演示项目中使用这种类型的动态添加Edittext
,如下所示:
UPDATE
我更改代码以使其与您的代码相似,以便我可以找到错误。您还会看到我的完整代码以及布局。它与您的代码相同,我可以输入任何EditText
,也可以阅读。如果仍然发现错误,请提供一些布局信息。
DEmo.java为:
public class DEmo extends AppCompatActivity {
int[] SCORE = {1, 4, 6, 5, 3};
String[] PLAYERS_LIST = {"aa", "bb", "cc", "dd", "ee"};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
Bundle bundle = new Bundle();
bundle.putIntArray("SCORE", SCORE);
bundle.putStringArray("PLAYERS_LIST", PLAYERS_LIST);
Fragment fragment = new fragment();
fragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.ly, fragment, "Fragment");
transaction.commitAllowingStateLoss();
}
}
demo.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/ly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
我的fragment.java如下:
public class fragment 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.fragment_view, 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.ly);
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();
}
}
}
}
}
fragment_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/ly"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="readtext"
android:text="done"/>
</LinearLayout>
希望对您有帮助