Android 在EditText中链接以检测哈希标记
我正在使用Android 在EditText中链接以检测哈希标记,android,android-edittext,hashtag,linkify,Android,Android Edittext,Hashtag,Linkify,我正在使用Linkify检测TextView中的hashtag,它工作正常,但我想在EditText控件中实现它 这是我在TextView中使用Linkify的方式: Pattern tagMatcher = Pattern.compile(("#([ء-يA-Za-z0-9_-]+)")); String newActivityURL = "content://com.hashtag.jojo/"; Pattern urlPattern = Patterns.WEB_URL; Transf
Linkify
检测TextView
中的hashtag,它工作正常,但我想在EditText
控件中实现它
这是我在TextView
中使用Linkify
的方式:
Pattern tagMatcher = Pattern.compile(("#([ء-يA-Za-z0-9_-]+)"));
String newActivityURL = "content://com.hashtag.jojo/";
Pattern urlPattern = Patterns.WEB_URL;
TransformFilter transformFilter = new TransformFilter() {
// skip the first character to filter out '@'
public String transformUrl(final Matcher match, String url) {
return match.group(0);
}
};
Linkify.addLinks(TextView, Linkify.ALL);
Linkify.addLinks(TextView, tagMatcher,newActivityURL, null,transformFilter);
如何将此应用于
EditText
?只需使用EditText而不是TextView即可,因为EditText是从TextView扩展而来的。工作一定很好
小例子:
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setText("http://http://www.dzone.com/");
Linkify.addLinks(editText1 , Linkify.WEB_URLS);
如果您想实时检查,只需执行以下操作:
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Linkify.addLinks(editText1 , Linkify.WEB_URLS);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
已更新
final TransformFilter filter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return match.group();
}
};
final Pattern hashtagPattern = Pattern.compile("#([ء-يA-Za-z0-9_-]+)");
final String hashtagScheme = "content://com.hashtag.jojo/";
final Pattern urlPattern = Patterns.WEB_URL;
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
System.out.println(count);
}
@Override
public void afterTextChanged(Editable s) {
Linkify.addLinks(s, hashtagPattern, hashtagScheme, null, filter);
Linkify.addLinks(s, urlPattern, null, null, filter);
}
});
你有什么问题吗?Linkify不接受EditText它只是用于TextView正如Gary111所说EditText是一个TextView你可以在任何地方使用它TextView可以在EditTextView中使用我想在EditText中写入时链接到apeer使用TextWatcher查看我的更新代码它在键入/写入链接时工作,但不适用于hashtag它给出了05-24 java.lang.StackOverflowerRone的问题是,如果标记修改了内容,PostTextChanged方法将再次被调用,使您处于无限循环中。@BrillPappin,我只是好奇,您能在gist或其他代码共享系统中发布一个示例代码吗。