Giter Club home page Giter Club logo

spannablehelper's Introduction

SpannableString使用记录

1.Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

2.Add the dependency

	dependencies {
	        compile 'com.github.ALguojian:SpannableHelper:1.0'
	}

用法:

  SpannableStringUtils spannableStringUtils = new SpannableStringUtils("飞流直下三千尺,疑是银河落九天");

          spannableStringUtils.setColor(Color.GREEN, 2, 6)
                  .setBackGround(ContextCompat.getColor(this,R.color.bbb), 4, 8)
                  .setBold(6, 10)
                  .setOnClick(9, 14, ContextCompat.getColor(this,R.color.aaa))
                  .setImage(this, R.mipmap.ic_launcher, 3, 6, 120, 160)
                  .setOnClickSpanListener(new SpannableStringUtils.OnClickSpanListener() {
                      @Override
                      public void OnClickSpanListener() {

                          Toast.makeText(MainActivity.this, "点我", Toast.LENGTH_SHORT).show();
                      }
                  });

          textView.setMovementMethod(LinkMovementMethod.getInstance());
          textView.setTextSize(20);
          textView.setText(spannableStringUtils);

简介:

spannableStringstring类似都是一种字符串类型,都可以用于textview设置为版本

setSpan((Object what, int start, int end, int flags))

  • what:设置格式
  • start:开始下标索引
  • end:结束下表索引
  • flags
    • Spanned.SPAN_INCLUSIVE_EXCLUSIVE从起始下标到终了下标,包括起始下标
    • Spanned.SPAN_INCLUSIVE_INCLUSIVE 从起始下标到终了下标,同时包括起始下标和终了下标
    • Spanned.SPAN_EXCLUSIVE_EXCLUSIVE 从起始下标到终了下标,但都不包括起始下标和终了下标
    • Spanned.SPAN_EXCLUSIVE_INCLUSIVE 从起始下标到终了下标,包括终了下标

设置监听事件

 SpannableString spannableString = new SpannableString(item.getContent());

    ClickableSpan span = new ClickableSpan() {

        @Override
        public void onClick(View widget) {

            ToastUtils.show(getActivity(), "haha");

        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(ds.linkColor);
            ds.setUnderlineText(false);
       }
    };
spannableString.setSpan(span, 2, item.getContent().length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
ForegroundColorSpan cspan = newForegroundColorSpan(Color.parseColor("#57caa1"));
spannableString.setSpan(cspan, 2, item.getContent().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = helper.getView(R.id.group_count);
textView.setText(spannableString);
//控制点击的时候的背景色
textView.setHighlightColor(Color.parseColor("#57caa1"));
textView.setMovementMethod(LinkMovementMethod.getInstance());

spannablehelper's People

Contributors

alguojian avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.