Hypertext: Java Source Code (Cont.)


The following commands are used to set a hyperlink style to the TextView object.
import android.text.SpannableStringBuilder;
This is the class for text whose content and markup can both be changed.

import android.text.Spanned;
This is the interface for text that has markup objects attached to ranges of it.

import android.text.style.URLSpan;
It is implementation of the ClickableSpan that allows setting a URL string.

SpannableStringBuilder class
This is the class for text whose content and markup can both be changed.

ssb.append( tv.getText( ) );
Appends the character sequence text and spans flag over the appended part.

ssb.setSpan( new URLSpan("#"), 0, ssb.length( ),
      Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
Mark the specified range of text with the specified object. The flags determine how the span will behave when text is inserted at the start or end of the span’s range.

tv.setText( ssb, TextView.BufferType.SPANNABLE );
Sets the text that this TextView is to display and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.
Next Page



Home Page

MyApplication/app/src/main/java/com/example/wenchen/myapplication/NextActivity.java
package com.example.wenchen.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.URLSpan;

public class NextActivity extends AppCompatActivity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_next );
    TextView tv = (TextView) findViewById( R.id.tvView );
    MainActivity.makeTextViewHyperlink( tv );
    tv.setOnClickListener( new View.OnClickListener( ) {
      @Override
      public void onClick( View v ) {
        Intent intent = new Intent( NextActivity.this, MainActivity.class );
        startActivity( intent );
      }
    } );
  }

  // Sets a hyperlink style to the textview.
  public static void makeTextViewHyperlink( TextView tv ) {
    SpannableStringBuilder ssb = new SpannableStringBuilder( );
    ssb.append( tv.getText( ) );
    ssb.setSpan( new URLSpan("#"), 0, ssb.length( ),
       Spanned.SPAN_EXCLUSIVE_EXCLUSIVE );
    tv.setText( ssb, TextView.BufferType.SPANNABLE );
  }
}




      Why do they lock gas station bathrooms?    
      Are they afraid someone will clean them?    
      – George Carlin