Android - 如何生成随机数并在TextView上设置这些数字

时间:2017-07-17 12:15:09

标签: android

我有一个按钮和五个TextView。单击时,我想生成1到70之间的随机数,并将结果显示到TextViews中。 TextViews中的数字不应该相同。

目前我得到的代码只能在一个TextView中生成一个数字。

{{1}}

6 个答案:

答案 0 :(得分:1)

如果数字不在该列表中,您可以使用List并为其添加生成的随机数

    List<Integer> myNumbers = new ArrayList<>();
    Random rand = new Random();
    while (true) {
        int number = rand.nextInt(69) + 1;
        if (!myNumbers.contains(number)) {
            myNumbers.add(number);

            if (myNumbers.size() == 5)
                break;
        }
    }

    text1.setText(myNumbers.get(0));
    text2.setText(myNumbers.get(1));
    text3.setText(myNumbers.get(2));
    text4.setText(myNumbers.get(3));
    text5.setText(myNumbers.get(4));

答案 1 :(得分:0)

这将给出1到70之间的数字

Random r = new Random();
    int lowRange = 1;
    int highRange = 70;
    int result = r.nextInt(highRange -lowRange ) + lowRange ;

答案 2 :(得分:0)

您可以使用Arraylist来实现此目的。试试这样的事情

ArrayList<Integer> number = new ArrayList<Integer>();
for (int i = 1; i <= 10; ++i) number.add(i);
Collections.shuffle(number);

答案 3 :(得分:0)

尝试这种方式:

TextView text1, text2, text3, text4, text5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);

    text1 = (TextView)findViewById(R.id.textView1);
    text2 = (TextView)findViewById(R.id.textView2);
    text3 = (TextView)findViewById(R.id.textView3);
    text4 = (TextView)findViewById(R.id.textView4);
    text5 = (TextView)findViewById(R.id.textView5);

    yourClickEventHere
}

public void generate(View view){
    for(int i = 0; i< 5 ; i++){
        Random rand = new Random();
        int number =  rand.nextInt(69)+1;
        switch i{
            case 0:
                String myString = String.valueOf(number);
                text1.setText(myString);
                break;
            case 1:
                String myString = String.valueOf(number);
                text2.setText(myString);
                break;
            case 2:
                String myString = String.valueOf(number);
                text3.setText(myString);
                break;
            case 3:
                String myString = String.valueOf(number);
                text4.setText(myString);
                break;
            case 4:
                String myString = String.valueOf(number);
                text5.setText(myString);
                break;
        }
    }
}

希望它有所帮助。

答案 4 :(得分:0)

尝试使用此方法获取唯一数字

private HashSet<Integer> getUniqueNumbers(){
        HashSet<Integer> numbers = new HashSet<>(5);
        while (numbers.size() < 5) {
            int number = new Random().nextInt(69) + 1;
            numbers.add(number);
        }
        return numbers;
    }

更通用的方式:

private HashSet<Integer> getUniqueNumbers(int range, int size){
        HashSet<Integer> numbers = new HashSet<>(size);
        while (numbers.size() < size) {
            int number = new Random().nextInt(range-1) + 1;
            numbers.add(number);
        }
        return numbers;
    }

呼叫:

getUniqueNumbers(70, 5);

答案 5 :(得分:0)

请尝试这种方式。我相信这就是你想要的。

ublic class RandomActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int viewCount = 5;

        LinearLayout root = new LinearLayout(this);
        root.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < viewCount; i++) {
            TextView v = generate();
            root.addView(v);
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
            params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT;
            params.gravity = Gravity.CENTER_HORIZONTAL;
            v.setLayoutParams(params);
        }
        setContentView(root);
    }

    static List<Integer> numbers = new ArrayList<>();

    static {
        for (int i = 1; i < 70; i++) {
            numbers.add(i);
        }
    }


    public TextView generate() {
        Random rand = new Random();
        int number = numbers.remove(rand.nextInt(numbers.size()));

        TextView myText = new TextView(this);
        String myString = String.valueOf(number);
        myText.setText(myString);
        return myText;
    }
}