【问题标题】:How to clear formatting from an EditText?如何从 EditText 中清除格式?
【发布时间】:2018-04-25 19:19:25
【问题描述】:

我有一个 EditText,并且可以添加粗体、斜体等格式......但我怎样才能删除它呢?我研究了 getSpan、过滤器和其他非字符串的东西,但无法理解它们!理想情况下,我希望能够清除特定标签以及围绕所选文本设置的所有标签。

更新我的解决方案:

private String getSelectedText(){
        int start = Math.max(mText.getSelectionStart(), 0);
        int end = Math.max(mText.getSelectionEnd(), 0);
        return mText.getText().toString().substring(Math.min(start, end), Math.max(start, end));
    }
private void clearFormat(){
        int s1 = Math.max(mText.getSelectionStart(), 0);
        int s2 = Math.max(mText.getSelectionEnd(), 0);
        String text = getSelectedText(); if(text==""){ return; }
        EditText prose = mText;
        Spannable raw = new SpannableString(prose.getText());
        CharacterStyle[] spans = raw.getSpans(s1, s2, CharacterStyle.class);
        for (CharacterStyle span : spans) {
            raw.removeSpan(span);
        }
        prose.setText(raw);
        //Re-select
        mText.setSelection(Math.min(s1,s2), Math.max(s1, s2));
    }

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    但是我怎样才能删除它呢?

    拨打removeSpan()Spannable

    例如,this sample project 中的此方法在 TextView 的内容中搜索搜索字符串并为其分配背景颜色,但仅在删除任何以前的背景颜色之后:

    private void searchFor(String text) {
        TextView prose=(TextView)findViewById(R.id.prose);
        Spannable raw=new SpannableString(prose.getText());
        BackgroundColorSpan[] spans=raw.getSpans(0,
                                                 raw.length(),
                                                 BackgroundColorSpan.class);
    
        for (BackgroundColorSpan span : spans) {
          raw.removeSpan(span);
        }
    
        int index=TextUtils.indexOf(raw, text);
    
        while (index >= 0) {
          raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
              + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
          index=TextUtils.indexOf(raw, text, index + text.length());
        }
    
        prose.setText(raw);
      }
    }
    

    【讨论】:

    • 谢谢,这让我走上了正轨!只需进行一些调整 :)
    • 如何使用 remove span 删除特定标签?
    • @Kutbi:我不知道这里的“标签”是什么。您可能希望提出一个单独的 Stack Overflow 问题,在那里您可以详细说明您正在寻找什么。
    【解决方案2】:

    你可以尝试的是:

    1- 创建一个自定义样式,您的 EditText 将具有“例如粗体、斜体...”

    2- 请注意在运行时使用R.style.normalText 将其改回正常样式

    3- 根据您希望通过setTextAppearance(Context context, int resid) 实现的行为更改此样式

    这是我在谷歌上搜索到的一个例子 How to change a TextView's style at runtime

    编辑:因为您的问题是“如何从 EditText 中清除格式”,所以这里是代码的具体答案:

    editTextToClearStyle.setTextAppearance(this,R.style.normalText);
    

    【讨论】:

      【解决方案3】:

      请看下面sn-p的评论。

      if (makeItalic) {
          SpannableString spanString = new SpannableString(textViewDescription.getText());
          spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, spanString.length(), 0);
          this.textViewDescription.setText(spanString);
      } else {
          SpannableString spanString = new SpannableString(
              textViewDescription.getText().toString()); // NOTE: call 'toString()' here!
          spanString.setSpan(new StyleSpan(Typeface.NORMAL), 0, spanString.length(), 0);
          this.textViewDescription.setText(spanString);
      }
      

      ...只需调用toString() 方法即可获取原始字符串字符。

      【讨论】:

        猜你喜欢
        • 2019-07-25
        • 2012-07-17
        • 2020-03-14
        • 1970-01-01
        • 1970-01-01
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多