2011-10-08 14 views

Odpowiedz

22

Można tworzyć TextView jak to:

int textViewCount = 10; 

TextView[] textViewArray = new TextView[textViewCount]; 

for(int i = 0; i < textViewCount; i++) { 
    textViewArray[i] = new TextView(this); 
} 
2

może być jej przydatne dla Ciebie i użyć przycisku tablicę więc jestem Gussing pracę TextView tak:

TextView[ ][ ] _txt; 

_txt = new TextView[_dimension][_dimension]; // _dimension = 5 what you want 
_txt[0][0] = (TextView) findViewById(R.id.text1); 
_txt[0][1] = (TextView) findViewById(R.id.text2); 

i więcej ...

+0

dlaczego u wziąć 2d tablicy w to? – ekjyot

+0

To było moje wymaganie, więc możesz użyć pojedynczej tablicy zamiast 2d –

2

Jeśli chcesz dużą liczbę TextView, w tym przypadku, aby uniknąć OutofBound stosowanie wyjątku następujący kod

LinearLayout parent = new LinearLayout(this); 
     TextView textView; 
     for(i = 0; i < count; i++) { 
      textView = new TextView(this); 
      textView.setTag(""+i);// setting tag with index i 
      parent.addView(textView); 
     } 
     int len=parent.getChildCount(); 
     int j = 0; 
     int requiredPosition = 5; 
     while(j<len) { 
      TextView tempTextView =((TextView)parent.getChildAt(i)); 
      if(tempTextView.getTag().equals(""+requiredPosition)){ 
       //Perform required operation 
       //tempTextView.setText(""); 
      } 
      j++; 
     } 
1

można nie osiągnąć coś takiego:

int textvwCount = 20;//number of textview you want to use in an array 

TextView[] arrTxtView = new TextView[textvwCount ]; // declare and assign array length of text views. 

for(int i = 0; i < textViewCount; i++) { // iterate over all array items and assign them text. 
    Textview txtCnt = new TextView(this); 
txtCnt .settext(i); 
textViewArray[i] =txtCnt ; 
} 
Powiązane problemy