我在Android上有一个
EditText
,希望其中嵌入的URL可被点击。我使用了Linkify
类,已经将它们变为蓝色并加下划线。但是我无法弄清楚如何使它们可点击。
谢谢!EditText
,希望其中嵌入的URL可被点击。我使用了Linkify
类,已经将它们变为蓝色并加下划线。但是我无法弄清楚如何使它们可点击。
谢谢!XML:
android:linksClickable="true"
android:autoLink="web|email"
JAVA:
TextView textView = (TextView) findViewById(R.id.textViewId);
textView.setText(Html.fromHtml(html));
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(html));
吗? - Kamil LelonekLinkify.addLinks(textView, Linkify.ALL);
。 - Kamil LeloneksetMovementMethod()
调用。 - lowellkJava
(从xml或上下文中创建您的EditText)
editText.setLinksClickable(true);
editText.setAutoLinkMask(Linkify.WEB_URLS);
editText.setMovementMethod(CustomMovementMethod.getInstance());
//If the edit text contains previous text with potential links
Linkify.addLinks(editText, Linkify.WEB_URLS);
接着,为了在用户输入时使URL看起来像链接
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Linkify.addLinks(s, Linkify.WEB_URLS);
}
});
setLinksClickable(false);
。 - Cheok Yan Chengval message: String = String.format(
getString(R.string.message_content),
firstNameEditText.text,
lastNameEditText.text,
dateTextView.text,
timeTextView.text
)
val gMapURL = getString(R.string.google_map_location)
// Setup my Spannable with clickable URLs
val spannable: Spannable = SpannableString(gMapURL)
Linkify.addLinks(spannable, Linkify.WEB_URLS)
// Append a zero-width space to the Spannable
val gText = TextUtils.concat(spannable, "\u200B")
val finalText = TextUtils.concat(message, gText)
messageContentEditText.setText(finalText)
android:autoLink="web"
android:inputType="textWebEditText"
code
,即用反引号括起来;(2) 详细阐述你的 XML 和 Java/Kotlin 代码之间的联系以及实际使其工作的步骤;(3) 解释你的方法如何改进 Kamil's answer,该回答发布于2013年。 - Valentin Kuhn