安卓自定义文本组件

背景:在写串口时候发现需要显示时候显示日志,并且还能支持滚动,于是自己动手封装了一个LogTextView组件

下面直接上代码:

java 复制代码
public class LogTextView extends ScrollView {

    private TextView textView;
    private Handler mainHandler; // 用于在主线程中更新UI

    public LogTextView(Context context) {
        super(context);
        this.init(context);
    }

    public LogTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    private void init(Context context) {
        this.textView = new TextView(context);
        this.textView.setTextColor(Color.WHITE);
        this.setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        this.textView.setTextSize(14);
        this.textView.setPadding(16, 0, 16, 0);
        this.textView.setLineSpacing(1.5f, 1.5f); // 设置行间距
        this.textView.setSingleLine(false); // 允许多行显示
        this.textView.setMaxLines(Integer.MAX_VALUE); // 最大行数为无限
        this.textView.setEllipsize(null); // 不使用省略号
        this.textView.setHorizontallyScrolling(false);
        this.addView(this.textView);

        this.mainHandler = new Handler(Looper.getMainLooper());
    }

    public void appendLog(String log) {
        this.mainHandler.post(() -> {
            String currentText = this.textView.getText().toString();
            if (!currentText.isEmpty()) {
                this.textView.setText(currentText + "\n" + log);
            } else {
                this.textView.setText(log); // 如果为空,则直接设置
            }
            this.fullScroll(View.FOCUS_DOWN);
            this.invalidate(); // 强制刷新视图
        });
    }

    public void clearLogs() {
        this.textView.setText("");
    }

    public String getCurrentLogs() {
        return this.textView.getText().toString();
    }

    public void copyToClipboard(Context context) {
        String logContent = this.getCurrentLogs();
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Logs", logContent);
        clipboard.setPrimaryClip(clip);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}

用法:

xml布局里面

XML 复制代码
       <com.example.serialportupgrade.view.LogTextView
                android:id="@+id/tv_log"
                android:layout_gravity="top"
                android:layout_width="match_parent"
                android:layout_height="240dp" />

Activity或者Fragment里面调用

java 复制代码
public class MainActivity extends BaseActivity  {
    private LogTextView tv_log;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);

    this.tv_log = this.findViewById(R.id.tv_log);
    this.tv_log.setPadding(0, 0, 0, 0);
     this.tv_log.appendLog("欢迎使用\n");

    }
 }

效果图:

相关推荐
云知谷11 分钟前
【经典书籍】C++ Primer 第16章模板与泛型编程精华讲解
c语言·开发语言·c++·软件工程·团队开发
独自破碎E20 分钟前
LeetCode 381: O(1) 时间插入、删除和获取随机元素 - 允许重复
java·算法·leetcode
程语有云22 分钟前
生产事故-Caffeine缓存误用之临下班的救赎
java·缓存·caffeine·阻塞·log·生产事故
workflower34 分钟前
基本作业-管理⾃⼰的源代码
开发语言·单元测试·软件工程·需求分析·个人开发
froginwe1143 分钟前
Pandas DataFrame:深入理解数据分析的利器
开发语言
Miraitowa_cheems44 分钟前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先
onthewaying1 小时前
Android SurfaceTexture 深度解析
android·opengl
Jm_洋洋1 小时前
【Linux系统编程】程序替换:execve(execl、execlp、execle、execv、execvp、execvpe)
linux·运维·c语言·开发语言·程序人生
CodeCraft Studio1 小时前
国产化Word处理控件Spire.Doc教程:用Java实现TXT文本与Word互转的完整教程
java·c#·word·spire.doc·word文档转换·txt转word·word转txt
徐子童1 小时前
数据结构---优先级队列(堆)
java·数据结构·面试题·优先级队列··topk问题