按钮单击以从活动列表中将活动切换为随机

时间:2013-08-14 11:54:28

标签: android

我只想点击按钮切换到可能的活动列表中的不同活动,比如说activity1,activity2等。由于某种原因,我当前的方法不允许我使用超过2,现在(rand <0.5)也给出了某种兰特双重错误。

reset = (Button) findViewById(R.id.reset);
reset.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v){
        Random rand = new Random();
        if (rand <0.5) {
               Intent reset1 = new Intent(Title.this, MainActivity.class);
               startActivity(reset1);
           } else {
               Intent reset2 = new Intent(Title.this, Question36.class);
               startActivity(reset2); 
        }
    }

});

3 个答案:

答案 0 :(得分:0)

试试这个

Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(100);

 if(randomInt <50){
                           Intent reset1 = new Intent(Title.this, MainActivity.class);
                           startActivity(reset1);
                       } else {
                           Intent reset2 = new Intent(Title.this, Question36.class);
                           startActivity(reset2); 
                    }

答案 1 :(得分:0)

在AndroidManifest文件中进行更改。

<activity
    android:name="com.example.alphabets.LoginActivity"
    android:label="@string/title_activity_login"
    android:windowSoftInputMode="adjustResize|stateVisible" >

<intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

</activity>

<activity
    android:name="com.example.alphabets.MainActivity"
    android:label="@string/app_name" >
</activity>

试试吧

答案 2 :(得分:0)

假设您有5个活动

   reset = (Button) findViewById(R.id.First_btnNext);
    reset.setOnClickListener(new View.OnClickListener() {

       @Override
            public void onClick(View v){
                 Random rand = new Random();
                 int num=rand.nextInt(5);

                 switch (num) {
            case 0:
                     Intent reset0 = new Intent(Title.this, FirstActivity.class);
                     startActivity(reset0);
                break;
            case 1:
                     Intent reset1 = new Intent(Title.this, SecondActivity.class);
                     startActivity(reset1);
                break;
            case 2:
                    Intent reset2 = new Intent(Title.this, ThirdActivity.class);
                    startActivity(reset2);
                break;
            case 3:
                    Intent reset3 = new Intent(Title.this, FourthActivity.class);
                    startActivity(reset3);
               break;
            case 4:
                    Intent reset4 = new Intent(Title.this, FifthActivity.class);
                    startActivity(reset4);
               break;
          default:
            break;
        }
      }
   });