安卓自定义文本组件

背景:在写串口时候发现需要显示时候显示日志,并且还能支持滚动,于是自己动手封装了一个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");

    }
 }

效果图:

相关推荐
Yeyu1 分钟前
你真的了解AIDL吗? 附:AIDL 与 Binder 交互全解析
android
啊森要自信16 分钟前
【GUI自动化测试】控件、鼠标键盘操作与多场景自动化
c语言·开发语言·python·adb·ipython
花北城24 分钟前
【C#】ABP框架服务端开发
开发语言·c#·abp
电商API_1800790524729 分钟前
Python 实现闲鱼商品列表批量采集,接口异常重试机制搭建
大数据·开发语言·数据库·爬虫·python
DogDaoDao31 分钟前
深入理解 Qt:从原理到实战的全景指南
开发语言·qt·程序员
摇滚侠36 分钟前
SpringMVC 入门到实战 视图解析器 44-48
java·spring·maven·intellij-idea
放下华子我只抽RuiKe536 分钟前
FastAPI 全栈后端(四):认证与授权
开发语言·前端·javascript·python·深度学习·react.js·fastapi
記億揺晃着的那天1 小时前
告别误操作!Spring Boot 多环境配置隔离与启动守卫实战
java·spring boot·后端·环境隔离
我是唐青枫1 小时前
Java Spring Data JPA 实战指南:Repository 查询、分页与实体映射
java·开发语言
张忠琳1 小时前
【Go 1.26.4】(Part 2) Go 1.26.4 超深度分析 — Runtime GMP 调度器 (proc.go + runtime2.go)
开发语言·golang