1. ไปสร้างปุ่มไว้ 1 ปุ่ม ที่ไฟล์ main.xml อยู่ที่ res/layout/main.xml
โค้ด: เลือกทั้งหมด
<?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"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Open"
/>
</LinearLayout>
แต่งหน้าได้ตามใจชอบ
3. สร้างคลาสขึ้นมาใหม่อีกไฟล์ ที่สืบทอดจาก Activity ด้วย วิธีการง่ายๆคือ ก็อบปี้ไฟล์ .java แล้ว paste ลงไปที่เดิม กำหนดชื่อไฟล์ สมมุติผมใช้ชื่อไฟล์ใหม่ว่า newpage.java นะครับ
ชื่อไฟล์จะตรงกับชื่อคลาสในไฟล์
4. เปิดไฟล์ AndroidMainfest.xml ขึ้นมา เปิดไปที่หน้าดูโค้ดนะครับ ให้เราสร้าง activity เพิ่มขึ้นมา เพราะว่าเราสร้างหน้าใหม่แล้ว โดยชื่อ activity ให้ใช้ว่า .ชื่อคลาส ดังนี้
โค้ด: เลือกทั้งหมด
<activity
android:name=".mainpage"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".newpage" android:theme="@android:style/Theme.Light">
</activity>
โค้ด: เลือกทั้งหมด
android:theme="@android:style/Theme.Light"
เปลี่ยนตรง Theme.Light เป็นแบบอื่นได้ เช่น
android:theme="@android:style/Theme.Dialog" แสดงเป็นแบบป๊อบอัพ
โค้ด: เลือกทั้งหมด
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class mainpage extends Activity{
Button bt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //กำหนด layout หลัก
bt = (Button)findViewById(R.id.button1);
sensorlist.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(mainpage.this,newpage.class);
startActivity(i);
}
});
}
}
โค้ด: เลือกทั้งหมด
import android.app.Activity;
import android.os.Bundle;
public class newpage extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newpage); //กำหนด layout หลัก
}
}