好。 这是我的问题。
- 我需要的用户正则表达式来菲力了一切,除了信件,然后我需要一个$字标签内包住找到的词。 与此STR = str.replaceAll(图案, “$ 0”);. 现在我过滤了所有正确的元素(标点符号,数字等),但其包围每个字中的每一个字母的一个标签不字。 让我怎么使用正则表达式组中的字母词?
从“(标签打开)T(A接近)(标签打开)H(紧密)(标签打开)I(A接近)(标签开放)S(紧密)(标签打开)I(A接近)(标签开放)S(紧密)(开放标签)W(A接近)(标签开放)R(紧密)(标签开放)O(紧密)(标签打开)N(A接近)(标签开放)G(A接近)“;
至 :
“(标签打开)本(亲密)(开放标签)是(密切)(标签打开)右(亲密)”;
然后我让他们可以点击的,我需要抓住click事件,并获得点击的文字在屏幕上的位置,因为我想使用点击事件,使工具提示显示出来的只是点击下面的字。 谢谢您的帮助。
公共类MainActivity延伸活动{
public String text = "This is just a sentence to test you. 23 this is another number23!g?";
public TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = explode(text);
tv = (TextView) findViewById(R.id.tv1);
tv.setLinksClickable(true);
tv.setMovementMethod(LinkMovementMethod.getInstance());
Spanned article = Html.fromHtml(text, null, null);
setHTML(article);
}
public void setHTML(Spanned html) {
SpannableString message = new SpannableString(html.toString());
Object[] spans = html.getSpans(0, html.length(), Object.class);
for (Object span : spans) {
int start = html.getSpanStart(span);
int end = html.getSpanEnd(span);
int flags = html.getSpanFlags(span);
if (span instanceof URLSpan) {
URLSpan urlSpan = (URLSpan) span;
span = new CallbackSpan(urlSpan.getURL());
}
message.setSpan(span, start, end, flags);
}
tv.setText(message);
}
public String explode(String str){
String pattern = "([a-zA-Z])";
str = str.replaceAll(pattern, "<a href=\"$0\">$0</a>");
return str;
}
private final class CallbackSpan extends ClickableSpan {
private String m_data;
private String url_main;
public CallbackSpan(String url) {
m_data = url.substring(0);
url_main = url;
}
public void onClick(View view) {
TextView item = (TextView)findViewById(R.id.tv2);
item.setText(url_main + " was clicked.");
Log.d("item", url_main);
}
}
}
最新的代码,请参见链接形式github上
message.setSpan(span, start, end, flags);
您需要删除的起源跨度设置之前新跨越。 下面请看
ClickableSpan的onclick()不工作URLSpan?
编辑
您可以捕获任何跨度click事件的extends LinkMovementMethod
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;
import java.lang.ref.WeakReference;
public class LinkMovementMethodExt extends LinkMovementMethod {
public static final int LinkMovementMethod_Down = 1001;
public static final int LinkMovementMethod_Up = 2002;
private static LinkMovementMethod sInstance;
private Class mSpanClass = null;
private WeakReference<Handler> mWeakReference = null;
public static MovementMethod getInstance(Handler handler, Class spanClass) {
if (sInstance == null) {
sInstance = new LinkMovementMethodExt();
((LinkMovementMethodExt) sInstance).mWeakReference = new WeakReference<>(handler);
((LinkMovementMethodExt) sInstance).mSpanClass = spanClass;
}
return sInstance;
}
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
/**
* get you interest span
*/
Object[] spans = buffer.getSpans(off, off, mSpanClass);
if (spans.length != 0) {
if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer, buffer.getSpanStart(spans[0]), buffer.getSpanEnd(spans[0]));
MessageSpan obj = new MessageSpan();
obj.setObj(spans);
obj.setView(widget);
Handler handler = mWeakReference.get();
if (handler != null) {
Message message = handler.obtainMessage();
message.obj = obj;
message.what = LinkMovementMethod_Down;
message.sendToTarget();
return true;
}
return false;
} else if (action == MotionEvent.ACTION_UP) {
Handler handler = mWeakReference.get();
if (handler != null) {
MessageSpan obj = new MessageSpan();
obj.setView(widget);
Message message = handler.obtainMessage();
message.obj = obj;
message.what = LinkMovementMethod_Up;
message.sendToTarget();
return true;
}
return false;
}
}
}
return super.onTouchEvent(widget, buffer, event);
}
public boolean canSelectArbitrarily() {
return true;
}
public boolean onKeyUp(TextView widget, Spannable buffer, int keyCode,
KeyEvent event) {
return false;
}
textView.setMovementMethod(LinkMovementMethodExt.getInstance());
编辑“Android开发者”
这是更好的方式来增加处理器性能的LinkMovementMethodExt类。
您可以捕获所有跨区这些均作为Message对象。
剪断在的onTouchEvent方法的代码:
Message message = Handler.obtainMessage();
message.obj = buffer.getSpans(off, off, Spanned.class);//get all spanned
message.what = 111;//which value ,it is up to you
message.sendToTarget(); //send message to you target handler
可以预期的处理程序在跨越你处理程序类。 可它是处理灵活的方式。
希望能帮助你。
以上的TextView文本是<a href='/a'>aaaa</a>123456<a href='/b'>bbbb</b>7890
我明白你的要求:
点击'AAAA',你要得到它s href value '/a', click 'bbbb',get it
ŞHREF '/ B'; 不触发这是在网页浏览器中打开默认的动作。
如果我的理解是正确的,你可以这样做:
- 设置为LinkMovementMethod TextView的,等:
textview.setMovementMethod(LinkMovementMethodExt.getInstance(handler, URLSpan.class));
- 获取利益跨度,这里是URLSpan。
在您处理程序的handleMessage方法,你可以这样做:
private Handler handler = new Handler() { public void handleMessage(Message msg) { int what = msg.what; if (what == 100) { Object[] spans = (Object[])msg.obj; for (Object span : spans) { if (span instanceof URLSpan) { System.out.println(((URLSpan) span).getURL()); } } } }; };
下载演示代码
- MainActivity具有只要你喜欢,你可以分配的颜色值的颜色属性。
怎么做?
假设你要点击TextView的各个单词? 我din't了解将所有字母词在您的评论。
下面可以用来点击个人作品,并显示在祝酒。
public class MainActivity extends Activity {
TextView _tv;
String[] each;
SpannableString ss1;
Button b;
EditText et;
String s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.ed);
_tv = (TextView) findViewById( R.id.tv );
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
s=et.getText().toString();
_tv.setText("");
for(int i=0;i<s.length();i++)
{
each = s.split("\\s+");
}
for(int i=0;i<each.length;i++)
{
System.out.println("................"+each[i]);
ss1= new SpannableString(each[i]);
//StyleSpan boldSpan = new StyleSpan( Typeface.BOLD );
//spannable.setSpan( boldSpan, 41, 52, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
ss1.setSpan(new MyClickableSpan(each[i]), 0, ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
_tv.append(ss1);
_tv.append(" ");
}
_tv.setMovementMethod(LinkMovementMethod.getInstance());
}
});
}
class MyClickableSpan extends ClickableSpan{
String clicked;
public MyClickableSpan(String string) {
// TODO Auto-generated constructor stub
clicked =string;
}
//clickable span
public void onClick(View textView) {
//do something
Toast.makeText(MainActivity.this,clicked ,
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.BLACK);//set text color
ds.setUnderlineText(false); // set to false to remove underline
}
}
}