main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"/>
<CheckBox
android:id="@+id/seeBook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看书"
/>
<CheckBox
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="写字"
/>
<CheckBox
android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="敲代码"
/>
<CheckBox
android:id="@+id/seeBeauty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看美女"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认"
android:id="@+id/submit"
/>
</LinearLayout>
MainActivity .java:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
//定义复选框
private CheckBox seeBook, write, code , seeBeauty;
// 定义按钮
private Button button;
//定义一个集合存放用户选择了哪几个爱好
private List<String> result = new ArrayList<>();
//定义复选框点击事件对象
private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/**
* 第一个参数CompoundButton: 代表用户当前点击的复选框按钮
* 参数二 isChecked: 是否选中true,false
*
* **/
//选中
if(isChecked){
//把 选中的内容添加到集合
result.add((String) buttonView.getText());
} else {//否则 把内容删掉
result.remove((String) buttonView.getText());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取复选框对象
seeBook = findViewById(R.id.seeBook);
write = findViewById(R.id.write);
code = findViewById(R.id.code);
seeBeauty = findViewById(R.id.seeBeauty);
// 确定按钮
button = findViewById(R.id.submit);
//绑定点击监听事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//弹出框 选中的内容
Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show();
}
});
//绑定点击复选框监听事件
seeBook.setOnCheckedChangeListener(onCheckedChangeListener);
write.setOnCheckedChangeListener(onCheckedChangeListener);
code.setOnCheckedChangeListener(onCheckedChangeListener);
seeBeauty.setOnCheckedChangeListener(onCheckedChangeListener);
}
}