首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单击按钮可在活动列表中从一个活动切换到随机活动

单击按钮可在活动列表中从一个活动切换到随机活动
EN

Stack Overflow用户
提问于 2013-08-14 19:05:24
回答 1查看 427关注 0票数 0

我想要的最好的方式去从一个类的按钮点击到一个活动列表中的随机活动…请告诉我在java中我应该把它放在哪里..正如您所看到的,我已经在尝试一种方法,但是我不能重复其他方法,并且由于某种原因,我在使用rand时遇到错误,java不能识别它。提前谢谢伙计们

代码语言:javascript
复制
    package com.example.whattodo2;

    import java.util.Random;

    import android.os.Bundle;
    import android.app.Activity;
     import android.content.Intent;

    import android.view.Menu;
    import android.view.View;

    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.ImageView;


    public class Title extends Activity {



    Button reset, rts;
    ImageView title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title);
        Animation a = AnimationUtils.loadAnimation(this, R.animator.animation);
         ImageView rImage = (ImageView) findViewById(R.id.title);
         rImage.startAnimation(a);
         func();

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

                    @Override
                    public void onClick(View arg0) {
                        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); }
                    }
                });
        rts = (Button) findViewById(R.id.rts);
        rts.setOnClickListener(new View.OnClickListener() {

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

                    }
                });


    }

    protected void func() {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.title, menu);
        return true;
    }



}
EN

回答 1

Stack Overflow用户

发布于 2013-08-14 19:22:32

将所有活动的名称存储在一个数组中,生成随机数,并获取与随机生成的数相对应的活动。示例代码段

代码语言:javascript
复制
    String[] title = new String[] { "Act1.class", "Act2.class","Act1.class","Act4.class","Act5.class"};

public String getRandomActivity(){
    Random randomGenerator = new Random();
            int randomInt = randomGenerator.nextInt(5);// pass number of elements as parameters
        return title[randomInt-1];//this should return class name

        }

Intent intent = new Intent(this,getRandomActivity())
startActivity(intent)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18230048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档