我有一个radioGroup和四个单选按钮,我只是想在程序运行时随机安排这些单选按钮。如何在Android中随机排列RadioButtons?
说我有单选按钮r1,r2,r3,r4和一个radiogroup。 我希望有时这些按照r2,r3,r1,r4(垂直)排列的单选按钮以及某些时候r1,r2,r4,r3等...
我该如何实现这一点?
我有一个radioGroup和四个单选按钮,我只是想在程序运行时随机安排这些单选按钮。如何在Android中随机排列RadioButtons?
说我有单选按钮r1,r2,r3,r4和一个radiogroup。 我希望有时这些按照r2,r3,r1,r4(垂直)排列的单选按钮以及某些时候r1,r2,r4,r3等...
我该如何实现这一点?
为了实现随机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]);
}
}
谢谢它的作品!你能解释随机交换部分吗? –
欢迎您,我在这篇文章中解释了这个过程https://stackoverflow.com/a/46255228/6195457 –
编写一个实现随机函数算法的函数(您可以编写自己的函数或在互联网上找到一个函数 - 它们非常常见和简单)。
然后,使用addView()以编程方式将RadioButton
添加到RadioGroup
来呈现它们。使用随机函数的结果来确定addView()函数的“index”参数。
您的随机函数将确保每次单选按钮被渲染时,它们的顺序都是随机的。
UPDATE
为了让事情更清晰,假设你的随机函数返回顺序如下: -
现在,你需要做的是“索引”参数依次调用addView()
四次,依次占据上述值。
例如: -
addView(radioButton2);
addView(radioButton1);
addView(radioButton4);
addView(radioButton3);
谢谢!所以参数我必须有一个radioButtons数组?我对吗 ? –
恩,我不认为你可以有一个数组作为参数。无论如何,你不需要数组。让我更新我的答案,让它更清晰! –
好的再次感谢! –
解释后的随机交换实现部分:https://stackoverflow.com/a/46252387/6195457
//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
}
感谢您的明确解释,我明白了! –
你必须以编程方式创建广播组,并将其添加到您的XML父。因此每次你可以改变他们的位置(随机)。 –
所有我的小工具以编程方式创建(单选按钮和组),所以我正在寻找一种方法来自动实现它(也许一个函数或方法)@PratikTank –