将文本移动到TableRow中的下一个Edittext

时间:2016-10-20 11:12:48

标签: android android-tablelayout

我创建了一个TableLayout,其中我使用EditText动态添加50行。我为单个编辑文本设置了文本限制,这样如果第1行达到限制,EditText的光标会动态移动到下一行。我怎样才能做到这一点?任何的想法?

1 个答案:

答案 0 :(得分:1)

我在这里找到了解决方案

for (int i = 0; i < 50; i++) {
        TableRow newRow = new TableRow(getApplicationContext());
        newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

        EditText dueAmountText = new EditText(getApplicationContext());
        dueAmountText.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 5.0f));
        dueAmountText.setTextColor(Color.BLACK);
        dueAmountText.setTextSize(26f);
        dueAmountText.setMaxLines(1);
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(TextLength);
        dueAmountText.setFilters(FilterArray);


        Drawable drawable = dueAmountText.getBackground(); // get current EditText drawable
        drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);

        dueAmountText.setBackground(drawable); // set the new drawable to EditText

        allwtEditTextList.add(dueAmountText);
        newRow.addView(dueAmountText);
        tableLayout.addView(newRow);
    }
    for (int z = 0; z < allwtEditTextList.size(); z++) {
        final int pos = z;
        allwtEditTextList.get(z).addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                // TODO Auto-generated method stub

                if (s.length() >= TextLength) {
                    Log.e("TextCount", pos + "--" + allwtEditTextList.size());

                    if (pos < (allwtEditTextList.size() - 1)) {
                        EditText editText = allwtEditTextList.get(pos + 1);
                        editText.requestFocus();
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                // Need getTag() here
                allwtEditTextList.get(pos).getTag();
            }
        });
    }



<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp">


        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        </TableRow>
    </TableLayout>
</ScrollView>