EditText中的多行文字,带有可点击和可编辑的链接

时间:2020-10-05 12:06:01

标签: android android-edittext linkmovementmethod

我正在使用多行EditText视图,该视图可以在输入中包含webUrl。为此,我正在使用LinkMovementMethod使EditText中的链接可点击。

我有与问题EditTexts with links both clickable and editable相同的问题,但是我有多行文字。

问题在于:

如果字符串的最后一部分是链接,则单击任意位置都会导致链接打开。

扩展名:

当我在EditText的当前区域中单击时,它将是可编辑的。 https://i.stack.imgur.com/DBKAX.png

示例:

XML布局:

    <EditText
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:gravity="start"
    android:hint="@string/Event_Description"
    android:inputType="textCapSentences|textMultiLine"
    android:minLines="12"
    android:textColorLink="@color/link_color"
    android:textIsSelectable="true" />

代码示例:

    etDescription = findViewById(R.id.description);
    etDescription.addTextChangedListener(descriptionTextWatcher);
    etDescription.setMovementMethod(LinkMovementMethod.getInstance());
    
private TextWatcher descriptionTextWatcher = 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);
    }
};

1 个答案:

答案 0 :(得分:0)

我暂时意识到LongClick的反应,但是我发现此解决方案很尴尬,我想使其更好。

代码示例:

etDescription.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        etDescription.requestFocus();

        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(etDescription, InputMethodManager.SHOW_IMPLICIT);

        return true;
    }
});
相关问题