我创建了5个textview,现在我想知道哪个textview被触及了。我已经制作了一个触摸列表,但它只引用了最后一个... plz指导我。
final LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
for(int i=0; i< 5;i++){
tv = new TextView(this);
tv.setTextSize(90);
tv.setId(i);
tv.setText("Text");
tvlist.add(tv);
ll.addView(tv, i);
}
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.i("TAG", "touchdown" + Integer.toString(v.getId()));
Log.i("TAG", "touchdown 2" + tvlist.get(v.getId()));
return false;
}
});
答案 0 :(得分:1)
像这样使用,为了更好地处理textview,你应该使用TextView数组
for(int i=0; i< 5;i++){
tv = new TextView(this);
tv.setTextSize(90);
tv.setId(i);
tv.setText("Text");
tvlist.add(tv);
ll.addView(tv, i);
tv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.i("TAG", "touchdown" + Integer.toString(v.getId()));
Log.i("TAG", "touchdown 2" + tvlist.get(v.getId()));
return false;
}
});
}
答案 1 :(得分:-1)
您也可以按如下方式使用:
TextView tv=new Textview[5];
int txtid =0;
for(int i=0; i< 5;i++){
tv = new TextView(this);
tv[i].setTextSize(90);
tv[i].setId(i);
tv[i].setText("Text");
tv[i].setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
txtid = tv[i].getId();
return false;
}
});
}
ll.addView(tv);