[Android Studio Dev] การส่งข้อมูลไปยังอีกหน้าด้วยการ Intent

Mobile Application Developing- Android, iOS, Window Phone สอนเขียนโปรแกรมบนมือถือ ระบบปฏิบัติการต่าง แอนดรอยด์ ไอโอเอส วินโดโฟน สอนเขียนโปรแกรมบนมือถือ

Moderator: mindphp, ผู้ดูแลกระดาน

prakasit.bank
PHP Super Member
PHP Super Member
โพสต์: 316
ลงทะเบียนเมื่อ: 02/06/2015 9:47 am

[Android Studio Dev] การส่งข้อมูลไปยังอีกหน้าด้วยการ Intent

โพสต์ที่ยังไม่ได้อ่าน โดย prakasit.bank »

การส่งค่าข้อมูลไปอีกหน้า เช่น การส่งชื่อ 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
Screenshot_5.png (49.64 KiB) Viewed 2276 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_3.png
Screenshot_3.png (33.45 KiB) Viewed 2276 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

โค้ด: เลือกทั้งหมด

android:onClick="onClickGo"

โค้ด: เลือกทั้งหมด

<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

โค้ด: เลือกทั้งหมด

android:onClick="onClickOk"

โค้ด: เลือกทั้งหมด

<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

โค้ด: เลือกทั้งหมด

<activity android:name=".HomeActivity"/>
ทดสอบ
Screenshot_2.png
Screenshot_2.png (30.31 KiB) Viewed 2276 times
Screenshot_1.png
Screenshot_1.png (50.79 KiB) Viewed 2276 times
เสร็จสิ้นการส่งข้อมูลไปยังอีกหน้าด้วยการ Intent
รูปภาพ
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 24