繁体   English   中英

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

[英]Multiline text in EditText with links, both clickable and editable

我正在使用多行 EditText 视图,它可以在输入中包含 webUrl。 为此,我使用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);
    }
};

暂时我意识到 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;
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM