0

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

我与带有可点击和可编辑链接的问题 EditTexts 有相同的问题,但我也有多行文本。

问题是:

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

扩展:

当我单击 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);
    }
};
4

1 回答 1

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;
    }
});
于 2020-10-05T13:13:29.667 回答