Copy codeThe Code is as follows: public class MainActivity extends Activity {
TextView TV;
CheckBox cb1;
CheckBox cb2;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Cb1 = (CheckBox) findViewById (R. id. checkbox1 );
Cb2 = (CheckBox) findViewById (R. id. checkbox2 );
TV = (TextView) findViewById (R. id. textview1 );
Cb1.SetOnCheckedChangeListener(Cb );
Cb2.SetOnCheckedChangeListener(Cb );
}
Private CheckBox. OnCheckedChangeListener cb = new CheckBox. OnCheckedChangeListener (){
@ Override
Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
String stv = getString (R. string. hoby );
String scb1 = getString (R. string. basketball );
String scb2 = getString (R. string. football );
If (cb1.isChecked () & cb2.isChecked ()){
TV. setText (stv + ":" + scb1 + "&" + scb2 );
} Else if (cb1.isChecked ()&&! Cb2.isChecked ()){
TV. setText (stv + ":" + scb1 );
} Else if (! Cb1.isChecked () & cb2.isChecked ()){
TV. setText (stv + ":" + scb2 );
} Else {
TV. setText (stv );
}
}
};
The layout file is as follows:
Copy codeThe Code is as follows: <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/textview1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hoby"/>
<CheckBox
Android: id = "@ + id/checkbox1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/basketball"/>
<CheckBox
Android: id = "@ + id/checkbox2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/football"/>
</LinearLayout>