这是我的自定义OnClickListener
。我正在传递我的 Gridview 按钮的位置ButtonAdapter.class
。我想为网格视图的每个按钮打开一个新活动。我应该在我的MyOnClickListener.class
?
MyOnClickListener.class
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
public class MyOnClickListener implements OnClickListener {
private final int position;
public MyOnClickListener(int position)
{
this.position = position;
}
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), WordsList.class);
v.getContext().startActivity(intent);
}
}
我得到了解决方案。我试过了,它有效!
public class MyOnClickListener implements OnClickListener {
private final int position;
public MyOnClickListener(int position)
{
this.position = position;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(position){
case 0:
Intent a = new Intent(v.getContext(), WordsList.class);
v.getContext().startActivity(a);
break;
case 1:
Intent b = new Intent(v.getContext(), About.class);
v.getContext().startActivity(b);
break;
}
}
}