单击按钮时打开随机活动

普卡特19

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

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

我已经尝试过在这里这里发布的代码但是没有一个起作用。

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

        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);
 
            }
        });
Farbod Salamat-Zadeh

要开始活动,您需要使用意图。当单击按钮时,可以这样调用:

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);
    }
}

如果愿意,您可以在此处阅读有关开始活动和使用意图的更多信息

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

如果您不想打开一个已经打开的活动,那会有些复杂。

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

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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

单击按钮时如何打开新活动?

来自分类Dev

当我单击按钮打开此活动时,它关闭

来自分类Dev

如何在单击按钮时打开两个活动

来自分类Dev

单击按钮时打开Webview

来自分类Dev

单击按钮时打开Webview

来自分类Dev

Android如何在单击按钮时使用不同的相对布局视图打开相同的活动?

来自分类Dev

如何在单击按钮时打开特定的选项卡式活动?

来自分类Dev

通知单击时如何打开“活动”

来自分类Dev

单击通知时如何打开android活动

来自分类Dev

单击通知时打开了错误的活动

来自分类Dev

单击按钮时的随机XML布局

来自分类Dev

单击按钮时从活动中删除片段?

来自分类Dev

Eclipse,单击按钮时的新活动

来自分类Dev

单击按钮时启动“导航抽屉活动”

来自分类Dev

防止单击按钮时打开引导模态

来自分类Dev

防止单击按钮时打开VBE

来自分类Dev

单击按钮时从 Fragment 打开 Fragment

来自分类Dev

单击打开输入的按钮时聚焦

来自分类Dev

在android中单击按钮打开新的课程/活动

来自分类Dev

如何打开系统活动并单击那里的按钮?

来自分类Dev

单击通知活动中的后退按钮后,Android 打开 MainActivity

来自分类Dev

单击按钮时如何在js中获得活动按钮

来自分类Dev

打开其他按钮时如何从按钮上删除活动

来自分类Dev

单击按钮重复单击按钮会重复打开新活动。如何避免这种情况

来自分类Dev

单击打开按钮时,Div打开和关闭

来自分类Dev

单击打开按钮时,Div打开和关闭

来自分类Dev

Angular - 单击按钮时使按钮处于活动状态,并在单击组中的其他按钮时处于非活动状态

来自分类Dev

ListView&onItemClick-单击时,打开新活动

来自分类Dev

单击cardview时无法打开新活动,但仅显示吐司

Related 相关文章

  1. 1

    单击按钮时如何打开新活动?

  2. 2

    当我单击按钮打开此活动时,它关闭

  3. 3

    如何在单击按钮时打开两个活动

  4. 4

    单击按钮时打开Webview

  5. 5

    单击按钮时打开Webview

  6. 6

    Android如何在单击按钮时使用不同的相对布局视图打开相同的活动?

  7. 7

    如何在单击按钮时打开特定的选项卡式活动?

  8. 8

    通知单击时如何打开“活动”

  9. 9

    单击通知时如何打开android活动

  10. 10

    单击通知时打开了错误的活动

  11. 11

    单击按钮时的随机XML布局

  12. 12

    单击按钮时从活动中删除片段?

  13. 13

    Eclipse,单击按钮时的新活动

  14. 14

    单击按钮时启动“导航抽屉活动”

  15. 15

    防止单击按钮时打开引导模态

  16. 16

    防止单击按钮时打开VBE

  17. 17

    单击按钮时从 Fragment 打开 Fragment

  18. 18

    单击打开输入的按钮时聚焦

  19. 19

    在android中单击按钮打开新的课程/活动

  20. 20

    如何打开系统活动并单击那里的按钮?

  21. 21

    单击通知活动中的后退按钮后,Android 打开 MainActivity

  22. 22

    单击按钮时如何在js中获得活动按钮

  23. 23

    打开其他按钮时如何从按钮上删除活动

  24. 24

    单击按钮重复单击按钮会重复打开新活动。如何避免这种情况

  25. 25

    单击打开按钮时,Div打开和关闭

  26. 26

    单击打开按钮时,Div打开和关闭

  27. 27

    Angular - 单击按钮时使按钮处于活动状态,并在单击组中的其他按钮时处于非活动状态

  28. 28

    ListView&onItemClick-单击时,打开新活动

  29. 29

    单击cardview时无法打开新活动,但仅显示吐司

热门标签

归档