Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Android Tutorials - สอนเขียน Android App ฟรี เขียนโปรแกรมแอนดรอยด์บน SmartPhone / Tablets > เรียกใช้งาน string.xml บน android ไฟล์จัดเก็บค่า string / array ตัวแปรในรูปแบบต่าง ๆ



Clound SSD Virtual Server

เรียกใช้งาน string.xml บน android ไฟล์จัดเก็บค่า string / array ตัวแปรในรูปแบบต่าง ๆ

รู้จักและเรียกใช้งาน string.xml บน android ในการพัฒนาโปรแกรม Android เราจะพบเห็นไฟล์ที่มีชื่อว่า string.xml อยู่เป็นประจำ สำหรับไฟล์ string.xml ถูกจัดเก็บไว้ใน path ที่มีชื่อว่า /res/values/string.xml โดยเป็นชุดของ string ที่ทำหน่้าที่จัดเก็บชื่อตัวแปร และค่าตัวแปร ที่จะสามารถนำค่าเหล่านั้นไปใช้ได้ในทุก ๆ ส่วนของโปรแกรม โดยสามารถเรียกได้จาก XML Layout และ Java Runtime นิยมใช้จัดเก็บค่าตัวแปรคงที่ และจะต้องเรียกใช้งานบ่อย ๆ เหตุผที่จัดเก็บค่าตัวแปรบางตัวลงในไฟล์ string.xml ก็คือ ลดความซ้ำซ้อนค่าของตัวแปรบางตัว เวลาที่จะมาแก้ไขเพิ่มลดค่า ก็สามารถทำได้อย่างง่ายดาย


Android and string.xml

โครงสร้างของไฟล์ถูกจดัเก็บไว้ใน /res/values/string.xml


ในโครงสร้างของไฟล์ string.xml ถูกจัดเก็บให้อยู่ในรูปแบบของ string แบบง่าย ๆ ที่จะสามารถเรียกใช้งานได้ง่าย ๆ เช่นเดียวกัน เช่น

รูปแบบของ String
<string name="app_name">MyApp</string>

ตัวอย่างแรกเป็นการกำหนดประเภา String มีชื่อตัวแปรว่า app_name และค่าตัวแปร MyApp

รูปแบบของ Array
    <string-array name="country_array">
        <item>Belgium</item>
        <item>France</item>
        <item>Italy</item>
        <item>Germany</item>
        <item>Spain</item>
        <item>Thailand</item>
        <item>Taiwan</item>
    </string-array>

ตัวอย่างแรกเป็นการกำหนดประเภา Arrayมีชื่อตัวแปรว่า country_array และค่าตัวแปรที่ประกอบด้วยสมาชิกอยู่ 8 ตัว คือ Belgium,France,Italy,Germany,Spain,Thailand,Taiwan

มาดูโครงสร้างและรายละเอียดของไฟล์ string.xml

Android and string.xml

ไฟล์ string.xml สามารถดูได้จากมุมมองของ Graphic ซึ่งจะสามารถกำหนดค่าผ่าน GUI ได้แบบง่าย ๆ

Android and string.xml

หรือจะดูมุมมองของ XML ก็ทำได้เช่นเดียวกัน

ทดสอบการสร้างตัวแปรและค่าตัวแปร
เราจะมาลองทดสอบการเพิ่มค่าตัวแปรบนไฟล์ string.xml และการเรียกใช้ค่าตัวแปรบน XML Layout และ Java Runtime

Android and string.xml

โดยเพิ่มตัวแปรชื่อว่า my_web มีค่าเป็น www.ThaiCreate.Com ดังรูป

<sring name="my_web">www.ThaiCreate.Com</sring>









กลับมายังไฟล์ activity_main.xml

Android and string.xml

ลาก Widgets ของ TextView มาใส่ Activity Form ของ Android จำนวน 2 ตัว ดังภาพ

Android and string.xml

เมื่อดูในมุมมองของ XML จะเห็นว่าตอนนนี้มีค่า Text เป็นค่า Default ว่า TextView

Android and string.xml

เราจะต้องกำหนดค่า TextView ตัวแรกให้มีค่า text จากค่าตัวแปร my_web ที่มีอยู่บนไฟล์ string.xml จะสามารถกำหนดและเรียกได้ดังรูป

android:text="@string/my_web"



Android and string.xml

เมื่อดูในมุมมองของ Graphical Layout ก็จะเห็นค่า text ได้เปลี่ยนแปลงตามไฟล์ string.xml ของตัวแปร my_web

ส่วน TextView ที่สองเราจะกำหนดค่าใน Java Runtime ตามตัวอย่าง

        // textView2
        final Button txtV2 = (Button)findViewById(R.id.textView2); 
        txtV2.setText(getResources().getString(R.string.my_web));


Code ทั้งหมด

Example 1 เรียกใช้ค่า string.xml แบบง่าย ๆ ในรูปแบบของ String

string.xml
<resources>
    <string name="app_name">MyApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">My App V 1.0</string>
  	<sring name="my_web">www.ThaiCreate.Com</sring>
    
</resources>


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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="@string/my_web" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:text="TextView" />

</RelativeLayout>


MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.app.Activity;


public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // textView2
        final Button txtV2 = (Button)findViewById(R.id.textView2); 
        txtV2.setText(getResources().getString(R.string.my_web));
        
    }   
    
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


Screenshot

Android and string.xml

จากตัวอย่างจะเห็นว่า TextView ทั้ง 2 ตัวจะได้ค่าที่เหมือนกัน แต่แตกต่างกันที่เรียกใช้งานจาก XML Layout กับ Java Runtime









Example 2 ไฟล์ string.xml กับค่าตัวแปรแบบ Array และการเรียกใช้ Array
ในตัวอย่างนี้เราจะสร้างตัวแปร Array ขึ้นมา 1 ชุด และใช้ ListView มาแสดงข้อมูลเหล่านั้น

string.xml
<resources>
    <string name="app_name">MyApp</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">My App V 1.0</string>
  	<string name="my_web">www.ThaiCreate.Com</string>
    
    <string-array name="country_array">
        <item>Belgium</item>
        <item>France</item>
        <item>Italy</item>
        <item>Germany</item>
        <item>Spain</item>
        <item>Thailand</item>
        <item>Taiwan</item>
    </string-array>
  	
</resources>

มีชื่อตัวแปรว่า country_array และค่าตัวแปรที่ประกอบด้วยสมาชิกอยู่ 8 ตัว คือ Belgium,France,Italy,Germany,Spain,Thailand,Taiwan

Android and string.xml

ไฟล์ XML Layout ของ activity_main.xml

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" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>


Android and string.xml

MainActivity.java
package com.myapp;

import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;


public class MainActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // listView1
        final ListView lsView = (ListView)findViewById(R.id.listView1); 
       
        String[] mTestArray;
        ArrayAdapter<String> adapter;

        mTestArray = getResources().getStringArray(R.array.country_array);  
        
        adapter = new ArrayAdapter<String>(
        	    this,
        	    android.R.layout.simple_list_item_1,
        	    mTestArray);
        
        lsView.setAdapter(adapter);
    }   
    
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}


สามารถเรียกชุดของค่าตัวแปรได้ง่าย ๆ ด้วย

mTestArray = getResources().getStringArray(R.array.country_array);  


Screenshot

Android and string.xml

ข้อมูลลจาก Array ถูกแสดงผลบน ListView



   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-07-04 06:32:48 / 2017-03-26 20:29:35
  Download : No files
 Sponsored Links / Related

 
Android กับ Intent ควบคุมการแสดงและซ่อน (Show/Hide) ของ Activity form ต่าง ๆ
Rating :

 
Android การใส่ Effect ให้กับ Intent ใน Activity ระหว่างการเปลี่ยน Activity
Rating :

 
Android กับ setContentView() ควบคุมการแสดงผล XML Layout ของแต่ล่ะ Activity
Rating :

 
Android Rotate Switching Portrait and Landscape - Emulator (มุมมองในแนวตั้งและแนวนอน)
Rating :

 
Android Enabled/Disabled Fix Portrait or Landscape Orientation Mode
Rating :

 
Android Change Activity Layout when Switching Portrait and Landscape
Rating :

 
Android Display FullScreen / No Title Bar
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่