当前位置: 首页 > article >正文

安卓自定义文本组件

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

下面直接上代码:

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布局里面

       <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里面调用

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");

    }
 }

效果图:


http://www.kler.cn/a/369139.html

相关文章:

  • C++学习路线(二十七)
  • DO・PO・DTO・BO・AO・VO
  • java 提示 避免用Apache Beanutils进行属性的copy。
  • hudi编译安装,使用spark3的maven指令
  • R语言 | paletteer包:拥有2100多个调色板!
  • Golang Agent 可观测性的全面升级与新特性介绍
  • 网络搜索引擎Shodan(3)
  • Javascript数据结构——哈希表
  • 什么是排列树?
  • 【单片机运行的原理及应用方向】
  • c#获取目录下所有文件
  • 51单片机应用开发(进阶)---外部中断(按键+数码管显示0-F)
  • 名城优企游学活动走进思腾合力:解析人工智能先行者的数字化之路
  • 记一次Esxi掉盘处理使用命令
  • [0152].第3节:IDEA中工程与模块
  • Python金色流星雨
  • 部署RocketMQ, 其实很简单 (带图, 附启动命令)
  • 视频智能分析平台LiteAIServer摄像机视频分析软件下载检测裸土
  • 易基因:Nat Commun:ATAC-seq等揭示恒河猴大脑高分辨率解剖区域的转录组和开放染色质图谱
  • 装饰器模式的适用场景示例
  • Django+Vue全栈开发项目入门(一)
  • 可以为服务器配置动态IP吗?
  • Redis 单机、主从、哨兵和集群架构详解和搭建
  • 口碑最好的开放式耳机有哪些?开放式蓝牙耳机排行榜盘点!
  • 【MySQL】视图与用户管理——MySQL
  • 华为交换机堆叠