ขั้นตอนที่ 1 : เข้าโปรแกรม Android Studio สร้างโปรเจคใหม่ขึ้นมา
ขั้นตอนที่ 2 : สร้าง ปุ่ม Button ขั้นมาในหน้า activity_main เป็นหน้าแรก
โค้ด: เลือกทั้งหมด
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<TextView
android:id="@+id/textview01"
android:text="PAGE 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="30sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Page"
android:id="@+id/button01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/textview01"
/>
</RelativeLayout>
วิธีสร้างหน้า layout ใหม่
คลิกขวาที่ layout เลือก New > XML > Layout XML File
ตั้งชื่อไฟล์ XML ส่วน Root Tag ให้เป็น Linear Layout เหมือนเดิมก็ได้ สามารถเปลี่ยนทีหลังได้
code หน้า 2
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textview02"
android:text="PAGE 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="30sp" />
</RelativeLayout>
- ให้เราสร้าง Java Class ขึ้นมาอีก 1 Class เป็น Class ที่เอาไว้เชื่อมต่อกับไฟล์ XML หน้า 2
วิธีสร้าง Java Class
คลิกขวาที่ package เลือก New > Java Class
- ตั้งชื่อ Class (ex. Page2Activity)
- หลังจากสร้าง Class ให้ extents Activity
โค้ด: เลือกทั้งหมด
public class Page2Activity extends Activity
- แล้วเลือก onCreate กด OK แล้วจะได้ code ตามนี้
โค้ด: เลือกทั้งหมด
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
โค้ด: เลือกทั้งหมด
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
}
ขั้นตอนที่ 6 : มาที่ MainActivity.java
สร้าง Method onClick ขึ้นมาเพื่อให้หน้า Main ไปยังหน้า Page2
จะได้ code ตามนี้
โค้ด: เลือกทั้งหมด
public void onClickNext(View view){
Button btn_next = (Button)findViewById(R.id.button01);
Intent intent = new Intent(MainActivity.this,Page2Activity.class);
startActivity(intent);
}
- บรรทัดแรก ชื่อ Method ตั้งชื่ออะไรก็(ex. onClickNext) เรียกใช้ View และตัวแปร ตั้งอะไรก็ได้ (ex. view)
- บรรทัดที่ 2 เรียกใช้ Button เพื่อให้เชิ่อมต่อปุ่มในหน้า xml โดย R.id.button01 เราต้องตั้ง id button01 ในหน้า xml
- บรรทัดที่ 3 เรียกใช้ Intent เป็นคำสั่งไปยังอีกหน้า (MainActivity.this = หน้าปัจจุบัน ,Page2Activity.class = หน้าที่จะไป );
ขั้นตอนที่ 7 : เราต้องไปเพิ่ม Activity ใน manifest มาที่โฟลเดอร์ minifest
ให้เพิ่ม Activity Page2Activity เข้าไป ต่อจากปีกกาปิดของ Activity
โค้ด: เลือกทั้งหมด
<activity android:name=".Page2Activity"/>
โค้ด: เลือกทั้งหมด
android:onClick="onClickNext"
โค้ด: เลือกทั้งหมด
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Page"
android:id="@+id/button01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_below="@id/textview01"
android:onClick="onClickNext"
/>
หน้าแรก
หน้าที่สอง
เสร็จสิ้นการเชื่อมต่อไปยังอีกหน้าด้วยการ Intent