You are on page 1of 7

Restaurant Random

Project : RestaurantRandom

สําหรับโปรเจ็กนี้ เป็นการนํา ButtonView และ TextView มาใช้ด้วยกัน เมื่อคลิกที่ปุ่มแล้ว


TextView จะแสดงชื่อร้านอาหาร จากการ Random

<?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 = "70dip"
android:textSize = "10pt" >
</TextView>

<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "30dip"
android:text = "Shaker!" >
</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 = "70dip"
android:textSize = "10pt" >
</TextView>

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


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

<Button
android:id = "@+id/button"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginTop = "30dip"
android:text = "Shaker!" >
</Button>

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


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

public class RestaurantRandom extends Activity {



TextView textView;
Button button;
String list[] = {
"MK Restaurant",
"KFC",
"Chesters Grill",
"Bar-B-Q Plaza",
"The Pizza Company"
};

/** 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(getRestaurantName());
}
});

public String getRestaurantName(){


return list[ (int) Math.round( Math.random()*list.length-1 ) ];
}

TextView textView;
Button button;

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


String list[] = {
"MK Restaurant",
"KFC",
"Chesters Grill",
"Bar-B-Q Plaza",
"The Pizza Company"
};

Code Java ในการการกําหนด ตัวแปร ชนิด String แบบ Array เพื่อกําหนดรายการชื่อร้านอาหาร

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

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

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

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

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


และ กําหนดฟังก์ชัน ในการ ตรวจสอบการ คลิก ของ button คือ
เมื่อมีการ คลิกที่ปุ่มนี้ จะไปเรียกใช้ฟังก์ชัน getRestaurantName() ที่เราเขียนขึ้นมา
เพื่อให้ Random ชื่อร้านอาหาร โดยที่จะ return ค่าเป็น String
public String getRestaurantName(){
return list[ (int) Math.round( Math.random()*list.length-1 ) ];
}

Code Java ฟังก์ชัน getRestaurantName( )


การทํางานของฟังก์ชัน
เนื่องจาก ฟังกชันก์ ถูกเขียนแบบ คอมโพสิท เวลาอ่านจะ อ่านจาก ข้างนอกไปข้างใน
ส่วน เวลาหาค่า จะคิดจากข้างใน ออก มา
ในที่นี้ มีอีก 2 ฟังก์ชันก์ชัน ซึ่งอยู่ใน Package : java.lang.Math คือ
- Math.random() จะ return ค่าตั้งแต่ 0.0 ถึง 1.0
ตัวอย่าง Math.random()*2 จะได้ค่า ระหว่าง 0.0 ถึง 2.0
Math.random()*9 จะได้ค่า ระหว่าง 0.0 ถึง 9.0
- Math.round( double ) รับรับค่าเป็นตัวเลข ทศนิยม
จากนั้น ถ้าทศนิยมนั้น มากกว่า 5 จะปัดขึ้น
น้อยกว่า ก็ ปัดลง
ตัวอย่าง Math.round( 9.7 ) จะได้ค่า 10
Math.round( 9.4 ) จะได้ค่า 9
วิธีการทํางานของ ฟังก์ชัน getRestaurantName()
1. ทําการสุ่มตําแหน่งของ Array จากขนาดของ Array เอง จะได้ว่า

Math.random()*list.length-1


2. เอาค่าที่ สุ่มได้มาปัดเศษ จะได้ว่า

Math.round( Math.random()*list.length-1 )



3. เนื่อง จากค่าที่ได้เป็น Double เราจึงต้องแปลงเป็น Integer ก่อน จะได้ว่า

(int) Math.round( Math.random()*list.length-1 )


4. ต่อมาคือ การหาชื่อ ร้านอาหารจาก index ที่เราคิดมา จะได้ว่า

list[ (int) Math.round( Math.random()*list.length-1 ) ];

drbomkung@gmail.com

You might also like