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);
答案 0 :(得分:1)
离开我的头顶,所以它可能无法完全编译:-)但这个想法是合理的
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);
答案 1 :(得分:1)
您可以使用Random
课来决定下一个意图。请以此代码段为例.-
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);
答案 2 :(得分:0)
试试这个:
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);
}
}
});
类似于5种不同活动之间的随机切换。您可以优化它,这样,如果您有许多不同的活动,那么您不仅可以在所有不同选项之间进行分支。
答案 3 :(得分:0)
您可以设置String Array
类名称,例如
String[] classes = new String[3];
classes[0] = Activity1;
classes[1] = Activity2;
classes[2] = Activity3;
然后使用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);