在下面的代码中,我将为两个表分别创建一个单击侦听器。如何为两者创建单击事件以比较其行ID。因为在比较行之后我必须在它们之间划一条线。
for (i = 0; i < data.getItem().size(); i++) {
final TableRow tr = new TableRow(this);
final TableRow tr1 = new TableRow(this);
tr.setPadding(2, 2, 2, 2);
TableLayout.LayoutParams tableRowParams=
new TableLayout.LayoutParams
(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.FILL_PARENT);
int leftMargin=0;
int topMargin=2;
int rightMargin=0;
int bottomMargin=1;
tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
tr.setLayoutParams(tableRowParams);
tr1.setLayoutParams(tableRowParams);
match1[i]= new TextView(this);
match1[i].setId(i);
System.out.println(match1[i].getId());
match1[i].setText(data.getItem().get(i));
System.out.println(match1[i].getText().toString());
match1[i].setTextColor(Color.WHITE);
match1[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
match1[i].setTextSize(12);
match1[i].setPadding(10,0,0,0);
tr.addView(match1[i]);
Log.e("TAG", "ID: "+match1[i].getId());
final View row=t1.getChildAt(i);
row.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int row_id=t1.indexOfChild(row);
Log.e("TAG ID for Table 1", "ID: "+row_id);
//Toast.makeText(getBaseContext(), row_id, Toast.LENGTH_SHORT).show();
}
});
t1.addView(tr, tableRowParams);
match2[i]= new TextView(this);
match2[i].setId(i);
match2[i].setText(data.getTarget().get(i));
System.out.println(match2[i].getText().toString());
match2[i].setTextColor(Color.WHITE);
match2[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
match2[i].setTextSize(12);
//match2[i].setPadding(10,0,0,0);
tr.addView(match2[i]);
Log.e("TAG ID For Tabel 2", "ID: "+match1[i].getId());
final View row1=t2.getChildAt(i);
row1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
int row_id=t2.indexOfChild(row1);
Log.e("TAG", "ID: "+row_id);
//Toast.makeText(getBaseContext(), row_id, Toast.LENGTH_SHORT).show();
}
});
t2.addView(tr1, tableRowParams);
/*if(match1[i].getId() == match2[i].getId()){
//draw.onDraw(canvas);
Toast.makeText(this, "Yipee", Toast.LENGTH_SHORT).show();
}else
Toast.makeText(this, ":(((((", Toast.LENGTH_SHORT).show();*/
}
答案 0 :(得分:2)
private class MyOnClickListener implements OnClickListener {
ViewGroup group;
private MyOnClickListener(ViewGroup group) {
this.group = group;
}
@Override
public void onClick(View view) {
int row_id = group.indexOfChild(row1); // or whatever
Log.e("TAG", "ID: "+row_id);
}
}
////////
MyOnClickListener listener = new MyOnClickListener(t1);
row.setOnClickListener(listener);
...
MyOnClickListener listener = new MyOnClickListener(t2);
anotherRow.setOnClickListener(listener);
答案 1 :(得分:2)
你可以这样做:
OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// whatever you want
}
};
// ...
row.setOnClickListener(listener);
row1.setOnClickListener(listener);
答案 2 :(得分:0)
两种选择:
让你的类实现OnClickListener
并在里面实现它:
class Custom implements OnClickListener{
public void onClick(View v){
switch(v.getId()){
case ROW1_ID:
....
break;
case ROW2_ID:
....
break;
}
}
}
像这样使用它:
row1.setOnClickListener(this)
row.setOnClickListener(this)
或使用像这样的简单变量:
private OnClickListener myListener = new OnClickListener(){
public void onClick(View v){
switch(v.getId()){
case ROW1_ID:
....
break;
case ROW2_ID:
....
break;
}
}
像这样使用它:
row1.setOnClickListener(myListener);
row.setOnClickListener(this)