日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android单击、长按获取当前触点坐标下(TextView)文字字符

發(fā)布時(shí)間:2023/12/4 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android单击、长按获取当前触点坐标下(TextView)文字字符 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package com.*.*.*.utils;import android.graphics.Rect; import android.text.Layout; import android.widget.TextView;public class TextViewUtils {/**獲取TextView某一個(gè)字符的坐標(biāo)位置@return 返回的是相對(duì)坐標(biāo)@parms tv@parms index 字符索引*/public static Rect getTextViewSelectionRect(TextView tv, int index) {Layout layout = tv.getLayout();Rect bound = new Rect();int line = layout.getLineForOffset(index);layout.getLineBounds(line, bound);int yAxisBottom = bound.bottom;//字符底部y坐標(biāo)int yAxisTop = bound.top;//字符頂部y坐標(biāo)int xAxisLeft = (int) layout.getPrimaryHorizontal(index);//字符左邊x坐標(biāo)int xAxisRight = (int) layout.getSecondaryHorizontal(index);//字符右邊x坐標(biāo)//xAxisRight 位置獲取后發(fā)現(xiàn)與字符左邊x坐標(biāo)相等,如知道原因請(qǐng)告之。暫時(shí)加上字符寬度應(yīng)對(duì)。if (xAxisLeft == xAxisRight){String s = tv.getText().toString().substring(index, index + 1);//當(dāng)前字符xAxisRight = xAxisRight + (int) tv.getPaint().measureText(s);//加上字符寬度}int tvTop=tv.getScrollY();//tv絕對(duì)位置return new Rect(xAxisLeft, yAxisTop+ tvTop, xAxisRight, yAxisBottom+tvTop );}/**獲取TextView觸點(diǎn)坐標(biāo)下的字符@param tv tv@param x 觸點(diǎn)x坐標(biāo)@param y 觸點(diǎn)y坐標(biāo)@return 當(dāng)前字符*/public static String getTextViewSelectionByTouch(TextView tv, int x, int y) {String s = "";for (int i = 0; i < tv.getText().length(); i++){Rect rect = getTextViewSelectionRect(tv, i);if (x < rect.right && x > rect.left && y < rect.bottom && y > rect.top){s = tv.getText().toString().substring(i, i + 1);//當(dāng)前字符break;}}return s;} }

在?點(diǎn)擊事件中通過獲取觸點(diǎn)坐標(biāo)后調(diào)用 TextViewUtils.getTextViewSelectionByTouch(tv, x, y)?即可獲取當(dāng)前字符。

總結(jié)

以上是生活随笔為你收集整理的Android单击、长按获取当前触点坐标下(TextView)文字字符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。