作者:张晓和46872 | 来源:互联网 | 2023-08-23 14:09
效果图:
图1:
图2:
MainActivity.java
package ccv.turbosnail.xue_xi;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MainActivity extends AppCompatActivity {private CheckBox cbSeafood,cbFruits,cbCooler,cbMeat,cbVegetables; private TextView tvShow; private Button btnQueDing; private List<CheckBox> checkBoxList &#61; new ArrayList<>(); &#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView(); checkBoxList.add(cbMeat);checkBoxList.add(cbFruits);checkBoxList.add(cbVegetables);checkBoxList.add(cbSeafood);checkBoxList.add(cbCooler);btnQueDing.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View v) {StringBuffer sb &#61; new StringBuffer(); for (CheckBox checkbox : checkBoxList) { if (checkbox.isChecked()) { sb.append(checkbox.getText().toString() &#43; "\n\n"); }}tvShow.setText(sb);}});}private void initView() {cbCooler &#61; findViewById(R.id.cb_cooler);cbFruits &#61; findViewById(R.id.cb_fruits);cbMeat &#61; findViewById(R.id.cb_meat);cbSeafood &#61; findViewById(R.id.cb_seafood);cbVegetables &#61; findViewById(R.id.cb_vegetables);tvShow &#61; findViewById(R.id.tv_show);btnQueDing &#61; findViewById(R.id.btn_Queding);}}
activity_main.xml&#xff1a;
<?xml version&#61;"1.0" encoding&#61;"utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"xmlns:app&#61;"http://schemas.android.com/apk/res-auto"xmlns:tools&#61;"http://schemas.android.com/tools"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"tools:context&#61;".MainActivity"><TextViewandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:textSize&#61;"25dp"android:padding&#61;"20dp"android:text&#61;"你喜欢吃什么&#xff1f;"/><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_seafood"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"海鲜"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_fruits"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"水果"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_cooler"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"冷饮"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_meat"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"肉"/><CheckBoxandroid:id&#61;"&#64;&#43;id/cb_vegetables"android:layout_marginTop&#61;"30dp"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:layout_marginLeft&#61;"20dp"android:text&#61;"蔬菜"/></LinearLayout><Buttonandroid:id&#61;"&#64;&#43;id/btn_Queding"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:background&#61;"#0594fa"android:layout_marginTop&#61;"60dp"android:layout_gravity&#61;"center"android:textColor&#61;"#fff"android:textSize&#61;"18dp"android:text&#61;"确定"/><Viewandroid:layout_marginTop&#61;"20dp"android:layout_width&#61;"match_parent"android:layout_height&#61;"5dp"android:background&#61;"#fc0101"/><TextViewandroid:id&#61;"&#64;&#43;id/tv_show"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:layout_marginTop&#61;"20dp"android:textSize&#61;"20dp"/></LinearLayout>
CheckBox 属性&#xff1a;