在程序界面建立三项复选框CheckBox组件,分别表示三门课程,建立一个Button组件和一个TextView组件。要求复选后点击按钮后,在TextView组件中显示“我喜欢:xxx,xxx”
时间: 2024-03-11 07:49:21 浏览: 24
好的,我明白您的要求了。您可以使用以下代码实现:
1. 在您的布局文件中添加三个CheckBox组件、一个Button组件和一个TextView组件:
```xml
<CheckBox
android:id="@+id/checkBox1"
android:text="课程1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkBox2"
android:text="课程2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkBox3"
android:text="课程3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button"
android:text="确定"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我喜欢:" />
```
2. 在您的Activity中,找到这些组件并为Button组件设置点击事件:
```java
public class MainActivity extends AppCompatActivity {
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "我喜欢:";
if (checkBox1.isChecked()) {
text += "课程1,";
}
if (checkBox2.isChecked()) {
text += "课程2,";
}
if (checkBox3.isChecked()) {
text += "课程3,";
}
textView.setText(text);
}
});
}
}
```
3. 运行您的应用程序,并选择一个或多个复选框,然后点击按钮,就会在TextView组件中显示您喜欢的课程列表。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)