【发布时间】:2011-10-08 04:27:45
【问题描述】:
我正在制作一个应用程序,我想在其中从字符串数组更改 textviews 的文本。 为此,我需要制作 textviews 数组。如何做到这一点?谁能帮我解决这个问题
【问题讨论】:
我正在制作一个应用程序,我想在其中从字符串数组更改 textviews 的文本。 为此,我需要制作 textviews 数组。如何做到这一点?谁能帮我解决这个问题
【问题讨论】:
您可以像这样创建 TextView:
int textViewCount = 10;
TextView[] textViewArray = new TextView[textViewCount];
for(int i = 0; i < textViewCount; i++) {
textViewArray[i] = new TextView(this);
}
【讨论】:
如果你想要大量的文本视图,在这种情况下避免 OutofBound 异常使用以下代码
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++;
}
【讨论】:
可能对你有用,我使用按钮数组,所以我在猜测 textview 的工作方式:
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);
还有更多...
【讨论】:
您可以通过以下方式实现:
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 ;
}
【讨论】: