2013-07-29 39 views
2

我的應用應該允許用戶在Edittext中對輸入或選定的文本進行樣式設置。其中一些樣式是下劃線,直通,粗體和斜體。他們很容易添加,但我不知道他們如何被刪除我怎麼能確定,如果這種風格已被添加到選定的文本。 代碼添加樣式:從edittext中的選定文本中刪除樣式

Spannable str = myEditText.getText(); 
    if (!SpannableUtils.isEmpty(str)) { 
     str.setSpan(new StrikethroughSpan(), startSelection, endSelection, 
       Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    } 

我嘗試使用removeSpan,但沒有奏效。 使用

 str.setSpan(new StyleSpan(android.graphics.Typeface.DEFAULT), 
       startSelection, endSelection, 
       Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

也試過,但它造成的錯誤:構造StyleSpan(字體)是未定義的。

+0

定義 「造成的錯誤」 – njzk2

+0

抱歉。用錯誤更新了問題。 – msana

回答

6

我覺得這個課程解決了你的問題。

public class StyleSpanRemover { 

    public void RemoveOne(Spannable spannable, 
          int startSelection, int endSelection, Class<?> style){ 

     ArrayList<SpanParts> spansParts = getSpanParts(spannable, startSelection, endSelection); 
     removeOneSpan(spannable, startSelection, endSelection, style); 
     restoreSpans(spannable, spansParts); 
    } 

    public void RemoveStyle(Spannable spannable, 
       int startSelection, int endSelection, int styleToRemove){ 

     ArrayList<SpanParts> spansParts = getSpanParts(spannable, startSelection, endSelection); 
     removeStyleSpan(spannable, startSelection, endSelection, styleToRemove); 
     restoreSpans(spannable, spansParts); 
    } 

    public void RemoveAll(Spannable spannable, int startSelection, int endSelection){ 

     ArrayList<SpanParts> spansParts = getSpanParts(spannable, startSelection, endSelection); 
     removeAllSpans(spannable, startSelection, endSelection); 
     restoreSpans(spannable, spansParts); 
    } 

    protected void restoreSpans(Spannable spannable, ArrayList<SpanParts> spansParts){ 

     for (SpanParts spanParts : spansParts) { 
      if(spanParts.part1.canAplly()) 
       spannable.setSpan(spanParts.part1.span, spanParts.part1.start, 
            spanParts.part1.end,spanParts.span_flag); 
      if(spanParts.part2.canAplly()) 
       spannable.setSpan(spanParts.part2.span, spanParts.part2.start, 
            spanParts.part2.end, spanParts.span_flag); 
     } 
    } 

    protected void removeAllSpans(Spannable spannable,int startSelection, int endSelection) { 

     Object spansToRemove[] = spannable.getSpans(startSelection, endSelection, Object.class); 
     for(Object span: spansToRemove){ 
      if(span instanceof CharacterStyle) 
       spannable.removeSpan(span); 
     } 
    } 

    protected void removeOneSpan(Spannable spannable,int startSelection, int endSelection, 
          Class<?> style) { 

     CharacterStyle spansToRemove[] = spannable.getSpans(startSelection, endSelection, CharacterStyle.class); 
     for(CharacterStyle span: spansToRemove){ 
      if(span.getUnderlying().getClass() == style) 
       spannable.removeSpan(span); 
     } 
    } 

    protected void removeStyleSpan(Spannable spannable, int startSelection, 
     int endSelection, int styleToRemove) { 

     MetricAffectingSpan spans[] = spannable.getSpans(startSelection, endSelection, MetricAffectingSpan.class); 
     for(MetricAffectingSpan span: spans){ 
      int stylesApplied = 0; 
      int stylesToApply; 
      int spanStart; 
      int spanEnd; 
      int spanFlag; 
      Object spanUnd = span.getUnderlying(); 
      if(spanUnd instanceof StyleSpan){ 
       spanFlag = spannable.getSpanFlags(spanUnd); 
       stylesApplied = ((StyleSpan) spanUnd).getStyle(); 
       stylesToApply = stylesApplied & ~styleToRemove; 

       spanStart = spannable.getSpanStart(span); 
       spanEnd = spannable.getSpanEnd(span); 
       if(spanEnd >= 0 && spanStart >= 0){ 
        spannable.removeSpan(span); 
        spannable.setSpan(new StyleSpan(stylesToApply), spanStart, spanEnd,spanFlag); 
       } 
      } 
     } 
    } 

    protected ArrayList<SpanParts> getSpanParts(Spannable spannable, 
               int startSelection,int endSelection){ 

     ArrayList<SpanParts> spansParts = new ArrayList<SpanParts>(); 
     Object spans[] = spannable.getSpans(startSelection, endSelection, Object.class); 
     for(Object span: spans){ 
      if(span instanceof CharacterStyle){ 
       SpanParts spanParts = new SpanParts(); 
       int spanStart = spannable.getSpanStart(span); 
       int spanEnd = spannable.getSpanEnd(span); 
       if(spanStart == startSelection && spanEnd == endSelection) continue; 
       spanParts.span_flag = spannable.getSpanFlags(span); 
       spanParts.part1.span = CharacterStyle.wrap((CharacterStyle) span); 
       spanParts.part1.start = spanStart; 
       spanParts.part1.end = startSelection; 
       spanParts.part2.span = CharacterStyle.wrap((CharacterStyle) span); 
       spanParts.part2.start = endSelection; 
       spanParts.part2.end = spanEnd; 
       spansParts.add(spanParts); 
      } 
     } 
     return spansParts; 
    } 

    private class SpanParts{ 
     int span_flag; 
     Part part1; 
     Part part2; 
     SpanParts() { 
      part1 = new Part(); 
      part2 = new Part(); 
     } 
    } 
    private class Part{ 
     CharacterStyle span; 
     int start; 
     int end; 
     boolean canAplly() { 
      return start < end; 
     } 
    } 
} 

使用方法:

int startSelection=editText.getSelectionStart(); 
int endSelection=editText.getSelectionEnd(); 
Spannable spannable = editText.getText(); 

StyleSpanRemover spanRemover = new StyleSpanRemover(); 
// to remove all styles 
spanRemover.RemoveAll(spannable,startSelection,endSelection); 
//to remove only StrikethroughSpan style use: 
spanRemover.RemoveOne(spannable,startSelection,endSelection,StrikethroughSpan.class); 
//to remove one StyleSpan use: 
spanRemover.RemoveStyle(spannable,startSelection,endSelection,Typeface.BOLD) 
+0

Spannable仍然有這些方法..但是,當我將它與removeSpan結合使用時,它們並沒有這樣做。 – msana

+0

是的,因爲兩者都實現了Spanned。我需要更多的信息才能提供幫助,因爲'removeSpan'應該可以工作 – ramaral

+0

刪除跨度確實有效,但現在我以這種方式工作。我的目標是僅從所選文本中刪除樣式平移。而不是edittext中的全部文本。 – msana

3

android.graphics.TYPEFACE.Default返回Typeface,和StyleSpan構造函數採用int爲paramater。

嘗試使用:

android.graphics.Typeface.DEFAULT.getStyle() 

代替:

android.graphics.Typeface.DEFAULT 
+0

android.graphics.Typeface.DEFAULT.getStyle()沒有做到這一點。它將文本恢復到原始樣式。 – msana

0

我覺得下面這種方式是最簡單但很有用。這個對我有用。

請試試這個代碼:

myEditText.setTypeface(Typeface.DEFAULT);