¾Èµå·ÎÀ̵å > ¹®ÀÚ¿ ¸®¼Ò½º <string>
¾Èµå·ÎÀ̵忡¼´Â ¿©·¯ ¸®¼Ò½ºµéÀ» /res/ ¹Ø¿¡¼ °ü¸®ÇÑ´Ù.
¹®ÀÚ¿ ¸®¼Ò½º´Â ±âº»ÀûÀ¸·Î /res/values ¹Ø¿¡ strings.xml¿¡¼ °ü¸®µÈ´Ù.
ÇÊ¿äÇÑ ¹®ÀÚ¿À» strings.xml¿¡ µî·ÏÇÏ°í, layout°ú Activity¿¡¼ ¸®¼Ò½º¸¦ »ç¿ëÇغ¸ÀÚ.
1. res(¸®¼Ò½º)¿¡ String ¹®ÀÚ¿ µî·ÏÇϱâ
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">HelloAndroid</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="sample1">Hello~! Friends~!</string>
<string name="sample2"><i>Hello~!</i><b>Android~!</b></string>
<string name="sample3">Name : %1$s, Score : %2$d</string>
</resources>
sample1 : Hello~! Friends~! ¶ó´Â StringÀ» ¸®¼Ò½º·Î µî·ÏÇß´Ù.
sample2 : Hello~! ´Â ±â¿ï±â¸¦ ³Ö°í, Android~!¸¦ º¼µå ó¸®ÇÑ StringÀ» ¸®¼Ò½º·Î µî·ÏÇß´Ù.
sample3 : ù¹ø° ÀÎÀÚ(%1)Àº ¹®ÀÚÇü($s), µÎ¹ø° ÀÎÀÚ(%2)´Â ¼ýÀÚÇü($d)·Î ¸ÅÇÎÇÒ ¼ö ÀÖ´Â StringÀ» ¸®¼Ò½º·Î µî·ÏÇß´Ù.
(app_name, action_settings, hello_world´Â ÃÖÃÊ ÇÁ·ÎÁ§Æ® »ý¼º½Ã µî·ÏµÇ¾î ÀÖ´Ù.)
2. layoutÀÇ xml¿¡¼ String ¸®¼Ò½º ºÒ·¯¿À±â
¾Èµå·ÎÀ̵åÀÇ È¸éÀº /res/layout ¹Ø¿¡ xml ÆÄÀÏ·Î Á¤ÀǵǾî ÀÖ´Ù.
ÇÁ·ÎÁ§Æ® »ý¼º½Ã ÃÖÃÊ È¸éÀº activity_main.xml ·Î ÁöÁ¤µÇ¾î ÀÖ´Ù.
activity_main.xml¿¡ µî·ÏÇÑ String ¸®¼Ò½º¸¦ ºÒ·¯¿Í ȸ鿡 Ãâ·ÂÇغ¸ÀÚ.
res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sample1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp"
android:text="@string/sample2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="15dp"
android:text="@string/sample3" />
</RelativeLayout>
- <TextView> °¡ 3°³ µî·ÏµÇ¾îÀÖ´Ù.
- ù ¹ø° <TextView>´Â id°¡ textView1 À̸ç, string ¸®¼Ò½ºÁß nameÀÌ sample1ÀÇ ¹®ÀÚ¿À» °¡Á®¿Â´Ù.(android:text="@string/sample1")
°á°ú: textView1 : Hello~! Friends~!
- µÎ¹ø°, ¼¼¹ø° <TextView>µµ ¸¶Âù°¡Áö·Î id´Â textView2, textView3 À̸ç, stirng ¸®¼Ò½ºÁß nameÀÌ sample2, sampl3ÀÇ ¹®ÀÚ¿À» °¡Á®¿Â´Ù.
°á°ú: textView2 : <i>Hello~!</i><b>Android~!</b> , textView3 :Name : %1$s, Score : %2$d
½ÇÇàȸéÀ» º¸ÀÚ!
-strings.xml ¿¡ µî·ÏµÈ ¹®ÀÚ¿ ¸®¼Ò½ºµéÀ» Ãâ·ÂÇÑ´Ù. ¿Í µî HTMLÀÇ ¹®¹ýÀÌ Àû¿ëµÇÁö¸¸,
%1$s, %2$d ´Â Activity¿¡¼ FormattingÀ¸·Î ¸ÅÇÎÀ» ÇØÁà¾ß ÇÑ´Ù.
3. Activity.java¿¡¼ ¸®¼Ò½º ºÒ·¯¿À±â
MainActivity.java¿¡¼ ¸®¼Ò½º¿¡ µî·ÏµÈ string ¹®ÀÚ¿À» ºÒ·¯¿Í¼ »ç¿ëÇغ¸ÀÚ.
// string ¸®¼Ò½ºÁß nameÀÌ sample1ÀÇ ¹®ÀÚ¿À» getString()À¸·Î °¡Á®¿Â´Ù.
// ¸®¼Ò½º¿¡¼ id°¡ textView1ÀÎ TextView¸¦ °¡Á®¿Í sample1 À» textView1 ¿¡ Àû¿ëÇÑ´Ù.
String sample1 = getString(R.string.sample1);
TextView textView1 = (TextView)findViewById(R.id.textView1);
textView1.setText(sample1);
// string ¸®¼Ò½ºÁß nameÀÌ sample2ÀÇ ¹®ÀÚ¿À» getText()·Î °¡Á®¿Â´Ù.
// ¸®¼Ò½º¿¡¼ id°¡ textView2ÀÎ TextView¸¦ °¡Á®¿Í sample2À» textView2 ¿¡ Àû¿ëÇÑ´Ù.
CharSequence sample2 = getText(R.string.sample2);
TextView textView2 = (TextView)findViewById(R.id.textView2);
textView2.setText(sample2);
// string ¸®¼Ò½ºÁß nameÀÌ sample3ÀÇ ¹®ÀÚ¿À» getString()À¸·Î °¡Á®¿Â´Ù.
// name À̶ó´Â String¿¡ htmlÇü½ÄÀ¸·Î ÀÎÄÚµùÇÑ "Hyeon" ¹®ÀÚ¿À» ÀúÀåÇÑ´Ù.
// String.format À» »ç¿ëÇÏ¿© sample3 ¿¡ name°ú 90À» ÀÎÀÚ·Î ´ëÀÔÇÑ °á°ú¸¦ resultText¿¡ ´ëÀÔÇÑ´Ù.
String sample3 = getString(R.string.sample3);
String name = TextUtils.htmlEncode("Hyeon");
String resultText = String.format(sample3, name, 90);
// ¸®¼Ò½º¿¡¼ id°¡ textView3ÀÎ TextView¸¦ °¡Á®¿Í resultText¸¦ textView3 ¿¡ Àû¿ëÇÑ´Ù.
TextView textView3 = (TextView)findViewById(R.id.textView3);
textView3.setText(resultText);
- String ¹®ÀÚ¿Àº getString() À¸·Î ¸®¼Ò½º¿¡ µî·ÏµÈ string ¹®ÀÚ¿À» °¡Á®¿Â´Ù.
- sample1 ¹®ÀÚ¿À» °¡Á®¿Í¼ TextView1 ¿¡ ¼ÂÆÃÇß´Ù.
- getText()ÀÇ °æ¿ì ¸®¼Ò½º¿¡ µî·ÏµÈ ¹®ÀÚ¿ÀÇ ÅÂ±× ¼Ó¼º±îÁö °¡Á®¿Â´Ù. ±×¸®°í ¹Ýȯ°ªÀº StringÀÌ ¾Æ´Ñ CharSequence ÀÌ´Ù.
- getText()¸¦ »ç¿ëÇÏ¿© sample2ÀÇ <i></i> <b></b> ¼Ó¼º±îÁö °¡Á®¿Í¼ TextView2¿¡ ¼ÂÆÃÇß´Ù.
- getString()À¸·Î °¡Á®¿Â sample3 ¿¡ String.format() À» »ç¿ëÇÏ¿© ÀÎÀÚ°ªÀ» ¸ÅÇÎÇØÁØ´Ù. (%1$s, %2$d)
- formattingÇÑ °á°ú¸¦ resultText¿¡ ´ëÀÔÇÑ ÈÄ TextView3¿¡ ¼ÂÆÃÇÑ´Ù.
- ·¹À̾ƿô¿¡¼ ±×´ë·Î ºÒ·¯¿Â °Í°ú ´Þ¸® (%1$s, %2$d) ¿¡ µ¥ÀÌÅÍ°¡ ¸ÅÇεǾî Ãâ·ÂµÈ´Ù.
ÇÁ·Î±×·¥À» ½ÇÇàÇغ¸ÀÚ.
- getText()·Î ºÒ·¯¿Â sample2°¡ HTML űױîÁö Àû¿ëµÇ¾î ½ÇÇàµÈ´Ù.
- sample3¿¡ µ¥ÀÌÅÍ°¡ ¸ÅÇεǾî Ãâ·ÂµÈ´Ù.