Use Case Analysis of Android CheckBox

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.