2016-10-20 1 views
0

J'ai créé un TableLayout dans lequel j'ajoute dynamiquement 50 lignes avec EditText. J'ai mis la limite de texte pour un seul texte d'édition tel que si la première rangée atteint la limite, le curseur de EditText se déplace vers la rangée suivante dynamiquement. Comment puis je faire ça? Une idée?Déplacer le texte vers le prochain Edittext dans TableRow

+0

Peut-être que vous avez besoin edittext.requestFocus() ;? –

+0

mais edittext est dynamique tout ce que le texte d'édition est cliqué et atteint pour limiter automatiquement aller à l'édition suivante – PriyankaChauhan

+0

Eh bien, vous devez toujours obtenir le prochain texte d'édition. Lors de la création dynamique des textes d'édition. donnez-leur des identifiants avec des nombres croissants afin que vous puissiez y accéder par la suite. –

Répondre

1

Ici, je trouve la solution

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>