{"id":1138,"date":"2019-03-15T15:08:15","date_gmt":"2019-03-15T06:08:15","guid":{"rendered":"https:\/\/www.charlezz.com\/?p=1138"},"modified":"2019-03-15T15:08:15","modified_gmt":"2019-03-15T06:08:15","slug":"databinding%ec%9d%84-%ec%82%ac%ec%9a%a9%ec%8b%9c-textview%ec%9d%98-%ed%85%8d%ec%8a%a4%ed%8a%b8%ea%b0%80-%eb%b3%80%ea%b2%bd%eb%90%98%ec%a7%80-%ec%95%8a%eb%8a%94-%ea%b2%bd%ec%9a%b0","status":"publish","type":"post","link":"https:\/\/charlezz.com\/?p=1138","title":{"rendered":"DataBinding\uc744 \uc0ac\uc6a9\uc2dc TextView\uc758 \ud14d\uc2a4\ud2b8\uac00 \ubcc0\uacbd\ub418\uc9c0 \uc54a\ub294 \uacbd\uc6b0"},"content":{"rendered":"<p>LiveData\uc640 DataBinding\uc744 \uc774\uc6a9\ud558\uc5ec \ub3d9\uc801\uc73c\ub85c TextView\uc758 Text\ub97c \ubcc0\uacbd\ud558\ub294 \ub85c\uc9c1\uc744 \uc801\uc6a9\ud588\uc2b5\ub2c8\ub2e4.<br \/>\n\uc608\ub97c\ub4e4\uba74, &#8220;Hello World&#8221; \ub97c &#8220;Hello Charles&#8221;\ub294 \uac83\uacfc \uac19\uc774 \ub2e8\uc21c\ud55c \ubcc0\uacbd\uc774\uc600\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;layout xmlns:tools=\"http:\/\/schemas.android.com\/tools\" \r\n        xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"&gt;\r\n    &lt;data&gt;\r\n        &lt;variable name=\"viewModel\" type=\"com.charlezz.MainViewModel\"\/&gt;\r\n    &lt;\/data&gt;\r\n\r\n    &lt;TextView\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:text=\"@{viewModel.text}\"\/&gt;\r\n\r\n&lt;\/layout&gt;<\/pre>\n<pre class=\"lang:java decode:true\">class MainViewModel: ViewModel (){\r\n\r\n    val text = MutableLiveData&lt;CharSequence&gt;().apply{\r\n        text.value = \"Hello World\"\r\n    }\r\n\r\n    fun changeText(enabled:Boolean){\r\n        text.value = \"Hello Charles\"            \r\n    }\r\n}<\/pre>\n<p>\uc2e4\uc81c \uc704\uc758 \ucf54\ub4dc\ub294 \ub3d9\uc791\ud558\ub294\ub370 \uc544\ubb34\uc774\uc0c1\uc774 \uc5c6\uc5c8\uc2b5\ub2c8\ub2e4.<br \/>\n&#8220;Hello World&#8221;\uc5d0\uc11c &#8220;<span style=\"color: #0000ff;\">Hello<\/span> World&#8221;\ub85c \ud14d\uc2a4\ud2b8\uc758 \uc77c\ubd80 \uc0c9\uc0c1\ub9cc \ubc14\uafb8\uba74 \uc5b4\ub5bb\uac8c \ub420\uae4c\uc694?<\/p>\n<p>\uc6b0\uc120 \ud14d\uc2a4\ud2b8\uc758 \uc0c9\uc0c1\uc744 \ubcc0\uacbd\ud558\ub294 \ucf54\ub4dc\ub294 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"lang:java decode:true\">class MainViewModel: ViewModel (){\r\n\r\n    val text = MutableLiveData&lt;CharSequence&gt;().apply{\r\n        text.value = \"Hello World\"\r\n    }\r\n\r\n    fun changeText(enabled:Boolean){\r\n        val spannable = SpannableString(\"Hello World\")\r\n        spannable.setSpan(ForegroundColorSpan(Color.BLUE), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)     \r\n        text.value = spnnable\r\n    }\r\n}<\/pre>\n<p>SpannableString\uc744 \ub9cc\ub4dc\ub294 \uac83\uc774\uc8e0.<br \/>\n\uc2e4\ud589\ud558\uba74 \uacb0\uacfc\ub294 \uc5b4\ub5a8\uae4c\uc694?<\/p>\n<p><span style=\"color: #0000ff;\"><span style=\"color: #000000;\">&#8220;<\/span>Hello<\/span> World&#8221;\uac00 \ub418\uae38 \uae30\ub300\ud588\uc9c0\ub9cc \uc544\ubb34\ub7f0 \ubcc0\ud654\uac00 \uc0dd\uae30\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<\/p>\n<p>DataBinding\uc744 \uc0ac\uc6a9\ud558\uac8c \ub418\uba74 \ub808\uc774\uc544\uc6c3\uc5d0 \ub300\ud55c <strong>LayoutNameBindingImpl<\/strong>\uac1d\uccb4\uac00 \uc0dd\uc131\ub418\uace0, \uac01 \uc704\uc82f\uc5d0 \ub9de\ub294 \uae30 \uc815\uc758\ub41c BindingAdapter\ub97c \ud1b5\ud574\uc11c \ub370\uc774\ud130\uc640 \ubdf0\uac00 \ubc14\uc778\ub529 \ub429\ub2c8\ub2e4. TextView\uc758 \uacbd\uc6b0 TextViewBindingAdapter\uac00 \ucc38\uc870\ub429\ub2c8\ub2e4.<\/p>\n<pre class=\"lang:java decode:true \">public class TextViewBindingAdapter {\r\n    ...\r\n    @BindingAdapter(\"android:text\")\r\n    public static void setText(TextView view, CharSequence text) {\r\n        final CharSequence oldText = view.getText();\r\n        if (text == oldText || (text == null &amp;&amp; oldText.length() == 0)) {\r\n            return;\r\n        }\r\n        if (text instanceof Spanned) {\r\n            if (text.equals(oldText)) {\r\n                return; \/\/ No change in the spans, so don't set anything.\r\n            }\r\n        } else if (!haveContentsChanged(text, oldText)) {\r\n            return; \/\/ No content changes, so don't set anything.\r\n        }\r\n        view.setText(text);\r\n    }\r\n    ...\r\n}<\/pre>\n<p>\uc2e4\ud589\uc2dc text\uac00 \uc544\uc9c1 \ucd08\uae30\ud654\ub418\uc9c0 \uc54a\uc740\ucc44\ub85c \ubc14\uc778\ub529\uc774 \ub418\ub294\uacbd\uc6b0(null)\uc640 \ub3d9\uc77c\ud55c \ud14d\uc2a4\ud2b8\uac00 \ubc18\ubcf5\uc801\uc73c\ub85c \ubc14\uc778\ub529\ub418\ub294 \uacbd\uc6b0\uac00 \uc788\uae30 \ub54c\ubb38\uc5d0, \ud37c\ud3ec\uba3c\uc2a4 \ud5a5\uc0c1\uc744 \uc704\ud574\uc11c view.setText(text)\uac00 \ud638\ucd9c\ub418\uae30 \uae4c\uc9c0 \uba87\uac1c\uc758 \uc870\uac74\ubb38\uc744 \ud1b5\ud574 \uac78\ub7ec\uc8fc\uace0 \uc788\ub294 \ubaa8\uc2b5\uc785\ub2c8\ub2e4. \ubc14\ub85c \uc774\uc810 \ub54c\ubb38\uc785\ub2c8\ub2e4. oldText(Hello World)\uc640 \uc0c8\ub85c \ub4e4\uc5b4\uc624\ub294 text(<span style=\"color: #0000ff;\">Hello<\/span> World)\uac00 \uac19\uae30 \ub54c\ubb38\uc5d0 setText(text)\uac00 \ud638\ucd9c\ub418\uc9c0 \uc54a\uc544 \ub80c\ub354\ub9c1 \ub418\uc9c0 \uc54a\uc558\ub358 \uac83\uc774\uc8e0.<\/p>\n<p>\uadf8\ub798\uc11c \uc544\ub798\uc640 \uac19\uc774 \uc800\ub294 CustomBindingAdapter\ub85c \ud574\uacb0\uc744 \ud588\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"lang:java decode:true\">@BindingAdapter(\"textByForce\")\r\npublic static void setText(EditText view, CharSequence text){\r\n    if(TextUtils.isEmpty(text)){\r\n        return;\r\n    }\r\n    view.setText(text);\r\n}<\/pre>\n<pre class=\"lang:java decode:true \">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;layout xmlns:tools=\"http:\/\/schemas.android.com\/tools\" \r\n        xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"&gt;\r\n    &lt;data&gt;\r\n        &lt;variable name=\"viewModel\" type=\"com.charlezz.MainViewModel\"\/&gt;\r\n    &lt;\/data&gt;\r\n\r\n    &lt;TextView\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            app:textByForce=\"@{viewModel.text}\"\/&gt;\r\n\r\n&lt;\/layout&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LiveData\uc640 DataBinding\uc744 \uc774\uc6a9\ud558\uc5ec \ub3d9\uc801\uc73c\ub85c TextView\uc758 Text\ub97c \ubcc0\uacbd\ud558\ub294 \ub85c\uc9c1\uc744 \uc801\uc6a9\ud588\uc2b5\ub2c8\ub2e4. \uc608\ub97c\ub4e4\uba74, &#8220;Hello World&#8221; \ub97c &#8220;Hello Charles&#8221;\ub294 \uac83\uacfc \uac19\uc774 \ub2e8\uc21c\ud55c \ubcc0\uacbd\uc774\uc600\uc2b5\ub2c8\ub2e4. &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt; &lt;layout xmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221; xmlns:app=&#8221;http:\/\/schemas.android.com\/apk\/res-auto&#8221;&gt; &lt;data&gt; &lt;variable name=&#8221;viewModel&#8221; type=&#8221;com.charlezz.MainViewModel&#8221;\/&gt; &lt;\/data&gt; &lt;TextView android:layout_width=&#8221;wrap_content&#8221; android:layout_height=&#8221;wrap_content&#8221; android:text=&#8221;@{viewModel.text}&#8221;\/&gt; &lt;\/layout&gt; class MainViewModel: ViewModel (){ val text = MutableLiveData&lt;CharSequence&gt;().apply{ text.value = &#8220;Hello World&#8221; } fun changeText(enabled:Boolean){ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[5],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/1138"}],"collection":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1138"}],"version-history":[{"count":2,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions"}],"predecessor-version":[{"id":1141,"href":"https:\/\/charlezz.com\/index.php?rest_route=\/wp\/v2\/posts\/1138\/revisions\/1141"}],"wp:attachment":[{"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/charlezz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}