在 Android 中的 For 循环外使用数组 int 值
Using an array int value outside For loop in Android
我试图在生成它的 For 循环之外使用 tvs[i]
变量,在另一种方法 OnValueChange
中,为 NumberPickers
。
但是,我只为第一个 tvs[i]
NumberPicker
生成一个 TextView
值,无论我旋转哪个 NumberPicker
。
谁能解释一下我在下面的代码中哪里出错了?我已经尝试将 For 循环扩展到 OnChangeListener
方法下方,但是我遇到了 tvs[i]
没有 final
值等问题。
link 与 tvs[i].setText
的初始 tvs[i]
值是什么??
public void setupUI()
{
int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2, R.id.tvId3, R.id.tvId7,
R.id.tvId8, R.id.tvId9, R.id.tvId10, R.id.tvId11, R.id.tvId12, R.id.tvId13, R.id.tvId14, R.id.tvId15, R.id.tvId16, R.id.tvId17,
R.id.tvId18, R.id.tvId19, R.id.tvId20, R.id.tvId21, R.id.tvId22, R.id.tvId23, R.id.tvId24, R.id.tvId25, R.id.tvId26, R.id.tvId27, R.id.tvId28, R.id.tvId29, R.id.tvId30, R.id.tvId31,
R.id.tvId32, R.id.tvId33, R.id.tvId34, R.id.tvId35, R.id.tvId36, R.id.tvId37, R.id.tvId38, R.id.tvId39, R.id.tvId40, R.id.tvId41, R.id.tvId42, R.id.tvId43, R.id.tvId44, R.id.tvId45, R.id.tvId46, R.id.tvId47, R.id.tvId48, R.id.tvId49, R.id.tvId50, R.id.tvId51, R.id.tvId52, R.id.tvId53};
tvs = new TextView[53];
for(int i=0; i < textViewIDs.length; i
++) {
tv = (TextView ) findViewById(textViewIDs[i]);
tvs[i] = tv;
}
//tv = (TextView) findViewById(R.id.tvId1);
int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2, R.id.npId3, R.id.npId7,
R.id.npId8, R.id.npId9, R.id.npId10, R.id.npId11, R.id.npId12, R.id.npId13, R.id.npId14, R.id.npId15, R.id.npId16, R.id.npId17,
R.id.npId18, R.id.npId19, R.id.npId20, R.id.npId21, R.id.npId22, R.id.npId23, R.id.npId24, R.id.npId25, R.id.npId26, R.id.npId27, R.id.npId28, R.id.npId29, R.id.npId30, R.id.npId31,
R.id.npId32, R.id.npId33, R.id.npId34, R.id.npId35, R.id.npId36, R.id.npId37, R.id.npId38, R.id.npId39, R.id.npId40, R.id.npId41, R.id.npId42, R.id.npId43, R.id.npId44, R.id.npId45, R.id.npId46, R.id.npId47, R.id.npId48, R.id.npId49, R.id.npId50, R.id.npId51, R.id.npId52, R.id.npId53};
for(int j=0; j < numpickIDs.length; j++) {
NumberPicker np = (NumberPicker ) findViewById(numpickIDs[j]);
//NumberPicker np = (NumberPicker) findViewById(R.id.npId1);
np.setOnValueChangedListener(new OnValueChangeListener()
{
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
tvs[i].setText(String.valueOf(newVal));
}
});
np.setMaxValue(12);
np.setMinValue(0);
}
}
在调用 setOnValueChangedListener 之前,添加这一行
final TextView curTextView = tvs[j];
然后将 onValueChanged 更改为
curTextView.setText(String.valueOf(newVal));
如果您想要更复杂的东西,您可以随时使用 View
class 的 "tag" 功能。
基本上,您可以将每个 TextView
设置为关联 NumberPicker
的 "tag",从而使 onValueChange
方法可以轻松访问并设置相关 TextView
.
的文本
在我的代码中,我也做到了,因此 Activity
实现了 NumberPicker.OnValueChangeListener
,这使得代码更简洁。
该示例在数组中显示了 TextView
和 NumberPicker
中的每一个,但可以使用任何数字...
public class MainActivity extends Activity
implements NumberPicker.OnValueChangeListener {
private int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2};
private int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2};
private TextView[] tvs = new TextView[textViewIDs.length];
private NumberPicker np;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
}
private void setupUI() {
for (int i = 0; i < textViewIDs.length; i++) {
tvs[i] = (TextView) findViewById(textViewIDs[i]);
np = (NumberPicker) findViewById(numpickIDs[i]);
// Set the TextView to be the "tag" of the NumberPicker
np.setTag(tvs[i]);
np.setMinValue(0);
np.setMaxValue(12);
np.setOnValueChangedListener(this);
}
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// Get the TextView of the NumberPicker and set
// its text
((TextView)picker.getTag()).setText(String.valueOf(newVal));
}
}
我试图在生成它的 For 循环之外使用 tvs[i]
变量,在另一种方法 OnValueChange
中,为 NumberPickers
。
但是,我只为第一个 tvs[i]
NumberPicker
生成一个 TextView
值,无论我旋转哪个 NumberPicker
。
谁能解释一下我在下面的代码中哪里出错了?我已经尝试将 For 循环扩展到 OnChangeListener
方法下方,但是我遇到了 tvs[i]
没有 final
值等问题。
link 与 tvs[i].setText
的初始 tvs[i]
值是什么??
public void setupUI()
{
int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2, R.id.tvId3, R.id.tvId7,
R.id.tvId8, R.id.tvId9, R.id.tvId10, R.id.tvId11, R.id.tvId12, R.id.tvId13, R.id.tvId14, R.id.tvId15, R.id.tvId16, R.id.tvId17,
R.id.tvId18, R.id.tvId19, R.id.tvId20, R.id.tvId21, R.id.tvId22, R.id.tvId23, R.id.tvId24, R.id.tvId25, R.id.tvId26, R.id.tvId27, R.id.tvId28, R.id.tvId29, R.id.tvId30, R.id.tvId31,
R.id.tvId32, R.id.tvId33, R.id.tvId34, R.id.tvId35, R.id.tvId36, R.id.tvId37, R.id.tvId38, R.id.tvId39, R.id.tvId40, R.id.tvId41, R.id.tvId42, R.id.tvId43, R.id.tvId44, R.id.tvId45, R.id.tvId46, R.id.tvId47, R.id.tvId48, R.id.tvId49, R.id.tvId50, R.id.tvId51, R.id.tvId52, R.id.tvId53};
tvs = new TextView[53];
for(int i=0; i < textViewIDs.length; i
++) {
tv = (TextView ) findViewById(textViewIDs[i]);
tvs[i] = tv;
}
//tv = (TextView) findViewById(R.id.tvId1);
int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2, R.id.npId3, R.id.npId7,
R.id.npId8, R.id.npId9, R.id.npId10, R.id.npId11, R.id.npId12, R.id.npId13, R.id.npId14, R.id.npId15, R.id.npId16, R.id.npId17,
R.id.npId18, R.id.npId19, R.id.npId20, R.id.npId21, R.id.npId22, R.id.npId23, R.id.npId24, R.id.npId25, R.id.npId26, R.id.npId27, R.id.npId28, R.id.npId29, R.id.npId30, R.id.npId31,
R.id.npId32, R.id.npId33, R.id.npId34, R.id.npId35, R.id.npId36, R.id.npId37, R.id.npId38, R.id.npId39, R.id.npId40, R.id.npId41, R.id.npId42, R.id.npId43, R.id.npId44, R.id.npId45, R.id.npId46, R.id.npId47, R.id.npId48, R.id.npId49, R.id.npId50, R.id.npId51, R.id.npId52, R.id.npId53};
for(int j=0; j < numpickIDs.length; j++) {
NumberPicker np = (NumberPicker ) findViewById(numpickIDs[j]);
//NumberPicker np = (NumberPicker) findViewById(R.id.npId1);
np.setOnValueChangedListener(new OnValueChangeListener()
{
public void onValueChange(NumberPicker picker, int oldVal,
int newVal)
{
tvs[i].setText(String.valueOf(newVal));
}
});
np.setMaxValue(12);
np.setMinValue(0);
}
}
在调用 setOnValueChangedListener 之前,添加这一行
final TextView curTextView = tvs[j];
然后将 onValueChanged 更改为
curTextView.setText(String.valueOf(newVal));
如果您想要更复杂的东西,您可以随时使用 View
class 的 "tag" 功能。
基本上,您可以将每个 TextView
设置为关联 NumberPicker
的 "tag",从而使 onValueChange
方法可以轻松访问并设置相关 TextView
.
在我的代码中,我也做到了,因此 Activity
实现了 NumberPicker.OnValueChangeListener
,这使得代码更简洁。
该示例在数组中显示了 TextView
和 NumberPicker
中的每一个,但可以使用任何数字...
public class MainActivity extends Activity
implements NumberPicker.OnValueChangeListener {
private int[] textViewIDs = new int[] {R.id.tvId1, R.id.tvId2};
private int[] numpickIDs = new int[] {R.id.npId1, R.id.npId2};
private TextView[] tvs = new TextView[textViewIDs.length];
private NumberPicker np;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
}
private void setupUI() {
for (int i = 0; i < textViewIDs.length; i++) {
tvs[i] = (TextView) findViewById(textViewIDs[i]);
np = (NumberPicker) findViewById(numpickIDs[i]);
// Set the TextView to be the "tag" of the NumberPicker
np.setTag(tvs[i]);
np.setMinValue(0);
np.setMaxValue(12);
np.setOnValueChangedListener(this);
}
}
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// Get the TextView of the NumberPicker and set
// its text
((TextView)picker.getTag()).setText(String.valueOf(newVal));
}
}