2014-09-01 3 views
1

굵게 표시된 텍스트를 EditText에 설정하고 싶습니다.선택한 텍스트를 굵게 설정하는 방법은 무엇입니까?

내가 선택한 문자를 알아낼 수 있으며 getSelectionStart() and getSelectionEnd() 위치를 알고 있습니다.

하지만 내 문제는 선택한 텍스트를 Button으로 굵게 설정하고 글꼴을 굵게 문자열로 설정하는 방법을 모르겠다는 것입니다 (단 EditText으로 설정하는 방법 만 알고 있음).

fettdruckButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      int selectionStart = fullscreenEdittext.getSelectionStart(); 
      int selectionEnd = fullscreenEdittext.getSelectionEnd(); 

      int differenz = selectionEnd - selectionStart; 

      String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd); 
      Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd); 


      //fullscreenEdittext.setTypeface(null, Typeface.BOLD); 

     } 
    }); 
+0

단추를 클릭 한 후 또는 텍스트를 선택하는 동안 단추를 클릭하십시오. –

+0

단추를 클릭 한 후 선택한 텍스트를 굵게 표시해야합니다. – user3603935

답변

0

선택한 텍스트를 굵게하는 EditextfullscreenEdittext.setText(Html.fromHtml(styledText)); 를 사용하여 HTML 서식 태그 <b>selectedText</b>의 속성을 사용 :

그래서 여기 내 예제 코드입니다. 코드 아래에서 봐주세요

int selectionStart = fullscreenEdittext.getSelectionStart(); 
    int selectionEnd = fullscreenEdittext.getSelectionEnd(); 

    String startingText = fullscreenEdittext.getText().toString() 
      .substring(0, selectionStart); 
    String selectedText = fullscreenEdittext.getText().toString() 
      .substring(selectionStart, selectionEnd); 
    String endingText = fullscreenEdittext.getText().toString() 
      .substring(selectionEnd); 

    fullscreenEdittext.setText(Html.fromHtml(startingText + "<b>" 
      + selectedText + "</b>" + endingText)); 
0
String completetext=fullscreenEdittext.getText().toString(); 
int selectionStart = fullscreenEdittext.getSelectionStart(); 
int selectionEnd = fullscreenEdittext.getSelectionEnd(); 
int differenz = selectionEnd - selectionStart; 
String selectedText = fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd); 
Log.d("selectedText", "selectedText " + selectedText + "|" + selectionStart + "|" + selectionEnd); 

String part1=fullscreenEdittext.getText().toString().substring(0, selectionStart); 

String part2=fullscreenEdittext.getText().toString().substring(selectionStart, selectionEnd); 

String part3=fullscreenEdittext.getText().toString().substring(selectionEnd,completetext.length()); 

fullscreenEdittext.setText(Html.fromHtml(part1+"<b>" + part2+ "</b>" +part3)); 
1

사용 SpannableStringBuilder는 니펫을.

SpannableStringBuilder stringBuilder = (SpannableStringBuilder) fullscreeneditText.getText(); 
stringBuilder.setSpan(new StyleSpan(Typeface.BOLD), selectionStart, selectionEnd, 0); 

이 작업을 여러 번 수행하려면 먼저 원래의 범위를 제거해야합니다.

관련 문제