我是一名菜鸟程序员,我正在尝试设置一个函数来将 TextView
或 TextViews
的 array
传递给一个函数,因此可以在 activity
中的各个点调用它。
public class ScoreboardActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scoreboard);
...
final TextView STATVIEW1A = (TextView) findViewById(R.id.place_stat1a); //Team 1
final TextView STATVIEW1B = (TextView) findViewById(R.id.place_stat1b);
final TextView STATVIEW1C = (TextView) findViewById(R.id.place_stat1c);
final TextView STATVIEW1D = (TextView) findViewById(R.id.place_stat1d);
final TextView STATVIEW2A = (TextView) findViewById(R.id.place_stat2a); //Team 2
final TextView STATVIEW2B = (TextView) findViewById(R.id.place_stat2b);
final TextView STATVIEW2C = (TextView) findViewById(R.id.place_stat2c);
final TextView STATVIEW2D = (TextView) findViewById(R.id.place_stat2d);
//final TextView[] STATVIEW = {STATVIEW1A,STATVIEW1B,STATVIEW1C,STATVIEW1D,
// STATVIEW2A,STATVIEW2B,STATVIEW2C,STATVIEW2D};
...
postStats();
... or
postStats(STATVIEW[]);
我想要一个例程来在我的 Activity_scoreobard 布局上发布 (8) TextView。我尝试在函数中引用 STATVIEW1A:
public void postStats () {
STATVIEW1AX.setText("#dumps: " + Integer.toString(DUMP1[GAME_NO]));
}
我还尝试通过在函数中传递 TextView 数组来引用每个 TextView:
public void postStats (TextView[] VIEWSTAT) {
VIEWSTAT[6].setText("#dumps: "+ Integer.toString(DUMP2[GAME_NO]));
}
虽然这两种情况在 Eclipse 中都不会显示错误,但程序并不喜欢这两种情况。
最佳答案
通常将它们传递给函数并不是一个好主意...
但是如果你愿意,你可以尝试这样的事情......
TextView[] STATVIEW = new TextView[SIZE];
然后初始化
STATVIEW[0] = (TextView) findViewById(R.id.place_stat1a); //Team 1
STATVIEW[1] = (TextView) findViewById(R.id.place_stat1b);
......
然后传递你的数组
postStats(STATVIEW);
替代方案是
创建一个方法
并传递值并将它们设置为TextViews
类似这样的事情
public void fillData(String[] data)
{
for (int i=0;i<STATVIEW.length;i++)
{
STATVIEW[i].setText(data[i]);
}
}
关于java - 将 TextView 或 TextView 数组传递给 Android 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17608091/