需求:在某些需求中,我们需要只显示一部分文字,点击之后,显示全部的文字,普通的TextView控件无法达到这种效果,我们需要继承TextView,并进行重写。
import android.content.Context; import android.content.res.TypedArray; import android.text.SpannableStringBuilder; import android.util.AttributeSet; import android.view.View; import android.widget.TextView; /** * User: Bazlur Rahman Rokon * Date: 9/7/13 - 3:33 AM */ public class ExpandableTextView extends TextView { private static final int DEFAULT_TRIM_LENGTH = 200; private static final String ELLIPSIS = "更多..."; private CharSequence originalText; private CharSequence trimmedText; private BufferType bufferType; private boolean trim = true; private int trimLength; public ExpandableTextView(Context context) { this(context, null); } public ExpandableTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView); this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH); typedArray.recycle(); setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setOnclick(); } }); } /** * 此方法用于打开和收起文字 */ public void setOnclick(){ trim = !trim; setText(); requestFocusFromTouch(); } private void setText() { super.setText(getDisplayableText(), bufferType); } private CharSequence getDisplayableText() { return trim ? trimmedText : originalText; } @Override public void setText(CharSequence text, BufferType type) { originalText = text; trimmedText = getTrimmedText(text); bufferType = type; setText(); } private CharSequence getTrimmedText(CharSequence text) { if (originalText != null && originalText.length() > trimLength) { return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS); } else { return originalText; } } public CharSequence getOriginalText() { return originalText; } public void setTrimLength(int trimLength) { this.trimLength = trimLength; trimmedText = getTrimmedText(originalText); setText(); } public int getTrimLength() { return trimLength; } }
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ExpandableTextView"> <attr name="trimLength" format="integer"/> </declare-styleable> </resources>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.rokonoid.widget.ExpandableTextView android:id="@+id/lorem_ipsum" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
import android.app.Activity; import android.os.Bundle; public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String yourText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + "Ut volutpat interdum interdum. Nulla laoreet lacus diam, vitae " + "sodales sapien commodo faucibus. Vestibulum et feugiat enim. Donec " + "semper mi et euismod tempor. Sed sodales eleifend mi id varius. Nam " + "et ornare enim, sit amet gravida sapien. Quisque gravida et enim vel " + "volutpat. Vivamus egestas ut felis a blandit. Vivamus fringilla " + "dignissim mollis. Maecenas imperdiet interdum hendrerit. Aliquam" + " dictum hendrerit ultrices. Ut vitae vestibulum dolor. Donec auctor ante" + " eget libero molestie porta. Nam tempor fringilla ultricies. Nam sem " + "lectus, feugiat eget ullamcorper vitae, ornare et sem. Fusce dapibus ipsum" + " sed laoreet suscipit. "; ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(R.id.lorem_ipsum); expandableTextView.setText(yourText); } }