[英]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.