package com.etgyd.textTest;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class Android_Test67_TextActivity extends Activity {
public TextView text;
public TextPaint paint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.text);
text.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
paint = text.getPaint();
float x, y;
String str = text.getText().toString().replace(",", " ").replace("-", " ");
String temp = " ";
x = event.getX();
y = event.getY();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == ' ') {
temp = temp + " " + i;
}
}
temp = temp + " " + str.length();
String[] nums = temp.trim().split(" ");// 获取空格位置
String subArg[] = str.split(" ");// 获取无空格的字符
if (subArg.length == 1) {
System.out.println(new String(subArg[0]));
} else {
for (int i = 1; i < subArg.length; i++) {
if (x <= (paint.measureText(str.substring(0, Integer.parseInt(nums[i]))))&& x > (paint.measureText(str.substring(0,Integer.parseInt(nums[i - 1]) + 1)))) {
System.out.println(new String(subArg[i]));
} else if (x <= (paint.measureText(str.substring(0,Integer.parseInt(nums[0]))))) {
System.out.println(new String(subArg[0]));
}
}
}
return false;//
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
demo.rar (23个子文件)
Android_Test67_Text
.project 855B
default.properties 362B
bin
com
etgyd
textTest
Android_Test67_TextActivity$1.class 3KB
R$string.class 427B
R$layout.class 394B
R$drawable.class 400B
R.class 531B
Android_Test67_TextActivity.class 1KB
R$attr.class 340B
R$id.class 382B
Android_Test67_Text.apk 15KB
resources.ap_ 11KB
classes.dex 4KB
proguard.cfg 1KB
AndroidManifest.xml 699B
src
com
etgyd
textTest
Android_Test67_TextActivity.java 2KB
res
values
strings.xml 197B
layout
main.xml 626B
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
.classpath 280B
gen
com
etgyd
textTest
R.java 733B
assets
共 23 条
- 1
资源评论
- XinYuan87192014-11-12很简单的实现方式,可以作为学习的材料,不过算法还是有问题
- 叁某长欺西瓜2014-02-14以为是图像分析那种取词。。原来是getText()
- jeffery_hu19832012-03-26是我理解错了。在找“获取任意界面上文字”方法的朋友就不要下了。 这个demo的原理如下:建一个TextView,然后增加OnTouch响应,user点击到这个TextView时,Ontouch中获得该控件上的字串。很有创意。
etgyd
- 粉丝: 1
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功