Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓-设置随机按钮图像可点击_Android_Image_Button_Random - Fatal编程技术网

Android 安卓-设置随机按钮图像可点击

Android 安卓-设置随机按钮图像可点击,android,image,button,random,Android,Image,Button,Random,我正在兜售我的随机生成的图像,这是作为一个按钮阅读成为可点击的,这导致了每一个不同的图像不同的活动。所以随机图像工作得很完美,唯一的问题是它不能点击这里是我的代码 final Button imgView = (Button)findViewById(R.id.top1); Random rand = new Random(); int rndInt = rand.nextInt(4) + 1; Strin

我正在兜售我的随机生成的图像,这是作为一个按钮阅读成为可点击的,这导致了每一个不同的图像不同的活动。所以随机图像工作得很完美,唯一的问题是它不能点击这里是我的代码

        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函数,并从它得到的文本中调用相应的活动。。。如果您不理解,请点击我。

有一种方法:创建一个新类

class ActivitySwitch implements OnClickListener{
    int imageNo;
    Activity context
    public ActivitySwitch(int imageNo,Context context) {
        super();
        this.context=(Activity)context;
        this.imageNo = imageNo;
    }
    @Override
    public void onClick(View v) {
        Intent it=new Intent();
        if(imageNo==1){
         it.setClass(context, Activity1.class);
            }
            else{
            .......
            }
            startActivityForResult(it,any_integer_value);




    }

}
然后在活动集中:


您是否在ImageView中添加了OnClickListener?我的朋友,您还需要开始活动。我在回答中做了一些修改,请看这里,所以我制作了上面提到的ActivityClass,然后在主类中添加了这个:imgView.setOnClickListenernew ActivitySwitch1,这个;但仍然无法单击按钮。。有什么解决办法吗请粘贴主活动和ActivitySwitch类的代码为什么设置了这么多onclickListener,它只会被最后一个覆盖。只需设置我在你的答案中建议的一个。它将满足您的要求,因为当我写:imgView.setonclicklistenernewactivityswitcherrndint时,这个;它给了我一个“rndInt”的错误。在onClickListener上实际应该更改什么?rndInt无法解析为变量,顺便说一句,在ActivitySwitch类上,当我将“any_integer_value”更改为例如“1”时,它也会给我一个错误“类型ActivitySwitch的方法StartActivityForresultitent,int未定义”
imgView.setOnClickListener(new ActivitySwitcher(randInt,this);