如何在 android 中随机排列 RadioButtons?

How to Arrange RadioButtons Randomly in android?

我有一个radioGroup和四个radiobutton,我只想在节目播放时随机排列这些radiobuttons 运行。

假设我有单选按钮 r1、r2、r3、r4 和一个单选按钮。 我有时希望这些单选按钮排列成 r2、r3、r1、r4(垂直),有时是 r1、r2、r4、r3 等等...

我该如何实现?

编写一个实现随机函数算法的函数(您可以自己编写或在 Internet 上找到一个 - 它们非常常见且简单)。

然后,使用 addView() 以编程方式将 RadioButton 添加到 RadioGroup 以呈现它们。使用随机函数的结果来确定 addView() 函数的 "index" 参数。

您的随机函数将确保每次呈现单选按钮时,它们的顺序保持随机。

更新

为了让事情更清楚,假设你的随机函数 returns 顺序如下:-

2 1个 4个 3

现在,你需要做的是调用addView()四次,"index"参数依次取上面的值。

例如:-

addView(radioButton2);
addView(radioButton1);
addView(radioButton4);
addView(radioButton3);

为了实现随机 RadioButton 随机序列,以下代码将帮助您:

在您的 layout.xml 文件中添加 RadioGroup:

 <RadioGroup
        android:id="@+id/gul_radio_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

并且在您的 java 文件中:

final int NUMBER_OF_RADIOBUTTONS_TO_ADD = 4;//Change it for other number of RadioButtons
    RadioButton[] radioButton;
    RadioGroup radioGroup;


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

        radioGroup = (RadioGroup) findViewById(R.id.gul_radio_group);

        //Initializing the RadioButtons
        radioButton = new RadioButton[NUMBER_OF_RADIOBUTTONS_TO_ADD];
        for (int i = 0; i < NUMBER_OF_RADIOBUTTONS_TO_ADD; i++) {
            radioButton[i] = new RadioButton(this);

            //Text can be loaded here
            radioButton[i].setText("Button " + (i + 1));
        }

        //Random Swapping
        for (int i = 0; i < 4; i++) {//this loop is randomly changing values 4 times
            int swap_ind1 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);
            int swap_ind2 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);
            RadioButton temp = radioButton[swap_ind1];
            radioButton[swap_ind1] = radioButton[swap_ind2];
            radioButton[swap_ind2] = temp;
        }
        radioButton[0].setChecked(true);//This will make the top RadioButton selected by default


        //Adding RadioButtons in RadioGroup
        for (int i = 0; i < NUMBER_OF_RADIOBUTTONS_TO_ADD; i++) {
            radioGroup.addView(radioButton[i]);
        }
    }

post 随机交换部分的解释:

  //Random Swapping
        //This loop iterates the explained process 4 times which is inside it
        for (int i = 0; i < 4; i++) {

             /*0,1,2,3 can be output for ((int) (Math.random() * 10) % 4)
              *Math.random returns a number in between 0 to 1 
              *we multiply it with 10 to make the number from 0 to 10
              *Taking %4 will return a number from 0 to 3*/ 
            int swap_ind1 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);

            //Getting second random index as above
            int swap_ind2 = ((int) (Math.random() * 10) % NUMBER_OF_RADIOBUTTONS_TO_ADD);

            //Swapping the RadioButtons at Random indexes above
            RadioButton temp = radioButton[swap_ind1];//storing value of first index in temp
            radioButton[swap_ind1] = radioButton[swap_ind2];//Putting second value at first index
            radioButton[swap_ind2] = temp;// putting first value at second index from temp
        }