SpannableString、ClickableSpan


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.clickablespan.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="22dp"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="22dp"
        android:paddingTop="10dp" >

        <CheckBox
            android:id="@+id/law_slector_tag"
            android:layout_width="17dp"
            android:layout_height="17dp"
            android:layout_marginTop="3dp"
            android:background="@drawable/selector_y"
            android:button="@null"
            android:checked="true" />

        <TextView
            android:id="@+id/law_info_tip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:focusable="false"
            android:lineSpacingExtra="3dp"
            android:padding="0dp"
            android:text="@string/law_info01"
            android:textColor="#828282"
            android:textSize="14dp" />
    </LinearLayout>

</RelativeLayout>

MainActivity

package com.example.clickablespan;


import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

	CheckBox lawCheckBox;
	TextView law_info_tip;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		lawCheckBox = (CheckBox) findViewById(R.id.law_slector_tag);
		law_info_tip = (TextView) findViewById(R.id.law_info_tip);
		
		/**
		 * Spanned.SPAN_EXCLUSIVE_EXCLUSIVE --- 不包含两端start和end所在的端点              (a,b)
			Spanned.SPAN_EXCLUSIVE_INCLUSIVE --- 不包含端start,但包含end所在的端点       (a,b]
			Spanned.SPAN_INCLUSIVE_EXCLUSIVE --- 包含两端start,但不包含end所在的端点   [a,b)
			Spanned.SPAN_INCLUSIVE_INCLUSIVE--- 包含两端start和end所在的端点                     [a,b]
		 */
		/**of Secure Service让其加大、加粗显示*/
		SpannableString ss = new SpannableString(getResources().getString(R.string.law_info03));
		ss.setSpan(new RelativeSizeSpan(1.2f), 3, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		ss.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.welcome_text_color)),
				3, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		/**点击后,可以弹出一个页面*/
		
		law_info_tip.setMovementMethod(LinkMovementMethod.getInstance());
		SpannableString lawTip = new SpannableString(getResources().getString(
				R.string.law_info02));
		lawTip.setSpan(new CustomClickableSpan(MainActivity.this), 0,
				lawTip.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

		law_info_tip.setText(R.string.law_info01);
		law_info_tip.append(lawTip);
		law_info_tip.append(ss);

		
		
		lawCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					lawCheckBox.setBackgroundResource(R.drawable.selector_y);
				} else {
					lawCheckBox.setBackgroundResource(R.drawable.selector_n);
				}
			}
		});
	}

}


CustomClickableSpan

package com.example.clickablespan;


import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;

public class CustomClickableSpan extends ClickableSpan {

	private Context mContext;
	@Override
	public void updateDrawState(TextPaint ds) {
		ds.setColor(mContext.getResources().getColor(R.color.color_blue_set));
	}

	@Override
	public void onClick(View widget) {
		widget.getContext().startActivity(
				new Intent(widget.getContext(), LawActivity.class));
	}

	public CustomClickableSpan(Context context) {
		super();
		mContext = context;
	}

}



activity_law.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.clickablespan.MainActivity" >

    <TextView
        android:text="xxx"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</RelativeLayout>

LawActivity

package com.example.clickablespan;

import android.app.Activity;
import android.os.Bundle;

public class LawActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_law);
	}

}


颜色
    <color name="color_blue_set">#008d9d</color>
    <color name="welcome_text_color">#212121</color>


字体
 <string name="law_info01">I agree with</string>
 <string name="law_info03">of Secure Service</string>
 <string name="law_info02">Terms and Conditions</string>


你可能感兴趣的:(SpannableString、ClickableSpan)