【问题标题】:How to go to the next random activity on android studio?如何在 android studio 上进行下一个随机活动?
【发布时间】:2015-09-20 01:25:50
【问题描述】:

How can I go to the next activity in random for example, on the first activity there is a button which will go to the 2nd, 3rd or 4th activity.when one of the 3 is selected how can I go to the next 2随机?然后当它完成时,它将再次回到第一个活动。请帮忙,我所知道的只是“意图”,它只会用于特定的活动。

【问题讨论】:

    标签: android android-intent random android-studio


    【解决方案1】:

    类似的东西

    Class[] classes = new Class[3];
    classes[0] = Activity0.class;
    classes[1] = Activity1.class;
    classes[2] = Activity2.class;
    Random r = new Random();
    
    Intent i = new Intent(this, classes[r.nextInt(classes.length)]);
    startActivity(i);
    

    【讨论】:

    【解决方案2】:

    您可以将活动的引用存储在存储在 SharedPreferences 中的 Set 或 Array 中以保持一致性。 然后,就像 letz 回答一样,只需使用简单的 Random 来获取随机活动。

    您可能还想从数组中弹出值以防止重复的意图,并在数组为空后返回第一个活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 2022-12-15
      • 2020-11-10
      • 1970-01-01
      相关资源
      最近更新 更多