我正在使我的随机生成图像(读作按钮)变得可点击,导致每个不同的图像的每个不同的活动touble。所以随机图像完美的工作实际上,它是不可点击的唯一的问题是我的代码Android - 设置随机按钮图像可点击
final Button imgView = (Button)findViewById(R.id.top1);
Random rand = new Random();
int rndInt = rand.nextInt(4) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
在我的布局,我指定的ID TOP1的按钮。 因此,上面的代码将查找我的可绘制图像,其名称分别为'img1.jpg','img2.jpg','img3.jpg'和'img4.jpg'。
所以我想要做的就是当'img1.jpg'生成时,它变得可点击并导致例如:Activity1.java,'img2.jpg'意图转到'Activity2.java'等等。
非常感谢。我打开的任何一种解决方案:)
更新:
这是我的主类的全码:
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_x);
final Button imgView = (Button)findViewById(R.id.top1);
Random rand = new Random();
imgView.setOnClickListener(new ActivitySwitch(1,this));
imgView.setOnClickListener(new ActivitySwitch(2,this));
imgView.setOnClickListener(new ActivitySwitch(3,this));
imgView.setOnClickListener(new ActivitySwitch(4,this));
int rndInt = rand.nextInt(4) + 1;
String imgName = "img" + rndInt;
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
imgView.setBackgroundResource(id);
}
这里的ActivitySwitch类:
public class ActivitySwitch implements OnClickListener{
int imageNo;
Context context;
public ActivitySwitch(int imageNo,Context context) {
super();
this.context=context;
this.imageNo = imageNo;
}
@Override
public void onClick(View v) {
Intent it=new Intent();
if(imageNo==1)
{
it.setClass(context, ProjektAID.class);
}
else if (imageNo==2)
{
it.setClass(context, ProjektADH.class);
}
else if (imageNo==3)
{
it.setClass(context, ProjektBOS.class);
}
else if (imageNo==4)
{
it.setClass(context, ProjektBROT.class);
}
}
}
您所添加的OnClickListener到您的ImageView? – RoflcoptrException 2011-03-14 08:58:16
以及我的朋友你也需要开始活动。我在我的答案plz中做了一些更改,请参阅 – 2011-03-14 12:52:56