androidstudio四人中随机抽取两人
时间: 2024-02-18 08:05:48 浏览: 28
你可以使用以下代码随机抽取两个人:
```
import java.util.ArrayList;
import java.util.Collections;
// 创建一个列表来存储四个人
ArrayList<String> names = new ArrayList<>();
names.add("Person 1");
names.add("Person 2");
names.add("Person 3");
names.add("Person 4");
// 使用 Collections 类中的 shuffle 方法来打乱列表顺序
Collections.shuffle(names);
// 从打乱后的列表中取前两个人
String person1 = names.get(0);
String person2 = names.get(1);
```
这样就可以随机抽取两个人了。
相关问题
androidstudio用按钮四人中随机抽取两人
好的,你可以按照以下步骤在 Android Studio 中实现这个功能:
1. 在布局文件中添加一个按钮组件,例如:
```xml
<Button
android:id="@+id/randomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="随机抽取两人" />
```
2. 在 Java 代码中获取该按钮组件,并为其设置点击事件:
```java
Button randomButton = findViewById(R.id.randomButton);
randomButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 在这里编写随机抽取两人的代码
}
});
```
3. 在点击事件中编写随机抽取两人的代码,可以借鉴上一个问题中的代码。例如:
```java
ArrayList<String> names = new ArrayList<>();
names.add("Person 1");
names.add("Person 2");
names.add("Person 3");
names.add("Person 4");
Collections.shuffle(names);
String person1 = names.get(0);
String person2 = names.get(1);
// 在这里将 person1 和 person2 显示在界面上,例如:
TextView resultTextView = findViewById(R.id.resultTextView);
resultTextView.setText(person1 + " 和 " + person2 + " 被随机抽取了!");
```
这样就可以实现在点击按钮时随机抽取两个人并将结果显示在界面上的功能了。
androidstudio在线性布局中用按钮四人中随机抽取两人
可以使用以下步骤在 Android Studio 中实现:
1. 打开 Android Studio,创建一个新项目。
2. 在 `activity_main.xml` 中,使用线性布局(`LinearLayout`)添加四个按钮,代码如下:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="张三"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="李四"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="王五"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="赵六"/>
</LinearLayout>
```
3. 在 `MainActivity.java` 中,获取四个按钮的引用,并为其中一个按钮(比如 `btn1`)设置点击事件。在点击事件中,使用和上面相同的代码来实现随机抽取两个人的功能,并将结果展示在 `Toast` 中,代码如下:
```java
public class MainActivity extends AppCompatActivity {
private Button btn1, btn2, btn3, btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 定义四个人的名字
String[] names = {"张三", "李四", "王五", "赵六"};
// 生成两个随机数,表示选出的两个人的下标
Random random = new Random();
int index1 = random.nextInt(4);
int index2;
do {
index2 = random.nextInt(4);
} while (index2 == index1);
// 获取选出的两个人的名字
String selected1 = names[index1];
String selected2 = names[index2];
// 显示抽取结果
String message = "选中的两个人是:" + selected1 + " 和 " + selected2;
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
}
```
4. 运行程序,在点击 `btn1` 后就可以随机抽取两个人并将结果显示在 `Toast` 中了。
这样就可以在 Android Studio 中使用线性布局和按钮实现随机抽取两个人的功能了。