简体   繁体   English

将随机文本设置为多个TextView,而无需任何重复

[英]Set Random Text To Multiple TextView Without Any Repetition

Hello Guys I am New to Android. 大家好,我是Android新手。 I am developing an App but stuck here. 我正在开发一个应用程序,但停留在这里。 I want to set Random Text to TextView and I did it successfully but I am getting same text on all textview I put it. 我想将“随机文本”设置为TextView并成功完成,但是我在放置的所有textview上都得到了相同的文本。 I want if the text set to one textview should not be show to other means its should not be repeat. 我想如果设置为一个textview的文本不应显示为其他意思,则不应重复显示。 Any help must be appreciated. 任何帮助都必须感谢。 I am posting some code here. 我在这里发布一些代码。

int[] array_set_up_text;
Random rand_set_up_text;

rand_set_up_text = new Random();
    array_set_up_text = new int[] { R.string.set_up_text1,  R.string.set_up_text2, R.string.set_up_text3,
            R.string.set_up_text4, R.string.set_up_text5, R.string.set_up_text6, R.string.set_up_text7,
             R.string.set_up_text8,R.string.set_up_text9,R.string.set_up_text10,R.string.set_up_text11,R.string.set_up_text12};
    rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);

text1.setText(array_set_up_text[rand_text_set_up]);
text2.setText(array_set_up_text[rand_text_set_up]);
text3.setText(array_set_up_text[rand_text_set_up]);

change your code to: 将您的代码更改为:

text1.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
text2.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);
text3.setText(array_set_up_text[rand_set_up_text.nextInt(array_set_up_text.length)]);

You need to recall the nextInt() method each time: 您每次都需要调用nextInt()方法:

text1.setText(array_set_up_text[rand_text_set_up]);
rand_set_up_text.nextInt()
text2.setText(array_set_up_text[rand_text_set_up]);
rand_set_up_text.nextInt()
text3.setText(array_set_up_text[rand_text_set_up]);

Your problem is this line 你的问题是这条线

 rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);

You get a random position once, then don't update it. 您一次获得一个随机位置,然后不更新它。

Easiest solution is to do something like below 最简单的解决方案是执行以下操作

rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text2.setText(array_set_up_text[rand_text_set_up]);

//Get a new random position.
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text3.setText(array_set_up_text[rand_text_set_up]);

//Get a new random position.
rand_text_set_up = rand_set_up_text.nextInt(array_set_up_text.length - 1);
text3.setText(array_set_up_text[rand_text_set_up]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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