android – 如何在TableLayout中添加editText.addTextChangedListener

前端之家收集整理的这篇文章主要介绍了android – 如何在TableLayout中添加editText.addTextChangedListener前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的工作环境是 Eclipse heliose,Android 1.6

我已经创建了一个tableLayout,其中列和行已成功添加,每个单元格都有一个textView和一个EditText,我在访问每个单元格时遇到问题,需要在单元格内的每个editText中添加addTextChangedListener.所以我需要将英文文本更改为指示文本.请提出解决方

  1. /**
  2. * Here we are going to define the grid The Grid has 1. grid's value in the
  3. * particular position 2.
  4. */
  5.  
  6. public void setGrid() {
  7.  
  8. String questionNumber[] = null;
  9.  
  10. Typeface fontface = Typeface.createFromAsset(getAssets(),"fonts/padmaa.ttf");
  11. /*
  12. * Table layout for the crossword elements
  13. */
  14. TableLayout tableLayout = (TableLayout) findViewById(R.id.crosswordTableLayout);
  15. tableLayout.setGravity(Gravity.TOP);
  16. tableLayout.setBackgroundColor(Color.WHITE);
  17. tableLayout.setScrollContainer(true);
  18.  
  19. for (int i = 0; i < sizeOfGrid; i++) {
  20. /*
  21. * This table row params is used to set the layout params alone we
  22. * are not going to use this anywhere
  23. */
  24. TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  25. tableRowParams.setMargins(1,1,1);
  26. tableRowParams.height = 30;
  27. tableRowParams.gravity = Gravity.TOP;
  28.  
  29. /*
  30. * Defining the row element for the crossword
  31. */
  32. TableRow tableRow = new TableRow(this);
  33. tableRow.setBackgroundColor(Color.WHITE);
  34. tableRow.setPadding(1,0);
  35. tableRow.setGravity(Gravity.TOP);
  36. tableRow.setLayoutParams(tableRowParams);
  37.  
  38. for (int j = 0; j < sizeOfGrid; j++) {
  39.  
  40. /*
  41. * (1).Here we are defining headerTextView to set the clue
  42. * numbers.
  43. * 2).columnEditText = a edit text view used to get the user
  44. * i/p data
  45. */
  46. TextView headerTextView = new TextView(this);
  47. columnEditText = new EditText(this);
  48.  
  49. headerTextView.setGravity(Gravity.TOP);
  50. headerTextView.setTextSize(10);
  51. headerTextView.setEnabled(false);
  52. headerTextView.setHeight(30);
  53. headerTextView.setWidth(10);
  54. headerTextView.setTextColor(Color.BLACK);
  55.  
  56. /* Override the edittext that has been created */
  57. columnEditText = (EditText) getLayoutInflater().inflate(R.layout.tablexml,null);
  58. columnEditText.setHeight(30);
  59. columnEditText.setWidth(25);
  60. /*
  61. * LinearLayout to arrange the two text view in a vertical
  62. * position
  63. */
  64. LinearLayout headerLinearLayout = new LinearLayout(tableRow.getContext());
  65. headerLinearLayout.setOrientation(LinearLayout.VERTICAL);
  66. headerLinearLayout.addView(headerTextView);
  67.  
  68. /*
  69. * LinearLayout to arrange the first Linearlayout and Edit text
  70. * in a horizontal position
  71. */
  72. LinearLayout cellLinearLayout = new LinearLayout(tableRow.getContext());
  73. cellLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
  74. cellLinearLayout.addView(headerLinearLayout);
  75. cellLinearLayout.addView(columnEditText);
  76. /*
  77. * Here we are setting the table's vertical border by some
  78. * workaround methods
  79. */
  80. cellLinearLayout.setLayoutParams(tableRowParams);
  81. /*
  82. * the column with complete black
  83. */
  84. if (cellValueArr[i][j] == null) {
  85. columnEditText.setBackgroundColor(Color.BLACK);
  86. headerTextView.setBackgroundColor(Color.BLACK);
  87. headerTextView.setGravity(Gravity.TOP);
  88. tableRow.setBackgroundColor(Color.BLACK);
  89. columnEditText.setEnabled(false);
  90. } else {
  91. /*
  92. * column with values and question numbers
  93. */
  94. tableRow.setBackgroundColor(Color.BLACK);
  95. if (quesNumArr[i][j] == null) {
  96. columnEditText.setBackgroundColor(Color.WHITE);
  97. headerTextView.setBackgroundColor(Color.WHITE);
  98. } else if (quesNumArr[i][j] != null) {
  99. /*
  100. * column without question number
  101. */
  102. questionNumber = quesNumArr[i][j].split("~");
  103. quesArrList.add(questionNumber[1]);
  104. headerTextView.setText(questionNumber[1]);
  105. headerTextView.setTextColor(Color.BLACK);
  106. headerTextView.setBackgroundColor(Color.WHITE);
  107. columnEditText.setBackgroundColor(Color.WHITE);
  108. }
  109.  
  110. }
  111. columnEditText.setId(i);
  112. headerTextView.setId(i);
  113.  
  114. /* add the linear layout to the row values */
  115. tableRow.addView(cellLinearLayout);
  116.  
  117. int childCount = tableRow.getChildCount();
  118. System.out.println("Child Count ::" + childCount);
  119.  
  120. LinearLayout linearChild = (LinearLayout) tableRow.getChildAt(columnEditText.getId());
  121. colText = (EditText) linearChild.getChildAt(1);
  122. System.out.println("GET ID " + linearChild.getChildAt(1).getId());
  123.  
  124. colText.addTextChangedListener(new TextWatcher() {
  125. @Override
  126. public void onTextChanged(CharSequence seq,int start,int before,int count) {
  127. // TODO Auto-generated method stub
  128. if (seq.length() > 0 & before == 0) {
  129. final String charSequence = String.valueOf(seq);
  130. final String engChar = String.valueOf(seq.charAt(seq.length() - 1));
  131. println("GUJ :: currentlatin ===> " + engChar + " :: charSequence ==>" + seq);
  132. // Method For Type Char
  133. List softList = conv.getSoftKeyPressForChar(engChar,getApplicationContext(),seq);
  134. charSeq = (String) softList.get(0);
  135. Typeface fontface1 = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/padmaa.ttf");
  136. colText.setTypeface(fontface1,Typeface.NORMAL);
  137. colText.setText(charSeq);
  138. // for placing the cursor position
  139. colText.clearFocus();
  140. colText.requestFocus();
  141.  
  142. }
  143. }
  144.  
  145. @Override
  146. public void beforeTextChanged(CharSequence s,int count,int after) {
  147. // TODO Auto-generated method stub
  148.  
  149. }
  150.  
  151. @Override
  152. public void afterTextChanged(Editable s) {
  153. // TODO Auto-generated method stub
  154. System.out.println("AFTER TEXT CHANGE");
  155. }
  156. });
  157.  
  158. }
  159. /* add the row values to the table layout */
  160. tableLayout.addView(tableRow);
  161. }
  162. }

解决方法

我没有整体查看代码.但从我最初的理解,你可以用这些方式做到,

>正如之前提到的那样,请进行网格视图.从最后一个评论到问题,我无法解决这个问题,但如果它有一些滚动问题,你可以尝试将一个布局边距底部调整到5 dp,这样它就不会被裁剪(如果是的话)你的意思是)>继续使用表格布局,创建一次TextWatcher()并在循环中按顺序添加它.如果你可以为每个元素设置setId或setTag,可能会很好,首先是editText获取名称,“editText1”,然后是“editText2”,依此类推.因此,在循环中,您可以基于循环索引访问每个元素.

猜你在找的Android相关文章