例题2-11
activity_main.xml代码
1 <?xml version="1.0" encoding="utf-8"?> 2 <GridLayout android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:columnCount="4" 5 android:rowCount="6" 6 xmlns:android="http://schemas.android.com/apk/res/android"> 7 <CheckBox android:id="@+id/c1" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:layout_columnSpan="4" 11 android:text="唱"> 12 </CheckBox> 13 <CheckBox android:id="@+id/c2" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_columnSpan="4" 17 android:text="跳"> 18 </CheckBox> 19 <CheckBox android:id="@+id/c3" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_columnSpan="4" 23 android:text="Rap"> 24 </CheckBox> 25 <CheckBox android:id="@+id/c4" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:layout_columnSpan="4" 29 android:text="篮球"> 30 </CheckBox> 31 <Button android:id="@+id/but1" 32 android:layout_width="wrap_content" 33 android:layout_height="wrap_content" 34 android:text="确认" 35 android:layout_columnSpan="4"/> 36 <TextView android:id="@+id/text1" 37 android:layout_height="wrap_content" 38 android:layout_width="wrap_content" 39 android:text="" /> 40 </GridLayout>
Mainactivity.java代码
1 package com.example.hello; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.CheckBox; 9 import android.widget.ProgressBar; 10 import android.widget.TextView; 11 12 public class MainActivity extends AppCompatActivity { 13 Button but1; 14 TextView textView; 15 CheckBox che1,che2,che3,che4; 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.activity_main); 20 textView = (TextView)findViewById(R.id.text1); 21 but1 = (Button)findViewById(R.id.but1); 22 che1 = (CheckBox)findViewById(R.id.c1); 23 che2 = (CheckBox)findViewById(R.id.c2); 24 che3 = (CheckBox)findViewById(R.id.c3); 25 che4 = (CheckBox)findViewById(R.id.c4); 26 but1.setOnClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View v) { 29 String s=""; 30 if(che1.isChecked()) 31 s=s+che1.getText()+" "; 32 if(che2.isChecked()) 33 s=s+che2.getText()+" "; 34 if(che3.isChecked()) 35 s=s+che3.getText()+" "; 36 if(che4.isChecked()) 37 s=s+che4.getText()+" "; 38 textView.setText("练习时长两年半的个人练习生“刘秀”的才艺:"+s); 39 } 40 }); 41 42 } 43 44 }