Basic Components of Android 10-textswitcher

Source: Internet
Author: User

Textswitcher is used to make the label on the screen produce an animated effect. When the settext () method is called, textswitcher disappears the current text content and displays the new text content in the form of an animation.

1. Common Methods
Setcurrenttext (charsequence text)
Set the text content of the currently displayed Text View (non-animated display)

Settext (charsequence text)
Set the text content of the next view and switch to the next view. You can exit the current text to display the next text.

2. Instance
The layout file XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <button Android: id = "@ ID/btnbasetextswitcher" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Click variable Text" Android: hint = "convert text in animated form"/> <textswitcher Android: Id = "@ ID/tsbase" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>

The Code is as follows:

Import Java. util. random; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. animation. animation; import android. view. animation. animationutils; import android. widget. button; import android. widget. textswitcher; import android. widget. textview; import android. widget. viewswitcher; import CN. youtous. r; public class basetextswitcher extends activity {private button btnbase; private textswitcher tsbase; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. base_textswitcher); btnbase = (button) findviewbyid (R. id. btnbasetextswitcher); btnbase. setonclicklistener (New basetextswitcherclicklistener (); tsbase = (textswitcher) findviewbyid (R. id. tsbase); tsbase. setfactory (New basetextswitcherfactory (); setupanimation ();}/*** set the animation format */private void setupanimation () {animation in = animationutils. loadanimation (this, android. r. anim. slide_in_left); In. setduration (2000); // to highlight the animation effect, we set the animation event to 2 seconds for animation out = animationutils. loadanimation (this, android. r. anim. slide_out_right); out. setduration (2000); tsbase. setinanimation (in); tsbase. setoutanimation (out);}/*** viewfactory */class basetextswitcherfactory implements viewswitcher. viewfactory {@ overridepublic view makeview () {textview T = new textview (basetextswitcher. this); T. setgravity (gravity. top | gravity. center_horizontal); T. settextsize (36); Return t ;}/ *** Click Event listener */class basetextswitcherclicklistener implements view. onclicklistener {@ overridepublic void onclick (view v) {random Rand = new random (); tsbase. settext (string. valueof (Rand. nextint ()));}}}

We can note that if you only set viewfactory for textswitcher and do not set the animation, textswitcher simply replaces the text.

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.