个人爱好
通过对CheckBox多选项的应用,构建一个个人爱好选择的小程序。显示效果如图,可实现多选项的应用。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hades.homework4.MainActivity">
<LinearLayout
android:layout_width="372dp"
android:layout_height="497dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="6dp"
tools:layout_editor_absoluteY="7dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="41dp"
android:text="你选择的是"
android:textSize="20dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="96dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="音乐"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="169dp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="美术" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="课程" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.hades.homework4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv;
Button bt, cls;
CheckBox cb1, cb2, cb3;
String status = "你选择的是:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView);
bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new click());
cls = (Button) findViewById(R.id.button2);
cls.setOnClickListener(new clear());
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
cb3 = (CheckBox) findViewById(R.id.checkBox3);
}
private class click implements View.OnClickListener {
@Override
public void onClick(View v) {
if (cb1.isChecked()) {
status += cb1.getText();
}
if (cb2.isChecked()) {
status += cb2.getText();
}
if (cb3.isChecked()) {
status += cb3.getText();
}
tv.setText(status);
}
}
private class clear implements View.OnClickListener {
@Override
public void onClick(View v) {
status = "你选择的是:";
cb1.setChecked(false);
cb2.setChecked(false);
cb3.setChecked(false);
tv.setText(status);
}
}
}