การส่งค่าข้อมูลไปอีกหน้า เช่น การส่งชื่อ User จากหน้า login ไปยังหน้าแรก
วิธีสร้างโปรเจค
ขั้นตอนที่ 1 : เข้าโปรแกรม Android Studio สร้างโปรเจคใหม่ขึ้นมา
ขั้นตอนที่ 2 : สร้าง TextView, EditText, 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/TV01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in"
android:textSize="30sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:id="@+id/ET01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/TV01"
/>
<Button
android:id="@+id/BT01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in"
android:layout_below="@+id/ET01"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
- Screenshot_5.png (49.64 KiB) Viewed 2278 times
ขั้นตอนที่ 3 : สร้างหน้า layout ขึ้นมาอีกหน้า
วิธีสร้าง Layout + Class
- หน้า Home (ex. activity_home)
โค้ด: เลือกทั้งหมด
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#55FF99"
android:orientation="vertical"
android:padding="15dp"
>
<TextView
android:id="@+id/TV01H"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="50sp"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="@+id/BT01H"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
- Screenshot_1.png (50.79 KiB) Viewed 2278 times
ขั้นตอนที่ 4 : มาที่ MainActivity.java
เพิ่ม Method onClick
โค้ด: เลือกทั้งหมด
public void onClickGo(View obj)
{
EditText name_edt = (EditText)findViewById(R.id.ET01);
String name = name_edt.getText().toString();
Intent ingo = new Intent(this,HomeActivity.class);
ingo.putExtra("NAME_DATA",name);
startActivity(ingo);
}
- บรรทัดแรก เชื่อม EditText ใน java กับ xml
- บรรทัดที่ 2 เก็บค่า name_edt ในรูปแบบของ String
- บรรทัดที่ 3 คำสั่ง Intent เพื่อเชื่อมไปยังหน้าอื่น
- บรรทัดที่ 4 ส่งค่า "NAME_DATA" ไปยังหน้าอื่น
- บรรทัดที่ 5 คำสั่งทำงาน Activity
ขั้นตอนที่ 5 : เพิ่ม onClick ใน layout activity_main
โค้ด: เลือกทั้งหมด
<Button
android:id="@+id/BT01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in"
android:layout_below="@+id/ET01"
android:layout_centerHorizontal="true"
android:onClick="onClickGo"
/>
ขั้นตอนที่ 6 : สร้าง Class HomeActivity.java แล้ว extends Activity
โค้ด: เลือกทั้งหมด
public class HomeActivity extends Activity
จากนั้น Override Method ดูได้จาก
ที่นี่
เพิ่ม code ใน Method onCreate
โค้ด: เลือกทั้งหมด
String name = getIntent().getStringExtra("NAME_DATA");
TextView name_tv =(TextView)findViewById(R.id.TV01H);
name_tv.setText(name);
- บรรทัดแรก รับค่าในรูปแบบของ String โดยรับค่า "NAME_DATA"
- บรรทัดที่ 2 เชื่อม TextView ใน java กับ xml
- บรรทัดที่ 3 set ค่าที่รับมาในแสดงใน TextView
ขั้นตอนที่ 7 : สร้าง Method onClick เพื่อไปยังหน้าแรก
โค้ด: เลือกทั้งหมด
public void onClickOk(View obj)
{
Button btn_ok = (Button)findViewById(R.id.BT01H);
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
}
ขั้นตอนที่ 8 : เพิ่ม onClick ใน layout activity_home
โค้ด: เลือกทั้งหมด
<Button
android:id="@+id/BT01H"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_gravity="center_horizontal"
android:onClick="onClickOk"
/>
ขั้นตอนที่ 9 : เพิ่ม Activity ใน manifest
ทดสอบ
- Screenshot_2.png (30.31 KiB) Viewed 2278 times
- Screenshot_3.png (33.45 KiB) Viewed 2278 times
เสร็จสิ้นการส่งข้อมูลไปยังอีกหน้าด้วยการ Intent