如何在Android在使用TextView实现一个显示与隐藏全文功能

  介绍

如何在Android在使用TextView实现一个显示与隐藏全文功能吗?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

<强>参数定义

& lt; declare-styleablename=癈ollapsibleTextView"比;   & lt; attrname=皊uffixColor"格式=癱olor"/比;   & lt; attrname=癱ollapsedLines"格式=癷nteger"/比;   & lt; attrname=癱ollapsedText"格式=皊tring"/比;   & lt; attrname=癳xpandedText"格式=皊tring"/比;   & lt; attrname=皊uffixTrigger"格式=癰oolean"/比;   & lt;/declare-styleable>

<强>这几个参数分别表示

    <李>后缀颜色,也就是“显示全文”、“隐藏”这几个字的颜色李 <李>折叠后显示几行文字 <李>折叠后的后缀文字,也就是“显示全文”李 <>李展开后的后缀文字,也就是“隐藏” <李>隐藏与展示的触发事件是点击后缀还是整个TextView
    ,,<强>主要的构造函数如:
 publicCollapsibleTextView(上下文语境、AttributeSet attrs intdefStyleAttr) {
  超级(上下文、attrs defStyleAttr);
  TypedArray属性=context.getTheme ()
  .obtainStyledAttributes (attrs R.styleable。CollapsibleTextView defStyleAttr 0);
  
  mSuffixColor=attributes.getColor (R.styleable。CollapsibleTextView_suffixColor 0 xff0000ff);
  mCollapsedLines=attributes.getInt (R.styleable。CollapsibleTextView_collapsedLines, 1);
  mCollapsedText=attributes.getString (R.styleable.CollapsibleTextView_collapsedText);
  如果(TextUtils.isEmpty (mCollapsedText)) mCollapsedText=?显示All";
  mExpandedText=attributes.getString (R.styleable.CollapsibleTextView_expandedText);
  如果(TextUtils.isEmpty (mExpandedText)) mExpandedText=?Hide";
  mSuffixTrigger=attributes.getBoolean (R.styleable。CollapsibleTextView_suffixTrigger、假);
  
  这一点。多行文字=getText ()==null, # 63;空:getText () .toString ();
  setMovementMethod (LinkMovementMethod.getInstance ());
  super.setOnClickListener (mClickListener);
  }


为了能够监听后缀的点击事件,需要使用ClickableSpan

str.setSpan (mClickSpanListener,   note.length (),   note.length suffix.length () + (),   SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);//ClickableSpan   私人ClickableSpan mClickSpanListener=new ClickableSpan () {   @Override   publicvoidonClick(视图部件){   如果(mSuffixTrigger) {   mExpanded=! mExpanded;   applyState (mExpanded);   }   }      @Override   publicvoidupdateDrawState (TextPaint ds) {   super.updateDrawState (ds);   ds.setUnderlineText(假);   }   };


privatevoidapplyState (booleanexpanded) {   如果(TextUtils.isEmpty(多行文字))返回;      字符串注意=多行文字,后缀;   如果(扩大){   后缀=mExpandedText;   其他}{   如果(mCollapsedLines - 1 & lt;0) {   把新的RuntimeException (“CollapsedLines必须等于或大于1“);   }   int lineEnd=getLayout ()。getLineEnd (mCollapsedLines - 1);   后缀=mCollapsedText;   int newEnd=lineEnd - suffix.length () - 1;   int结束=newEnd比;0,# 63;newEnd: lineEnd;      TextPaint油漆=getPaint ();   int maxWidth=mCollapsedLines * (getMeasuredWidth ()——getPaddingLeft ()——getPaddingRight ());   而(paint.measureText(注意。substring(0) +后缀)比;maxWidth)   结束,   注意=注意。substring(0,结束);   }      最后SpannableString str=new SpannableString(注意+后缀);   如果(mSuffixTrigger) {   str.setSpan (mClickSpanListener   note.length (),   note.length suffix.length () + (),   SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);   }   str.setSpan(新ForegroundColorSpan (mSuffixColor),   note.length (),   note.length suffix.length () + (),   SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);   邮报》(新Runnable () {   @Override   publicvoidrun () {   setText (str);   }   });   }

其中油漆。measureText可以测量出文本布局的宽度从而得只文本行数并与mCollapsedLines比较裁剪出合适的字符长度并添加上后缀与跨度赋予TextView即可

由于getLineEnd等函数只有在布局过程之后值才有意义,所以要合理的选择applyState的时机:

@Override   protectedvoidonLayout (booleanchanged、intleft inttop、intright intbottom) {   超级。onLayout(改变,左,上,右,下);   如果(mShouldInitLayout,,getLineCount()比;mCollapsedLines) {   mShouldInitLayout=false;   applyState (mExpanded);   }   }

如何在Android在使用TextView实现一个显示与隐藏全文功能