Android:测验应用程序中的随机活动

时间:2013-12-07 04:36:13

标签: java android

我正在寻找随机活动教程,但我的应用程序并没有真正有效。

在回答测验并点击下一个测验后,我想启动随机活动。

以下是我在其中一项测验活动中所做的代码但还没有随机代码

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class G2g1Activity extends Activity {


private Button btnOK;
private Button btnHint;
private EditText inputtxt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_g2g1);


    inputtxt = (EditText) findViewById(R.id.g2g1_et);
    btnOK = (Button) findViewById(R.id.g2g1_btnOK);
    btnHint = (Button) findViewById(R.id.g2g1_btnHint);

     btnOK.setOnClickListener(new View.OnClickListener() 
       {

       @Override
       public void onClick(View ContentView) {
           // TODO Auto-generated method stub

         String name;

             name=inputtxt.getText().toString();

             if (name.equalsIgnoreCase("Cash"))
             {

               Toast.makeText(getApplicationContext(), "CASH", Toast.LENGTH_SHORT).show();


               Intent i = new Intent (G2g1Activity.this, G2g2Activity.class);
               startActivity(i);
               finish();

             }
             else
             {  Toast.makeText(getApplicationContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
             }     


            };
            });

         btnHint.setOnClickListener(new View.OnClickListener() 
            {

                @Override
                public void onClick(View ContentView) {
                    // TODO Auto-generated method stub

                    Toast.makeText(getApplicationContext(), "Money, owned money, type + Currency, Coins, Exchange", Toast.LENGTH_LONG).show();


            };
            });

    }


}

3 个答案:

答案 0 :(得分:4)

您可以根据自己的活动数量生成一个随机数。然后使用switch语句以随机数开始活动。

例如,

int min = 1;
int max = 10;

Random r = new Random();
int random = r.nextInt(max - min + 1) + min; //+1 if max is inclusive

然后switch

switch(random) {

case 1: //Activity A
break;

case 2: //Activity B
break;

case 3: //Activity C
break;

.
.
.
}

答案 1 :(得分:1)

尝试下面的代码,我在字符串数组中使用默认的5个活动。

String[] title = new String[] { "Activity1.class", "Activity2.class","Activity3.class","Activity4.class","Activity5.class"};

public String getRandomAct(){
    Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(5);
        return title[randomInt-1];

        }

Intent i= new Intent(this,getRandomAct())
startActivity(i);

答案 2 :(得分:0)

创建一个包含所有活动名称的字符串数组

    String activities[]={"activity1","activity2"...}

然后创建一个随机整数并随机选择一个字符串变量,然后通过以下方法将其添加到新意图中:

    Random random=new Random;
    String randomstr=activities[random.nextInt(activities.length-1)];
Class   ourClass=Class.forName("com.example.quiz."+randomstr);
    Intent i= new Intent(this.currentactivity,ourClass):