Andriod eclipse 复选框和单选按钮的使用,选择喜欢吃的水果,火龙果,草莓,芒果,西瓜,樱桃,java怎么写
时间: 2024-10-29 18:12:47 浏览: 46
在Android应用程序中,Eclipse是一个早期的IDE(集成开发环境),用于开发基于Java语言的Android应用。如果你想创建一个简单的界面,让用户选择他们喜欢的水果,你可以使用RadioButtons(单选按钮)和CheckBoxes(复选框)。下面是一个基本示例:
```java
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.CompoundButton;
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton dragonFruit, strawberry, mango, watermelon, cherry;
private Button submitBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
radioGroup = findViewById(R.id.radio_group_fruits);
dragonFruit = findViewById(R.id.dragon_fruit);
strawberry = findViewById(R.id.strawberry);
mango = findViewById(R.id.mango);
watermelon = findViewById(R.id.watermelon);
cherry = findViewById(R.id.cherry);
submitBtn = findViewById(R.id.submit_button);
// 创建字符串数组,用于Adapter填充选项
String[] fruits = {"火龙果", "草莓", "芒果", "西瓜", "樱桃"};
// 设置RadioButton和Spinner的数据源
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_checked, fruits);
radioGroup.setAdapter(adapter);
// 为RadioButton设置监听事件
dragonFruit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "选择了火龙果", Toast.LENGTH_SHORT).show();
}
}
});
// ... (类似地为其他水果按钮添加监听)
// 当提交按钮点击时,检查选中的水果
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId != -1) {
switch (selectedId) {
case R.id.dragon_fruit:
// 处理火龙果被选中
break;
// 其他水果的case...
}
} else {
Toast.makeText(MainActivity.this, "请选择一种水果", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
这个例子中,我们首先设置了RadioGroup,然后为每个水果创建了一个RadioButton,并通过ArrayAdapter将它们与列表项关联起来。当用户选择一个水果时,相应的OnCheckedChangeListener会被触发。最后,我们还绑定了一个提交按钮的监听器,在用户点击时检查并处理选中的水果。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)