如何在TableLayout中添加editText.addTextChangedListener

7

我的工作环境是Eclipse Heliose,Android 1.6

我创建了一个TableLayout,成功添加了列和行,每个单元格都有一个TextView和一个EditText。我在访问每个单元格时遇到了问题,并需要为每个单元格内的EditText添加addTextChangedListener。因此,我需要将英文文本更改为印地文。请建议一种解决方案。

/**
 * Here we are going to define the grid The Grid has 1. grid's value in the
 * particular position 2.
 */

public void setGrid() {

    String questionNumber[] = null;

    Typeface fontface = Typeface.createFromAsset(getAssets(), "fonts/padmaa.ttf");
    /*
     * Table layout for the crossword elements
     */
    TableLayout tableLayout = (TableLayout) findViewById(R.id.crosswordTableLayout);
    tableLayout.setGravity(Gravity.TOP);
    tableLayout.setBackgroundColor(Color.WHITE);
    tableLayout.setScrollContainer(true);

    for (int i = 0; i < sizeOfGrid; i++) {
        /*
         * This table row params is used to set the layout params alone we
         * are not going to use this anywhere
         */
        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tableRowParams.setMargins(1, 1, 1, 1);
        tableRowParams.height = 30;
        tableRowParams.gravity = Gravity.TOP;

        /*
         * Defining the row element for the crossword
         */
        TableRow tableRow = new TableRow(this);
        tableRow.setBackgroundColor(Color.WHITE);
        tableRow.setPadding(1, 0, 0, 0);
        tableRow.setGravity(Gravity.TOP);
        tableRow.setLayoutParams(tableRowParams);

        for (int j = 0; j < sizeOfGrid; j++) {

            /*
             * (1).Here we are defining headerTextView to set the clue
             * numbers. 
             * 2).columnEditText = a edit text view used to get the user
             * i/p data
             */
            TextView headerTextView = new TextView(this);
            columnEditText = new EditText(this);

            headerTextView.setGravity(Gravity.TOP);
            headerTextView.setTextSize(10);
            headerTextView.setEnabled(false);
            headerTextView.setHeight(30);
            headerTextView.setWidth(10);
            headerTextView.setTextColor(Color.BLACK);

            /* Override the edittext that has been created */
            columnEditText = (EditText) getLayoutInflater().inflate(R.layout.tablexml, null);
            columnEditText.setHeight(30);
            columnEditText.setWidth(25);
            /*
             * LinearLayout to arrange the two text view in a vertical
             * position
             */
            LinearLayout headerLinearLayout = new LinearLayout(tableRow.getContext());
            headerLinearLayout.setOrientation(LinearLayout.VERTICAL);
            headerLinearLayout.addView(headerTextView);

            /*
             * LinearLayout to arrange the first Linearlayout and Edit text
             * in a horizontal position
             */
            LinearLayout cellLinearLayout = new LinearLayout(tableRow.getContext());
            cellLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            cellLinearLayout.addView(headerLinearLayout);
            cellLinearLayout.addView(columnEditText);
            /*
             * Here we are setting the table's vertical border by some
             * workaround methods
             */
            cellLinearLayout.setLayoutParams(tableRowParams);
            /*
             * the column with complete black
             */
            if (cellValueArr[i][j] == null) {
                columnEditText.setBackgroundColor(Color.BLACK);
                headerTextView.setBackgroundColor(Color.BLACK);
                headerTextView.setGravity(Gravity.TOP);
                tableRow.setBackgroundColor(Color.BLACK);
                columnEditText.setEnabled(false);
            } else {
                /*
                 * column with values and question numbers
                 */
                tableRow.setBackgroundColor(Color.BLACK);
                if (quesNumArr[i][j] == null) {
                    columnEditText.setBackgroundColor(Color.WHITE);
                    headerTextView.setBackgroundColor(Color.WHITE);
                } else if (quesNumArr[i][j] != null) {
                    /*
                     * column without question number
                     */
                    questionNumber = quesNumArr[i][j].split("~");
                    quesArrList.add(questionNumber[1]);
                    headerTextView.setText(questionNumber[1]);
                    headerTextView.setTextColor(Color.BLACK);
                    headerTextView.setBackgroundColor(Color.WHITE);
                    columnEditText.setBackgroundColor(Color.WHITE);
                }

            }
            columnEditText.setId(i);
            headerTextView.setId(i);

            /* add the linear layout to the row values */
            tableRow.addView(cellLinearLayout);

            int childCount = tableRow.getChildCount();
            System.out.println("Child Count ::" + childCount);

            LinearLayout linearChild = (LinearLayout) tableRow.getChildAt(columnEditText.getId());
            colText = (EditText) linearChild.getChildAt(1);
            System.out.println("GET ID " + linearChild.getChildAt(1).getId());

            colText.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence seq, int start, int before, int count) {
                    // TODO Auto-generated method stub
                    if (seq.length() > 0 & before == 0) {
                        final String charSequence = String.valueOf(seq);
                        final String engChar = String.valueOf(seq.charAt(seq.length() - 1));
                        println("GUJ :: currentlatin ===> " + engChar + " :: charSequence ==>" + seq);
                        // Method For Type Char
                        List softList = conv.getSoftKeyPressForChar(engChar, getApplicationContext(), seq);
                        charSeq = (String) softList.get(0);
                        Typeface fontface1 = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/padmaa.ttf");
                        colText.setTypeface(fontface1, Typeface.NORMAL);
                        colText.setText(charSeq);
                        // for placing the cursor position
                        colText.clearFocus();
                        colText.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
                    System.out.println("AFTER TEXT CHANGE");
                }
            });

        }
        /* add the row values to the table layout */
        tableLayout.addView(tableRow);
    }
}

为什么要通过编程来完成这个任务?你可以为ListView创建一个定制的适配器,每个项目都包括1个EditText和TextView,这样在设计和开发方面都会更加容易。 - Paresh Mayani
谢谢回复,实际上我们正在创建一个填字游戏应用程序,所以我是这样做的。那个填字游戏的字体是古吉拉特语字符。因此需要更改用户输入的文本,这里涉及到一种音译。 - Saravana Muthu
这意味着你有一个行数为rows,列数为cols的屏幕,例如4*4。 - Paresh Mayani
是的,我已经创建了像行*列位置一样的单元格。在这里,我该如何获取每个单元格的值。第一个任务是我正在尝试在每个单元格中插入值,稍后我需要从每一行中获取这些值。每个单元格的值将使用古吉拉特语字体。 - Saravana Muthu
然后确切的点是,您可以通过为GridView创建一个自定义适配器来执行相同的任务(具有行*列),您可以在特定行和列中获取/设置EditText中的值。 - Paresh Mayani
最初我们只使用了网格视图,但是在网格布局中我们遇到了问题,当它向下滚动时,最后两行显示的是最初不可见的,会被交换到其他行,因此我尝试在表格布局中进行工作。 - Saravana Muthu
1个回答

0

我还没有全面查看代码。但是从我的初步了解来看,您可以通过以下任何一种方式来实现:

  1. 正如之前有人提到的,使用GridView。我无法从最后一个问题评论中获得太多信息,但如果这是一些滚动问题,您可以尝试将布局边距底部设置为5 dp,这样它就不会被裁剪(如果这是您的意思)。

  2. 继续使用表格布局,在循环中创建TextWatcher()并依次添加。如果您可以为每个元素设置setId或setTag,例如第一个editText获取名称“editText1”,然后是“editText2”等等,那么在循环中,您可以根据循环索引访问每个元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接