Usage of TextView in android

Source: Internet
Author: User

This article describes TextView4 common usage:

1. How to display text (URL, text of different sizes, fonts, and colors)

Activity_main.xml

 
  
  
 


MainActivity. java

Package com. example. android_textview; import android. OS. bundle; import android. app. activity; import android. text. html; import android. text. method. linkMovementMethod; import android. widget. textView; public class MainActivity extends Activity {private TextView textView1, textView2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView1 = (TextView) findViewById (R. id. textview1); textView2 = (TextView) findViewById (R. id. textview2); // textview1 String html = "I love android
"; Html + ="I love android

"; Html + ="Baidu"; CharSequence charSequence = Html. fromHtml (html); textView1.setText (charSequence); textView1.setMovementMethod (LinkMovementMethod. getInstance (); // a hyperlink is generated when you click. // Textview2 String text = "\ n my URL: http://www.sina.com \ n"; text + = "my mailbox: 123@163.com \ n"; text + = "my phone: 13956789918 \ n "; textView2.setText (text); textView2.setMovementMethod (LinkMovementMethod. getInstance ());}}



2. Display emoticon images and text in TextView

Image directory:


Activity_main.xml

 
  
 


MainActivity. java

Package com. android. mytextview; import java. lang. reflect. field; import android. OS. bundle; import android. app. activity; import android. graphics. color; import android. graphics. drawable. drawable; import android. text. html; import android. text. html. imageGetter; import android. text. method. linkMovementMethod; import android. view. menu; import android. widget. textView; public class MainActivity extends Activity {private TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById (R. id. textview); // textview4 textView. setTextColor (Color. BLACK); textView. setBackgroundColor (Color. WHITE); textView. setTextSize (20); String html2 = "Image 1 Image 2 Image 3

"; Html2 + =" Image 4 image 5 "; CharSequence charSequence2 = Html. fromHtml (html2, new ImageGetter () {public Drawable getDrawable (String source) {// obtain information about system resources, such as Drawable drawable = getResources (). getDrawable (getResourceId (source); // compress the third image by 50% if (source. equals ("http://www.2cto.com/uploadfile/Collfiles/20140419/2014041908232829. ") {drawable. setBounds (0, 0, drawable. getIntrinsicWidth ()/2, drawable. getIntrinsic Height ()/2);} else {drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight ();} return drawable ;}}, null); textView. setText (charSequence2); textView. setMovementMethod (LinkMovementMethod. getInstance ();} public int getResourceId (String name) {try {// obtain the Field object based on the variable name of the resource ID, which is implemented using the java reflection boarding system. Field field = R. drawable. class. getField (name); // obtains and returns the value of the Field (static variable) of the resource id, using the reflection mechanism. Return Integer. parseInt (field. get (null ). toString ();} catch (Exception e) {// TODO: handle finished tione. printStackTrace () ;}return 0 ;}}

Note: Chapter 3 is compressed according to the proportion of 50%, and Chapter 4 is a hyperlink.

3. Click the link to bring up the Activity

Click "show Activity1"

Click "show Activity2"

Activity_main.xml

 
  
  
 


MainActivity. java

Package com. android. mytextview; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. text. spannableString; import android. text. spanned; import android. text. method. linkMovementMethod; import android. text. style. clickableSpan; import android. view. view; import android. widget. textView; public class MainActivity extends Activity {private TextView textView1, textView2; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView1 = (TextView) findViewById (R. id. textview1); textView2 = (TextView) findViewById (R. id. textview2); String text1 = "show Activity1"; String text2 = "show Activity2"; SpannableString s1 = new SpannableString (text1); SpannableString s2 = new SpannableString (text2 ); s1.setSpan (new ClickableSpan () {@ Overridepublic void onClick (View widget) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. this, Activity1.class); startActivity (intent) ;}}, 0, text1.length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); s2.setSpan (new ClickableSpan () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. this, Activity2.class); startActivity (intent) ;}}, 0, text2.length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); textView1.setText (s1); textView2.setText (s2); textView1.setMovementMethod (LinkMovementMethod. getInstance (); textView2.setMovementMethod (LinkMovementMethod. getInstance ());}}

Activity1.java

package com.android.mytextview;import android.app.Activity;import android.os.Bundle;public class Activity1 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setTitle("Activity1");}}


Activity2.java

package com.android.mytextview;import android.app.Activity;import android.os.Bundle;public class Activity2 extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTitle("Activity2");}}


4. Use TextView to achieve the effect of a lawn lamp

Activity_main.xml

 
  
 


MainActivity. java

Package com. example. android_textview; import android. OS. bundle; import android. app. activity; import android. text. html; import android. text. method. linkMovementMethod; import android. widget. textView; public class MainActivity extends Activity {private TextView textView1; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ; TextView1 = (TextView) findViewById (R. id. textview1); // textview1 String html = "on the evening of June 23, April 17, Sina Weibo finally successfully logged on to the Nasdaq exchange and issued 16.8 million shares. The final issue was priced at $17, at the lower limit of the previously proposed release price range. At this price, Sina Weibo will raise $0.2856 billion, lower than expected. "; CharSequence charSequence = Html. fromHtml (html); textView1.setText (charSequence); textView1.setMovementMethod (LinkMovementMethod. getInstance (); // a hyperlink is generated when you click. }}

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.