You are on page 1of 4

Button

Project : ButtonDemo

สําหรับโปรเจ็กนี้ เราจะแนะนําให้รู้จักกับ ButtonView โดยที่โปรเจ็กนี้ จะให้ Click ที่ปุ่ม แล้วแสดง


ข้อความว่า Hello ... จาก TextView

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:textSize = "14pt" >
</TextView>

<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Click Here!" >
</Button>

</LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

Code XML ในการจัด Layout ของ LinearLayout


orientation : อะไรก็ตามที่ใส่เข้าไปใน linear จะเรียงต่อกันตามแนวดิ่ง
width : กว้าง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
height : สูง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
gravity : อะไรก็ตามที่ อยู่ใน นี้ จะมีการจัดให้อยู่ตรงกลางของ Layout นี้
<TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:textSize = "14pt" >
</TextView>

Code XML ในการจัด Layout ของ TextView


id : มีชื่อตัวแปรว่า textView
width : กว้าง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่งในที่นี้ยังไม่มีข้อความอะไรอยู่เลย
height : สูง เท่ากับขนาดความสูงของข้อมูลที่อยู่ข้างใน ซึ่งในที่นี้ยังไม่มีมีข้อความอะไรอยู่เลย
gravity : ตําแหน่งของ TextView นี้ ให้อยู่ตรงกลางในแนว นอน
textSize : ขนาดตัวอักษร 14 pt

<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Click Here!" >
</Button>


Code XML ในการจัด Layout ของ TextView

id : มีชื่อตัวแปรว่า button
width : กว้าง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่งในที่นี้ คือ Click Here!
height : สูง เท่ากับขนาดความสูงของข้อมูลที่อยู่ข้างใน ซึ่งในที่นี้ คือ Click Here!
gravity : ตําแหน่งของ TextView นี้ ให้อยู่ตรงกลางในแนว นอน
text : เป็นการกําหนดข้อความ ในที่นี้คือ Click Here!
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ButtonDemo extends Activity {



TextView textView;
Button button;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textView = (TextView)findViewById(R.id.textView);

button = (Button)findViewById(R.id.button);
button.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Hello BOM");
}
});

}
}

TextView textView;
Button button;

Code Java ในการประกาศตัวแปร TextView และ Button

textView = (TextView)findViewById(R.id.textView);

Code Java ในการการเชื่อม ตัวแปร TextView กับ Layout ( XML )

button = (Button)findViewById(R.id.button);

Code Java ในการการเชื่อม ตัวแปร Button กับ Layout ( XML )


button.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setText("Hello BOM");
}
});

Code Java ในการกําหนดการ คลิก ปุ่ม ในที่นี้คือ เมื่อ คลิกปุ่มนี้จะแสดงคําว่า Hello BOM

drbomkung@gamil.com

You might also like