อ่าน: 2665
ความเห็น: 3
ความเห็น: 3
ยืนยันการออกจากแอปพลิเคชัน เมื่อกดปุ่ม Back ใน Android app
Android App (Back btn)
สวัสดีครับวันนี้มี Code มาฝากอีกแล้ว กับ Android code กับการทำ Dialog ยืนยันการออก/ปิดแอปพลิเคชัน
ผู้ที่หลงเข้ามาอ่านน่าจะเคยเห็นกันมากันบ้างล่ะนะ โดยเฉพาะแอปพลิเคชันจำพวกเกมส์ จะใช้กันเยอะมาก
สำหรับบทความนี้จะใช้วิธีอย่างง่าย คือดักการกดปุ่ม Back ที่หน้าแรกสุด เมื่อผู้ใช้กดปุ่ม Back ที่หน้าแรกสุดก็จะแสดง Dialog เพื่อให้ยืนยันทันที ถ้าเลือก Yes ก็จะใช้คำสั่งปิดแอปพลิเคชัน ถ้า No ก็จะยกเลิก Dialog เฉยๆ
สำหรับปุ่ม Back จะรู้ได้ว่าผู้ใช้กดปุ่ม Back ได้ด้วยฟังก์ชัน onBackPressed
สำหรับปุ่ม Back จะรู้ได้ว่าผู้ใช้กดปุ่ม Back ได้ด้วยฟังก์ชัน onBackPressed
public void onBackPressed() {
// กดปุ่ม Back แล้วจะให้โปรแกรมทำอะไรให้ใส่ในนี้
}
เนื่องจากอยากให้ขึ้น Dialog เพื่อถามผู้ใช้ ดังนั้นในนี้ก็ต้องใช้คำสั่ง Dialog โดยตัวอย่างนี้ใช้แค่ Dialog ธรรมดาๆ มีปุ่ม Yes กับ No ให้กด ถ้าผู้ที่หลงเข้ามาอ่านจะเปลี่ยนเป็น Dialog อย่างอื่นก็ใช้ Custom Dialog ได้ เพราะสำคัญอยู่แค่ว่ากดปุ่มหนึ่งเพื่อออกจากแอปพลิเคชัน กับอีกปุ่มยกเลิก
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Exit");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setCancelable(true);
dialog.setMessage("Do you want to exit?");
dialog.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
dialog.setNegativeButton("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
ในตัวอย่างนี้จะกำหนดหัวข้อ Dialog ว่า Exit มีไอคอนแสดงด้วยเป็นไอคอนแอพน่ะแหละเอามาแก้ขัด และแสดงข้อความว่า Do you want to exit? และกำหนดให้ผู้ใช้แตะนอกเหนือจาก Dialog เพื่อยกเลิกได้ สำหรับปุ่ม Yes จะเป็นปุ่มแบบ Positive Button จะใช้คำสั่ง finish ส่วน No จะเป็นปุ่มแบบ Negative Button จะใช้คำสั่ง dialog.cancel
ดังนั้นโค๊ดในบทความนี้ก็จะมีเท่านี้นี่แหละ สำหรับ onBackPressed จะอยู่บริเวณเดียวกันกับ onCreate, onPause, onResume และ onStop เลย
ดังนั้นโค๊ดในบทความนี้ก็จะมีเท่านี้นี่แหละ สำหรับ onBackPressed จะอยู่บริเวณเดียวกันกับ onCreate, onPause, onResume และ onStop เลย
Main.java
package app.akexorcist.dialogclosewarning;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
public class Main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onBackPressed() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Exit");
dialog.setIcon(R.drawable.ic_launcher);
dialog.setCancelable(true);
dialog.setMessage("Do you want to exit?");
dialog.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
dialog.setNegativeButton("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
}
ส่วน Layout ก็ไม่มีอะไร เพราะในตัวอย่างนี้ใช้ Dialog พื้นฐาน
main.xml
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="app.akexorcist.dialogclosewarning"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="app.akexorcist.dialogclosewarning.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ทีนี้ก็ลองทดสอบดูเลย เปิดแอปพลิเคชันขึ้นมาแล้วกดปุ่ม Back ดู ก็จะมี Dialog แสดงขึ้นมาให้ยืนยันอีกที ถ้ากด Yes ก็จะปิดแอปพลิเคชัน และถ้ากด No หรือข้างนอกพื้นที่ Dialog ก็จะเป็นการยกเลิก
สร้าง: 19 มิถุนายน 2557 18:31
แก้ไข: 19 มิถุนายน 2557 18:31
[ แจ้งไม่เหมาะสม ]
บันทึกอื่นๆ
- เก่ากว่า « วิ่ง วิ่ง วิ่ง
- ใหม่กว่า » การเลือกไฟล์ภาพจาก Gallery ด้วย ...
20 มิถุนายน 2557 08:29
#98735
น้าฝากตรวจการบ้านให้ที อิอิอิ