简体   繁体   中英

How to use randomization, so when you press a button it brings you to a random screen

I have searched for an answer to this question with my time in my Computer Science lab. We are using Android Studio for this app.

What I want to do is to use randomization to make a set of screens be randomized when you click a button. My duo is working on a dice rolling app, and we had the idea to make six different screens for each of the sides of the die. Basically, when we click the button to "roll the dice", it think for a second, then brings you to a random page with a picture of the number on the die which you got.

This is incredibly weird, and I have searched for at least 3 hours straight for a solution to this problem but to no avail. If anybody needs more information on the problem (because I do not know how to properly phrase it), then just ask me.

Just use Random.nextInt() to get a random number up to 6, and use that to choose one image of the 6 for each die side. You do not need to create 6 different screens, you just need 6 different images where the number indicates which image to use. For example:

// A list of drawables you've defined in /res/drawable folder for each die side
final int[] images = new int[6] {
    R.drawable.die_side_1,
    R.drawable.die_side_2,
    R.drawable.die_side_3,
    R.drawable.die_side_4,
    R.drawable.die_side_5,
    R.drawable.die_side_6
};

int random = Random.nextInt(6);       // Get random value, 0-5
int dieSideDrawable = images[random]; // Pick image to show based on random value
mDieImageView.setImageResource(dieSideDrawable); // Show image on an image view

Hope that helps!

The easiest way to do exactly what you want would be to put the Activities in an Array, and select by using the Random class' nextInt method to choose the appropriate activity from the Array.

That being said, most likely you want to create a single activity with two images, and instead of selecting an activity or Fragment to show, you'd select the images you'd load into the Activity.

I would recommend using Fragments to achieve this.

create a list of fragments

ArrayList<Fragment> fragmentList = new ArrayList<>();

Now use the java Random class to generate the random number.

Random rand = new Random();

int  n = rand.nextInt(fragmentList.size());

then just show that fragment.

getSupportFragmentManager()
                .beginTransaction()
                .replace(containerViewId, fragmentList.get(n))
                .addToBackStack(null)
                .commit();

Using multiple activities seems unnecessary here (and will significantly slow down your app). If you want to show a different image based on the result of a random number that's been generated, then just .setImageResource() for your Image View based on the result of that random number.

In the example below I separated the random number generation (the generateRandomInt() method which stores a random integer in the thisRoll variable) and only called it when the changeImageView() method runs onClick.

public void changeImageView(View view){
    generateRandomInt();

    if (thisRoll == 1) {
        mainImage.setImageResource(R.drawable.s1);
    } else if (thisRoll == 2) {
        mainImage.setImageResource(R.drawable.s2);
    } else if (thisRoll == 3) {
        mainImage.setImageResource(R.drawable.s3);
    } else if (thisRoll == 4) {
        mainImage.setImageResource(R.drawable.s4);
    } else if (thisRoll == 5) {
        mainImage.setImageResource(R.drawable.s5);
    } else {
        mainImage.setImageResource(R.drawable.s6);
    }
    Toast.makeText(DiceRollActivity.this, thisRoll + " ...But The House Always Wins!", Toast.LENGTH_SHORT).show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM