Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android textView 折疊 展開 ExpandableTextView

android textView 折疊 展開 ExpandableTextView

編輯:關於Android編程

項目過程中可能會用到可以折疊和展開的TextView , 這裡給出一種實現思路,自定義控件。

package com.example.expandtextviewdemo;

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;

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) {
                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;
    }
}

需要在你的attr.xml 中添加以下內容




    
        
    


然後在你的布局文件中引用如下代碼:



    


最後 , 在你的acitivty中測試一下


package com.example.expandtextviewdemo;

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

public class ExpandableTextActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expandable_text);
		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);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.expandable_text, menu);
		return true;
	}

}



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved