Home > JAVA | Mobile Programming > เรามาสร้างปุ่มใน android กันดีกว่า

เรามาสร้างปุ่มใน android กันดีกว่า

May 2, 2011 No Comments    

เรามาสร้างปุ่มใน android กันดีกว่า

เรามาต่อเนื่องกันต่อสำหรับการพัฒนาแอพพลิเคชั่นบนระบบปฏิบัติการ android ซึ่งครั้งนี้จะเป็นการสร้างปุ่มในตัวโปรแกรมอย่าง่ายๆ ซึ่งการสร้างปุ่มนี้ผมจะใช้วิธีการเขียนโดย xml เป็นตัวกำหนดลักษณะของปุ่มนั้นเองเริ่มแรกให้สร้าง project ตามนี้ครับ

android

android

ซึ่งเราจะต้องสร้างไฟล์ xml และกำหนดค่าของไฟล์ xml ที่อยู่ใน resource ของตัว application ซึ่งจะอยู่ใน directory res เริ่มแรกให้เราทำการแก้ไข้ไฟล์ใน res/layout/main.xml

android

android

มันจะมีการแสดงผลเป็นแบบ Graphical Layout แต่เนื่องด้วยผมจะทำการกำหนดลักษณะปุ่มด้วย xml เลยทำการคลิกที่แถบ xml จากนั้นทำการแก้ไข้โค้ดตามโค้ดนี้เลยครับ


<?xml version=<em>"1.0"</em> encoding=<em>"utf-8"</em>?>

<LinearLayout xmlns:android=<em>"http://schemas.android.com/apk/res/android"</em>

android:orientation=<em>"vertical"</em>

android:layout_width=<em>"fill_parent"</em>

android:layout_height=<em>"fill_parent"</em>

>

<TextView

android:layout_width=<em>"fill_parent"</em>

android:layout_height=<em>"wrap_content"</em>

android:text=<em>"@string/main_title"</em>

/>

<Button

android:layout_width=<em>"fill_parent"</em>

android:layout_height=<em>"wrap_content"</em>

android:text=<em>"@string/thaicoding_label"</em>/>

<Button

android:layout_width=<em>"fill_parent"</em>

android:layout_height=<em>"wrap_content"</em>

android:text=<em>"@string/facebook_label"</em>/>

</LinearLayout>

หลังจากนั้นให้ทำการแก้ไขไฟล์ string.xml ที่อยู่ใน res/values/ โดยให้คลิกที่แถบ xml เพื่อทำการแก้ไขค่า xml หรือใครจะ add เพิ่มในแถบ Resources ก็ได้นะครับ ทำการแก้ไขไฟล์ตามนี้ครับ


<?xml version=<em>"1.0"</em> encoding=<em>"utf-8"</em>?>

<resources>

<string name=<em>"hello"</em>>Hello World, TestButton!</string>

<string name=<em>"app_name"</em>>TestButton</string>

<string name=<em>"main_title"</em>>Thaicoding.net</string>

<string name=<em>"thaicoding_label"</em>><span style="text-decoration: underline;">Thaicoding</span></string>

<string name=<em>"facebook_label"</em>><span style="text-decoration: underline;">Facebook</span></string>

</resources>

android

 

ลองกด ctrl + F11 ก็จะได้แนวนอนดังรูปครับ

android

 

ซึ่งจากโค้ด xml นั้นจะเห็นว่าเรามีการสร้าง node <Button /> ขึ้นคือการสร้างปุ่มกด 1 ปุ่มนั้นเอง และมีการกำหนดความกว้างยาวตามรูปแบบ layout ของ android ส่วนแต่ละ layout มีคุณสมบัติอย่างไรผมขอติดไว้ก่อนนะครับ ซึ่งชื่อของ Title และปุ่มนั้นจะถูกกำหนดค่าจาก resource string ที่อยู่ใน string.xml ครับ โดยใน node Button นั้นเรามีการกำหนด xml attribute คือ android:text=“@string/thaicoding_label ซึ่งจะเห็นว่าเราไปกำหนด node string ใน string.xml ให้มีค่าเป็น Thaicoding นั้นเอง

<string name=“thaicoding_label”>Thaicoding</string>

จากทั้งหมดเห็นว่า xml มีความสำคัญกับการพัฒนา android application มากนะครับแล้วพบกันใหม่บทความหน้าครับ

Download SouceCode

 

Tags: , , ,

Comments : เรามาสร้างปุ่มใน android กันดีกว่า

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>