How can I make links in an EditText clickable?

为君一笑 提交于 2019-12-03 08:17:41

问题


I have an EditText on Android I'd for which I'd like any embedded urls to be clickable. I used the Linkify class, which has turned them blue and underlined them. However, I can't figure out how to actually make them clickable.

Thanks!


回答1:


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());



回答2:


For edit text I managed to get links clickable on the following way. First i implemented a Custom MovementMethod as describe here

Java

(Create your edit text from xml or context)

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);

Then to manage that the urls look like links while the user types

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);

        }
    });


来源:https://stackoverflow.com/questions/18219568/how-can-i-make-links-in-an-edittext-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!