【发布时间】:2012-08-31 09:09:30
【问题描述】:
这是我的自定义OnClickListener。我从ButtonAdapter.class 传递我的Gridview 按钮的位置。我想为网格视图的每个按钮打开一个新活动。我应该对我的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;
}
}
}
【问题讨论】:
-
你现在面临什么问题?
-
问题已解决。我使用了 switch(position){case 0:....}
标签: android android-intent android-button onclicklistener