안드로이드 텍스트뷰 속성 3. [bufferType] (Android TextView Attributes 3)

2016. 8. 29. 12:16


1. TextView 속성 (3)

TextView 속성 리스트 및 요약 설명을 확인하시려면 [안드로이드 텍스트뷰 속성] 페이지를 참고하시기 바랍니다.


TextView 속성에 대한 자세한 설명 및 예제를 확인하시려면, 아래 표에서 속성 이름을 클릭하시기 바랍니다.


속성 속성 속성 속성 속성
autoLink autoText breakStrategy bufferType capitalize
cursorVisible digits drawableBottom drawableEnd drawableLeft
drawablePadding drawableRight drawableStart drawableTint drawableTintMode
drawableTop editable editorExtras elegantTextHeight ellipsize
ems fontFamily fontFeatureSettings freezesText gravity
height hint hyphenationFrequency imeActionId imeActionLabel
imeOptions includeFontPadding inputMethod inputType letterSpacing
lineSpacingExtra lineSpacingMultiplier lines linksClickable marqueeRepeatLimit
maxEms maxHeight maxLength maxLines maxWidth
minEms minHeight minLines minWidth numeric
password phoneNumber privateImeOptions scrollHorizontally selectAllOnFocus
shadowColor shadowDx shadowDy shadowRadius singleLine
text textAllCaps textAppearance textColor textColorHighlight
textColorHint textColorLink textIsSelectable textScaleX textSize
textStyle typeface width

2. TextView 속성 활용

2.1 getText() 함수로 리턴되는 텍스트를 위한 버퍼 타입 지정하기. (bufferType)

TextView의 getText() 함수는 Java 코드 상에서 TextView의 텍스트를 가져오기 위해 사용하는 함수입니다. TextView를 사용한 경험이 있으면 거의 다 아는 사실이죠.
음... 표현을 조금 달리 해볼까요? TextView의 getText() 함수는 TextView 내부의 "텍스트를 저장하는 버퍼에 대한 참조"를 가져오기 위해 사용하는 함수입니다. 즉, getText() 함수를 호출하면 내부 버퍼에 대한 참조를 CharSequence 타입으로 넘겨 받아 사용할 수 있습니다.

  public CharSequence getText () ;

이 때, TextView의 "bufferType" 속성을 사용하면 리턴되는 타입을 CharSequence 외에 Spannable 또는 Editable로 변환(cast)하여 사용할 수 있습니다.

  * android:bufferType - getText() 함수가 리턴하는 최소(minimum) 버퍼 타입 지정.
        > normal, spannable, editable 값 사용 가능. (TextView의 기본 값 normal)
          -. normal (0) : CharSequence 리턴할 수 있음.
          -. spannable (1) : 오직 Spannable만 리턴할 수 있음.  
          -. editable (2) : 오직 Spannable 또는 Editable만 리턴할 수 있음.
        > TextView의 기본 값은 normal.
        > EditText의 경우, 속성 설정과 관계없이 editable로 적용.

Spannable은 CharSequence의 서브 인터페이스입니다. Editable은 Spannable의 서브 인터페이스입니다. 각 인터페이스에 대한 자세한 설명 및 사용법은 문자열 처리에 대한 별도의 내용으로 설명하겠지만, 차이점만 간단히 구분하자면 다음과 같습니다.


  • CharSequence : 바꿀 수 없는(immutable) 문자열(contents) 처리에 사용. (내용 불변)
  • Spannable : 문자열(contents)은 바꿀 수 없으나 markup 객체를 적용(attach) 또는 해제(detach) 가능.
  • Editable : 문자열(contents) 및 markup 객체를 모두 변경 가능.


bufferType 속성은 최소(minimum) 버퍼 타입을 지정하는 것이므로, "normal" 값을 지정하면 리턴 타입을 Spannable 또는 Editable로 타입 변환(cast)하여 사용할 수 없습니다. 텍스트를 변경할 수 없도록 설정하였는데, 변경이 가능한 인터페이스 형태로 변환하여 사용할 수 없는 것이죠.
하지만 bufferType 값을 "editable"로 지정하면 셋 중 아무거나 타입 변환(cast)하여 사용할 수 있습니다. 내용을 변경할 수 있도록 만들었으니, 외부에서 그것을 변경하든 안하든 큰 문제는 되지 않는 것입니다.


아래 예제를 한번 살펴보겠습니다.

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:bufferType="editable"
        android:id="@+id/textView1"
        android:text="ABC" />
    TextView textView1 = (TextView) findViewById(R.id.textView1) ;
    CharSequence csStr = (CharSequence)textView1.getText() ; 

위에서 설명한대로, CharSequence로 타입 변환(cast)을 해도, 문제가 발생하지 않습니다.


이번에는 bufferType 속성 값을 "normal"로 사용하고 Editable로 타입 변환(cast)를 수행한 예제를 한번 보죠.

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:bufferType="normal"
        android:id="@+id/textView1"
        android:text="ABC" />
    TextView textView1 = (TextView) findViewById(R.id.textView1) ;
    Editable edtStr = (Editable)textView1.getText() ; 

normal로 지정한 버퍼 타입을 Editable로 접근하려고 하면 Exception이 발생합니다.

    ...
    java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Editable
    ...
    Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Editable

위의 설명만으로 bufferType 속성에 대해 이해가 잘 되지 않는다면, String, CharSequence, Spannable, Editable 등에 대해 살펴보시고, 각 클래스 또는 인터페이스 간의 관계에 대해서 분석해보시기를 권장합니다.

텍스트뷰 속성 bufferType


3. 참고.


ANDROID 프로그래밍/TEXTVIEW