How can I make links in an EditText clickable?

前端 未结 4 873
暗喜
暗喜 2021-02-01 07:21

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 unde

相关标签:
4条回答
  • 2021-02-01 08:06

    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());
    
    0 讨论(0)
  • XML:-- Write it in your edittext

    android:autoLink="web" android:inputType="textWebEditText"

    0 讨论(0)
  • 2021-02-01 08:14

    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);
    
            }
        });
    
    0 讨论(0)
  • 2021-02-01 08:23

    Hope this will help someone

    val 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)
    
    0 讨论(0)
提交回复
热议问题