【问题标题】:Open random activities when button is clicked单击按钮时打开随机活动
【发布时间】:2015-04-11 14:45:27
【问题描述】:

我有 5 个活动。我想要发生的是在单击 MainMenu 上的“开始”按钮时打开随机活动。

例如:活动 1 -> 活动 4 -> 活动 3

我尝试了here 和here 发布的代码,但没有一个有效。

这是我在开始按钮中的代码

        gotoMenu=(Button)findViewById(R.id.btnStart);
        gotoMenu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                startActivity(new Intent(MainMenu.this, Menu_WelcomeLuzon.class));
                overridePendingTransition(R.animator.transition_fade_in, R.animator.transition_fade_out);
 
            }
        });

【问题讨论】:

    标签: android random


    【解决方案1】:

    要开始一项活动,您需要使用意图。您可以在单击按钮时调用它,如下所示:

    Button myButton = (Button) findViewById(R.id.MY_BUTTON);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent intent = new Intent(CurrentActivity.class, NextActivity.class);
           startActivity(intent);
        }
    

    为了让它变得随机,我们需要稍微改变一下,让它更像这样:

    Button myButton = (Button) findViewById(R.id.MY_BUTTON);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Here, we are generating a random number
            Random generator = new Random();
            int number = generator.nextInt(5) + 1; 
            // The '5' is the number of activities
    
            Class activity = null;
        
            // Here, we are checking to see what the output of the random was
            switch(number) { 
                case 1:
                    // E.g., if the output is 1, the activity we will open is ActivityOne.class
                    activity = ActivityOne.class;
                    break;
                case 2:
                    activity = ActivityTwo.class;
                    break;
                case 3:
                    activity = ActivityThree.class;
                    break;
                case 4:
                    activity = ActivityFour.class;
                    break;
                default:
                    activity = ActivityFive.class;
                    break;
            }
            // We use intents to start activities
            Intent intent = new Intent(getBaseContext(), activity);
            startActivity(intent);
        }
    }
    

    如果您愿意,可以阅读更多关于启动活动和使用意图的信息here。

    更新问题/评论的更新答案:

    如果你不想打开已经打开的 Activity,那会稍微复杂一些。

    在您的主要活动中添加以下代码(与上一个答案中的代码几乎相同,但略有不同):

    Button myButton = (Button) findViewById(R.id.MY_BUTTON);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // We are creating a list, which will store the activities that haven't been opened yet
            ArrayList<Class> activityList = new ArrayList<>();
            activityList.add(ActivityOne.class);
            activityList.add(ActivityTwo.class);
            activityList.add(ActivityThree.class);
            activityList.add(ActivityFour.class);
            activityList.add(ActivityFive.class);
    
            Random generator = new Random();
            int number = generator.nextInt(5) + 1; 
    
            Class activity = null;
        
            // Here, we are checking to see what the output of the random was
            switch(number) { 
                case 1:
                    activity = ActivityOne.class;
                    // We are adding the number of the activity to the list
                    activityList.remove(ActivityOne.class);
                    break;
                case 2:
                    activity = ActivityTwo.class;
                    activityList.remove(ActivityTwo.class);
                    break;
                case 3:
                    activity = ActivityThree.class;
                    activityList.remove(ActivityThree.class);
                    break;
                case 4:
                    activity = ActivityFour.class;
                    activityList.remove(ActivityFour.class);
                    break;
                default:
                    activity = ActivityFive.class;
                    activityList.remove(ActivityFive.class);
                    break;
            }
            // We use intents to start activities
            Intent intent = new Intent(getBaseContext(), activity);
            // `intent.putExtra(...)` is used to pass on extra information to the next activity
            intent.putExtra("ACTIVITY_LIST", activityList);
            startActivity(intent);
        }
    }
    

    在所有其他 5 个活动中,使用以下代码:

    Button myButton = (Button) findViewById(R.id.ANOTHER_BUTTON);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ArrayList<Class> activityList = new ArrayList<>();
            Bundle extras = getBaseContext().getIntent().getExtras();
            activityList = extras.get("ACTIVITY_LIST");
    
            if(activityList.size() == 0) {
                // Do something when after all activities have been opened
                doSomeAction();
            } else {
                // Now, the random number is generated between 1 and however many 
                // activities we have remaining
                Random generator = new Random();
                int number = generator.nextInt(activityList.size()) + 1; 
    
                Class activity = null;
        
                // Here, we are checking to see what the output of the random was
                switch(number) { 
                    case 1:
                        // We will open the first remaining activity of the list
                        activity = activityList.get(0);
                        // We will now remove that activity from the list
                        activityList.remove(0);
                        break;
                    case 2:
                        // We will open the second remaining activity of the list
                        activity = activityList.get(1);
                        activityList.remove(1);
                        break;
                    case 3:
                        // We will open the third remaining activity of the list
                        activity = activityList.get(2);
                        activityList.remove(2);
                        break;
                    case 4:
                        // We will open the fourth remaining activity of the list
                        activity = activityList.get(3);
                        activityList.remove(3);
                        break;
                    default:
                        // We will open the fifth remaining activity of the list
                        activity = activityList.get(4);
                        activityList.remove(4);
                        break;
                }
    
                // Note: in the above, we might not have 3 remaining activities, for example,
                // but it doesn't matter because that case wouldn't be called anyway,
                // as we have already decided that the number would be between 1 and the number of
                // activities left.
    
    
                // Starting the activity, and passing on the remaining number of activities 
                // to the next one that is opened
                Intent intent = new Intent(getBaseContext(), activity);
                intent.putExtra("ACTIVITY_LIST", activityList);
                startActivity(intent);
            }
        }
    }
    

    编辑:[07/07/2020]

    这篇文章是很久以前写的,我忽略了很多东西。

    Tenfour04 是正确的,我们需要 switch 语句,因此可以更简洁:

    int r = new Random().nextInt(activityList.size);
    Class<? extends Activity> activity = activityList.removeAt(r);
    

    【讨论】:

    • switch 语句是不必要的冗长。你可以使用Class&lt;? extends Activity&gt; activity = activityList.removeAt(new Random().nextInt(activityList.size));
    • 您确实需要将删除的项目存储到保存的实例状态并在屏幕旋转的情况下恢复它们。
    • @Tenfour04 你是对的 - 我不敢相信我忽略了这一点。无论如何,我已经根据您的建议编辑了我的答案 - 谢谢!
    【解决方案2】:

    @Farbod Salamat-Zadeh 你的解决方案很棒,除了那部分

    Bundle extras = getBaseContext().getIntent().getExtras();
    

    在 Android Studio 2.1 中会导致错误。 这应该是

    Bundle extras = getIntent().getExtras();
    

    【讨论】:

    • 嗨 Artem - 刚刚看到你的答案。事实上,您运行的 Android Studio 版本不应该影响您使用的代码 - 无论如何都应该相同。但是,代码会有所不同,具体取决于您使用它的位置。如果在Activity 中,则为getIntent().getExtras();如果在Fragment 中,在此之前我们需要getActivity()。这是因为方法getIntent() is part of the Activity class.
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    相关资源
    最近更新 更多