2014-10-16 2 views
0

서식있는 텍스트 편집기를 만들고 있습니다. 요구 사항은 사용자가 편집 텍스트에서 편집기를 사용한다는 것입니다. 사용자가 텍스트 편집을 완료하면 edittext의 html 내용이 수집되어 서버에 게시됩니다. edittext의 스타일은 서버에 게시되어야합니다.중첩 된 HTML 태그

나는 다음과 같은 서식있는 텍스트 편집기 글고에서 서식을 구현 한 :

mainEditText.addTextChangedListener(new TextWatcher() { 
    public void afterTextChanged(Editable spanTest) { 
     boldBtn = (ToggleButton) findViewById(R.id.boldBtn); 
     italicBtn = (ToggleButton) findViewById(R.id.italicBtn); 
     underLineBtn = (ToggleButton) findViewById(R.id.underLineBtn); 

     // http://developer.android.com/reference/android/text/Selection.html 

     // Runa toast for the selector index 

     // http://developer.android.com/reference/android/content/ClipboardManager.html 

     try { 
      int selectionStart = Math.max(mainEditText.getSelectionStart(), 0); 

      int position = Selection.getSelectionStart(mainEditText.getText()); 
      if (position < 0) { 
       position = 0; 
      } 
      if (position > 0) { 

       if (selectionStart > position || position > (cursorLoc + 1)) { 
        // user changed cursor location, reset 
        selectionStart = position - 1; 
       } 
       cursorLoc = position; 
       if (boldBtn.isChecked()) { 
        // edited to make texteditor bug free 
        if (cursorLoc >= styleStart) { 
         spanTest.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
         Log.e("Html span: " , Html.toHtml(spanTest)); 
        } else {} 

       } 

       if (italicBtn.isChecked()) { 
        // edited to make texteditor bug free 
        if (cursorLoc >= styleStart) { 
         spanTest.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        } else {} 

       } 

       if (underLineBtn.isChecked()) { 
        //edited to make texteditor bug free 
        if (cursorLoc >= styleStart) { 
         spanTest.setSpan(new UnderlineSpan(), styleStart, position, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        } else {} 
       } 

      } 
     } catch (IndexOutOfBoundsException varName) {} 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     // testClipboard(); 
    } 

    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     int startSelection = Selection.getSelectionStart(s); 
     int endSelection = Selection.getSelectionEnd(s); 

    } 
}); 

이것은 내가 서버로 보내기 전에 텍스트를 확인하고 방법입니다

String htmlString=Html.toHtml(mainEditText.getText()); 
     makeAToast(htmlString); 
     Log.d("htmlString", htmlString); 

HTML하는 중첩 태그가 생성됩니다.

<p dir="ltr">hello boys <b><b><b><b><b><b><b><b><b>n</b></b></b></b></b></b></b></b></b><b><b><b><b><b><b><b><b>o</b></b></b></b></b></b></b></b><b><b><b><b><b><b><b>w</b></b></b></b></b></b></b><b><b><b><b><b><b> </b></b></b></b></b></b><b><b><b><b><b>b</b></b></b></b></b><b><b><b><b>o</b></b></b></b><b><b><b>l</b></b></b><b><b>d</b></b><b> </b></p> 

어디에서 잘못 했습니까? 서버로 보내기 전에 적절한 html을 생성하기 위해 조정해야하는 것은 무엇입니까?

+0

getText()를 수행하기 전에 TextView.clearComposingText()를 사용해 볼 수 있습니까? – Rohan

+0

@Rohan 실제로 어디에 넣으시겠습니까? – kittu88

+0

텍스트를 서버로 보내기 전에 mainEditText.clearComposingText(); mainEditText.getText()를 호출하십시오. – Rohan

답변

0

동일한 스타일에 복수 StyleSpan이 추가되었으므로 동일한 범위가 이미 선택 항목에 적용되었는지 확인하는 것이 좋습니다.

// get existing spans on selection 
StyleSpan[] existingSpans = spanTest.getSpans (editText.getSelectionStart(), editText.getSelectionEnd(), StyleSpan.class); 

boolean styleExists = false; 
int yourStyle = Typeface.BOLD; // add your button logic here to get the appropriate style 

foreach (StyleSpan span : existingSpans) 
{ 
    if (span.getStyle() == yourStyle) 
    { 
     styleExists = true; 
     break; 
    } 
} 

if (!styleExists) 
{ 
    spanTest.setSpan (new StyleSpan (yourStyle, editText.getSelectionStart(), editText.getSelectionEnd(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 
else 
{ 
    // do nothing as a span with same style already exists. 
} 
+0

"편집 가능"이란 무엇입니까? – kittu88

+0

편집 가능은 귀하의 경우 spanTest입니다. 이거 시도해 봤어? – Rohan

+0

예 지금 구현 중입니다. ... – kittu88