【问题标题】:How to change span position automatically, if some text added before spanned text in EditText?如果在 EditText 中的跨区文本之前添加了一些文本,如何自动更改跨区位置?
【发布时间】:2017-08-21 07:16:12
【问题描述】:

我正在创建一个应用程序,其中 edittext 将样式范围添加到文本的某些位置

editable.setSpan(new StyleSpan(Typeface.BOLD), start, start+replacement.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

我的问题是,如果用户添加,跨越文本样式之前的一些文本或空白将被删除,如何避免这种情况?

    int SPAN_COMPOSING = 256;
    int SPAN_EXCLUSIVE_EXCLUSIVE = 33;
    int SPAN_EXCLUSIVE_INCLUSIVE = 34;
    int SPAN_INCLUSIVE_EXCLUSIVE = 17;
    int SPAN_INCLUSIVE_INCLUSIVE = 18;
    int SPAN_INTERMEDIATE = 512;
    int SPAN_MARK_MARK = 17;
    int SPAN_MARK_POINT = 18;
    int SPAN_PARAGRAPH = 51;
    int SPAN_POINT_MARK = 33;
    int SPAN_POINT_MARK_MASK = 51;
    int SPAN_POINT_POINT = 34;
    int SPAN_PRIORITY = 16711680;
    int SPAN_PRIORITY_SHIFT = 16;
    int SPAN_USER = -16777216;
    int SPAN_USER_SHIFT = 24;

在我的情况下我需要使用哪种类型?

【问题讨论】:

  • 使用 TextWatcher。每当您的文本发生更改时,您都会收到回调。在那里使用 setSpan。
  • 你能再看一下我的问题吗?
  • 我知道这可能有点晚了,但你应该首先确保你跨越到可编辑。长度 .EXCLUSIVE_EXCLUSIVE 如果你不希望跨度向左或向右扩展是最好的。但是这个如果您跨越一个愚蠢的单词的一部分,则可能由于自动更正而出现问题。您可以通过禁用自动更正。 android:inputType="textMultiLine|textCapSentences|textShortMessage|textVisiblePassword" 在 EditText XML 中

标签: android spannablestring spannable spanned android-editable


【解决方案1】:

您可以使用如下代码。

editable.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) {
                // here is your code to change position
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        });

【讨论】:

  • 能否请您详细说明一下,或者如果可以先发给我之前的图片。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多