Toast คือการแสดงข้อความแจ้งเตือนผ่าน Popup ที่ลอยอยู่เหนือ Activity ปัจจุบัน โดยที่ Activity ปัจจุบันนั้นยังคงทำงานอยู่อย่างปกติ โดยจะแสดงให้เห็นถึงการเขียนคำสั่งภายใน Class Activity มาเริ่มสร้างข้อความ Toast กันเลย
วิธีสร้างโปรเจค
ขั้นตอนที่ 1 : สร้างปุ่มขึ้นมา 1 ปุ่ม
โค้ด: เลือกทั้งหมด
<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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="@+id/button01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
- Screenshot_1.png (47.31 KiB) Viewed 1950 times
ขั้นตอนที่ 2 : มาที่ MainActivity.java สร้าง Method onclick และเชื่อมต่อกับปุ่มใน xml
โค้ด: เลือกทั้งหมด
public void onClickToast(View view){
Button btn_next = (Button)findViewById(R.id.button01);
}
ขั้นตอนที่ 3 : สร้าง Toast ข้อความข้างใน Method
โค้ด: เลือกทั้งหมด
public void onClickToast(View view){
Button btn_next = (Button)findViewById(R.id.button01);
Toast.makeText(getApplicationContext(),"Test Toast",Toast.LENGTH_LONG).show();
}
ตรง " " ให้เราใส่ข้อความที่จะให้แสดงเป็น pop up (ex. "Test Toast")
- Toast.LENGTH_LONG = แสดงข้อความ Toast ยาว
- Toast.LENGTH_SHORT = แสดงข้อความ Toast สั้น
ขั้นตอนที่ 4 : ใส่ onclick ของปุ่มใน xml
จะได้ตามนี้
โค้ด: เลือกทั้งหมด
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="@+id/button01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClickToast"
/>
ทดสอบ
- Screenshot_2.png (21.62 KiB) Viewed 1950 times
- Screenshot_3.png (22.92 KiB) Viewed 1950 times
เสร็จสิ้นการสร้างข้อความ Toast ตอนกด Button
Toast คือการแสดงข้อความแจ้งเตือนผ่าน Popup ที่ลอยอยู่เหนือ Activity ปัจจุบัน โดยที่ Activity ปัจจุบันนั้นยังคงทำงานอยู่อย่างปกติ โดยจะแสดงให้เห็นถึงการเขียนคำสั่งภายใน Class Activity มาเริ่มสร้างข้อความ Toast กันเลย
[url=https://www.mindphp.com/forums/viewtopic.php?f=30&t=28230]วิธีสร้างโปรเจค[/url]
[b]ขั้นตอนที่ 1 : [/b]สร้างปุ่มขึ้นมา 1 ปุ่ม
[code]<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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="@+id/button01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>[/code]
[attachment=2]Screenshot_1.png[/attachment]
[b]ขั้นตอนที่ 2 : [/b]มาที่ MainActivity.java สร้าง Method onclick และเชื่อมต่อกับปุ่มใน xml
[code]public void onClickToast(View view){
Button btn_next = (Button)findViewById(R.id.button01);
}[/code]
[b]ขั้นตอนที่ 3 : [/b]สร้าง Toast ข้อความข้างใน Method
[code]public void onClickToast(View view){
Button btn_next = (Button)findViewById(R.id.button01);
Toast.makeText(getApplicationContext(),"Test Toast",Toast.LENGTH_LONG).show();
}[/code]
ตรง " " ให้เราใส่ข้อความที่จะให้แสดงเป็น pop up (ex. "Test Toast")
[list]
[*]Toast.LENGTH_LONG = แสดงข้อความ Toast ยาว
[*]Toast.LENGTH_SHORT = แสดงข้อความ Toast สั้น[/list]
[b]ขั้นตอนที่ 4 : [/b]ใส่ onclick ของปุ่มใน xml
[code]android:onClick="onClickToast"[/code]
จะได้ตามนี้
[code]<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toast"
android:id="@+id/button01"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onClickToast"
/>[/code]
[b][u]ทดสอบ[/u][/b]
[attachment=1]Screenshot_2.png[/attachment]
[attachment=0]Screenshot_3.png[/attachment]
[b][size=150][color=#40BF00]เสร็จสิ้นการสร้างข้อความ Toast ตอนกด Button[/color][/size][/b]