วิธีสร้างโปรเจค
ขั้นตอนที่ 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>
วิธีสร้าง 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>
เพิ่ม 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
โค้ด: เลือกทั้งหมด
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"
/>
โค้ด: เลือกทั้งหมด
public class HomeActivity extends Activity
เพิ่ม 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
โค้ด: เลือกทั้งหมด
public void onClickOk(View obj)
{
Button btn_ok = (Button)findViewById(R.id.BT01H);
Intent intent = new Intent(HomeActivity.this,MainActivity.class);
startActivity(intent);
}
โค้ด: เลือกทั้งหมด
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"
/>
โค้ด: เลือกทั้งหมด
<activity android:name=".HomeActivity"/>
เสร็จสิ้นการส่งข้อมูลไปยังอีกหน้าด้วยการ Intent