๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[์•ˆ๋“œ๋กœ์ด๋“œ] ๊ธ€์ž ์ƒ‰ ๋ฐ”๊พธ๊ธฐ - SpannableString, SpannableStringBuilder

by devshin.kr 2021. 11. 26.
728x90
๋ฐ˜์‘ํ˜•


build.gradle (:app)

plugins {
    id 'com.android.application'
}

android {
    compileSdk 30

    defaultConfig {
        applicationId "com.devshin93.spannabletest"
        minSdk 25
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="50sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>



MaginActivity.java
์ฃผ์„์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ฐ”๊ฟ” ๊ฐ€๋ฉฐ ํ™•์ธํ•ด ๋ณด์ž.

package com.devshin93.spannabletest;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;

import androidx.appcompat.app.AppCompatActivity;

import com.devshin93.spannabletest.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        SpannableString str = new SpannableString("Red");
        SpannableStringBuilder resultBuilder = new SpannableStringBuilder(str);

        int begin = 0;
        int end = resultBuilder.length();

        resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);    // Red Black
//        resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);    // Red Red
//        resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    // Red Red
//        resultBuilder.setSpan(new ForegroundColorSpan(Color.RED), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);      // Red Black

        resultBuilder.append("Black");

        binding.textView.setText(resultBuilder);
    }
}

 



1๋ฒˆ, 4๋ฒˆ ๊ฒฐ๊ณผ
Spannable.SPAN_INCLUSIVE_EXCLUSIVE
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

 

 

2๋ฒˆ, 3๋ฒˆ ๊ฒฐ๊ณผ
Spannable.SPAN_INCLUSIVE_INCLUSIVE
Spannable.SPAN_EXCLUSIVE_INCLUSIVE

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€