安卓自定义TextView实现自动滚动
xml文件代码
<com.mobile.APITest.ScrollEditText android:id="@+id/statusEditText" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_weight="1" android:gravity="top" android:scrollbarFadeDuration="1000" android:scrollbarStyle="insideOverlay" android:scrollbars="vertical" android:singleLine="false" android:textColor="@color/black" />
自定义TextView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | package com.mobile.APITest; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.text.method.ScrollingMovementMethod; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.mobile.pos.lib.Global.POSCharUtils; /** * Created by Administrator on 2016/11/23. */ @SuppressLint ( "AppCompatCustomView" ) public class ScrollEditText extends TextView { private static final String TAG = "ScrollEditText" ; private boolean pressFlag = false ; //判断手指是否按着屏幕,如果是就不需要滚动了。 public ScrollEditText(Context context) { super (context); } public ScrollEditText(Context context, AttributeSet attrs) { super (context, attrs); } public ScrollEditText(Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); } @Override public boolean dispatchTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { postDelayed(runnable, 2000 ); } if (event.getAction() == MotionEvent.ACTION_MOVE) { pressFlag = true ; } if (event.getAction() == MotionEvent.ACTION_DOWN) { pressFlag = true ; removeCallbacks(runnable); } POSCharUtils.showLogD(TAG, event.getAction() + "" ); return super .dispatchTouchEvent(event); } private Runnable runnable = new Runnable() { @Override public void run() { pressFlag = false ; } }; @Override protected void onFinishInflate() { super .onFinishInflate(); setOnLongClickListener( new OnLongClickListener() { @Override public boolean onLongClick(View v) { showDialog(); return false ; } }); setMovementMethod(ScrollingMovementMethod.getInstance()); } public void appendStr( final CharSequence text) { post( new Runnable() { @Override public void run() { ScrollEditText. super .append(text); POSCharUtils.showLogD(TAG, text.toString()); if (pressFlag) return ; int scrollAmount = getLayout().getLineTop(getLineCount()) - getHeight(); if (scrollAmount > 0 ) scrollTo( 0 , scrollAmount); else scrollTo( 0 , 0 ); } }); } public void setTextStr( final CharSequence text) { post( new Runnable() { @Override public void run() { ScrollEditText. super .setText(text); if (pressFlag) return ; int scrollAmount = getLayout().getLineTop(getLineCount()) - getHeight(); if (scrollAmount > 0 ) scrollTo( 0 , scrollAmount); else scrollTo( 0 , 0 ); } }); } private void scrollTop() { scrollTo( 0 , 0 ); } private void scrollBottom() { int scrollAmount = getLayout().getLineTop(getLineCount()) - getHeight(); if (scrollAmount > 0 ) scrollTo( 0 , scrollAmount); else scrollTo( 0 , 0 ); } //信息内容是简单地列表项 public void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder( this .getContext()); //定义复选框 builder.setItems( new String[]{ "顶部" , "底部" }, listener); builder.create().show(); } private DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0 : scrollTop(); break ; case 1 : scrollBottom(); break ; } } }; } |
长按ScrollEditText 可以跳转至view的底部或者顶部
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2016-10-11 mysql压缩包安装配置