简体   繁体   English

意向活动切换为随机活动

[英]intent activity switching to random activity

iam trying to switch from say activity 1 to activity 2 when a button is clicked. 我试图在单击按钮时从活动1切换到活动2。 and ive got how to do that. 我知道该怎么做。 what i cant figure out is how to get it so 50% of the time it goes to activity 2 and the other 50% to activity 3. iam sorry i know its a very obvious question. 我不知道是如何获得它的,所以50%的时间去了活动2,另外50%的时间去了活动3。对不起,我知道这是一个非常明显的问题。 iam new to this so could you please explain a little and also similar effects like switching to completely randomn out of say 5 different specified activites. 我对此不陌生,所以请您说明一下,还提供类似的效果,例如从5种不同的指定活动中完全随机化。 thanks in advance ... intent code below. 在此先感谢...下面的意图代码。 please explain using my code if possible. 请解释使用我的代码(如果可能)。

 yes.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent yes1=new Intent(MainActivity.this,Question2.class);
            startActivity(yes1);

Off the top of my head, so it might not compile completely :-) but the idea is sound 不在我的头上,所以它可能无法完全编译:-),但是这个想法很合理

List<Class<? extends Activity>> activities = new ArrayList<Class<? extends Activity>>();
activities.add(Question.class);
activities.add(Question2.class);

Random generator = new Random();
Class<? extends Activity> randomActivity = activities.get(generator.nextInt(activities.size()));

Intent intent = new Intent(this, randomActivity);
startActivity(intent);

You could just use Random class to decide which will be your next intent. 您可以只使用Random类来确定下一个意图。 Please, take this code snippet as an example.- 请以此代码段为例。-

Intent newIntent = null;
Random rand = new Random();

int index = rand.nextInt(4);
switch (index) {
    case 0:
        newIntent = new Intent(this, Question1.class);
    break;
    case 1:
        newIntent = new Intent(this, Question2.class);
    break;
    case 2:
        newIntent = new Intent(this, Question3.class);
    break;
    case 3:
        newIntent = new Intent(this, Question4.class);
    break;
}

startActivity(newIntent);

Try this: 尝试这个:

yes.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View arg0) {
       double rand = Math.random();
       if(rand < 0.5){
           Intent yes1 = new Intent(MainActivity.this, Question2.class);
           startActivity(yes1);
       } else {
           Intent yes2 = new Intent(MainActivity.this, Question3.class);
           startActivity(yes2);
       }
   }
});

Similar for random switching between 5 different activities. 在5种不同活动之间随机切换的情况类似。 You could probably optimize it so you're not just if branching between all your different options if you have so many different activities. 您可能会对其进行优化,因此,如果您有许多不同的活动,则不只是在所有不同选项之间进行分支。

You could set up a String Array of the class names like 您可以设置类名称的String Array ,例如

String[] classes = new String[3];
classes[0] = Activity1;
classes[1] = Activity2;
classes[2] = Activity3;

Then use a Random number to pick between them and on click do something like 然后使用Random数在它们之间选择,然后单击以执行类似操作

 @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Random r = new Random();
        int number = r.nextInt(4);
        String curClass = classes[number);
        Intent yes1=new Intent(MainActivity.this,Class.forName(curClass));
        startActivity(yes1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM