单击按钮时打开随机活动 [英] Open random activities when button is clicked

查看:62
本文介绍了单击按钮时打开随机活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 5 activities. What I want to happen is to open random activities when the Start button is clicked on the MainMenu.

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

For Example: Activity 1 -> Activity 4 -> Activity 3

我已经尝试在此处

I have tried the codes posted here and here but none of them worked.

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

This is my code in Start Button

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

任何代码段都会有很大的帮助.预先感谢!

Any code snippets would be a big help. Thanks in advance!

推荐答案

要启动活动,您需要使用意图.您可以在单击按钮时调用它,如下所示:

To start an activity, you need to use intents. And you can call this when a button is clicked like so:

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

要使其随机,我们需要对其稍作更改,使其更像这样:

To make this random, we need to change it slightly so it is more like this:

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

您可以在此处如果您愿意.

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

If you don't want to open an Activity that has already been opened, that would be a little more complex.

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

In your main activity add the code below (it is almost the same as the one in the previous answer, but a little different):

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个活动中,使用下面的代码:

In all other 5 activities, use the code below:

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

这篇关于单击按钮时打开随机活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆