Я использую этот тип динамического c добавления Edittext
в моем демонстрационном проекте, как показано ниже:
ОБНОВЛЕНИЕ
Я изменяю код, чтобы он соответствовал аналогично в ваш код, чтобы я мог найти ошибку. Вы видите мой полный код с макетом тоже. Он такой же, как ваш код, и я могу ввести любой 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>
мой фрагмент . 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();
}
}
}
}
}
фрагмент_обзора. 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>
Надеюсь, это поможет вам