20.11.2016 Views

Android App Development ฉบับสมบูรณ์

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

จากการเพิ่มโค้ด XML ดังกล่าวได้ผลลัพธ์ดังนี้<br />

เขียนโค้ดกำหนดการทำงานของแอพพลิเคชัน<br />

เมื่อผู้อ่านสร้างหน้าจอแอพพลิเคชันเรียบร้อยแล้ว ขั้นตอนต่อไปคือ การเขียนโค้ดกำหนดการทำงาน<br />

ของแอพพลิเคชันในไฟล์ ChangeBathActivity.java เพื่อใช้ในการแปลงค่าระยะทาง โดยมีขั้นตอนดังต่อไปนี้<br />

1. เปิดไฟล์ ChangeBathActivity.java ซึ่งมีโค้ดดังนี้<br />

2. แก้ไขโค้ดในไฟล์ ChangeBathActivity.java ดังนี้<br />

package com.app.changebath;<br />

import android.app.Activity;<br />

import android.os.Bundle;<br />

import android.view.View;<br />

import android.widget.EditText;<br />

import android.widget.RadioButton;<br />

import android.widget.Toast;<br />

public class ChangeBath extends Activity {<br />

private EditText text;<br />

/** Called when the activity is first created. */<br />

public void onCreate(Bundle savedInstanceState) {<br />

super.onCreate(savedInstanceState);<br />

setContentView(R.layout.main);<br />

text = (EditText) findViewById(R.id.EditText01);<br />

}<br />

public void myClickHandler(View view) {<br />

switch (view.getId()) {<br />

case R.id.Button01:<br />

กรณีกดปุ่ม<br />

RadioButton milesButton = (RadioButton)<br />

findViewById(R.id.RadioButton01);<br />

1<br />

82<br />

<strong>Android</strong> <strong>App</strong> <strong>Development</strong> <strong>ฉบับสมบูรณ์</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!