My android learning experience 13 and android learning experience 13

Source: Internet
Author: User

My android learning experience 13 and android learning experience 13

Use of the ToggleButton Control

The ToggleButton control can be identified by its name as a "Switch" control, that is, there are two buttons in different States.

There are three main special attributes:

Android: textOn = "on" ---- text displayed when the status is true
Android: textOff = "off" ---- text displayed when the status is false
Android: checked = "true" ---- identify status

The following is a simple example to illustrate how ToggleButton is used.

Two controls are required. One is ToggleButton and the other is TextView. The function is to display different texts in different statuses of ToggleButton (more complex operations can be implemented, please write as needed)

The layout file code is:

<ToggleButton
Android: id = "@ + id/toggleButton1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentTop = "true"
Android: textOn = "open"
Android: textOff = "off"
Android: checked = "true"/>
<! -- Android: checked = "true" attribute mainly displays whether the object is selected or not. If no data is written, it is deselected by default. -->

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/toggleButton1"
Android: layout_alignParentRight = "true"
Android: layout_below = "@ + id/toggleButton1"
Android: layout_marginTop = "31dp"
/>

 

The content in the source code file is as follows:

Private ToggleButton tgB;
Private TextView TV;

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
TgB = (ToggleButton) findViewById (R. id. toggleButton1 );
TV = (TextView) findViewById (R. id. textView1 );

TgB. setOnCheckedChangeListener (new OnCheckedChangeListener (){


Public void onCheckedChanged (CompoundButton buttonView, boolean isChecked ){
// Display different texts based on different statuses of the ToggleButton Control
If (isChecked ){
TV. setText ("On Now ");
TV. setTextSize (30f );
} Else {
TV. setText ("the status is off now ");
TV. setTextSize (30f );
}

}
});

}

Interface:

 

 

Please advise if something is wrong. Thank you.

 

Related Article

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.