Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตรวจจับ Long Press (กดค้าง) ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตการใช้ GestureDetector เพื่อตรวจจับการกดค้าง (Long Press) บนหน้าจอ

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hold touch to detect Long Press"
        android:textStyle="bold"/>

</RelativeLayout>

ขั้นตอนที่ 3: โค้ดหลัก (src/MainActivity.java)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gestureDetector = new GestureDetector(this, new GestureListener());
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
            Toast.makeText(MainActivity.this, "Long pressed", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}

ขั้นตอนที่ 4: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

การทดสอบ

รันแอป กดค้างที่หน้าจอประมาณ 500ms - จะแสดง Toast "Long pressed"

ขยายความสามารถ: Gesture อื่นๆ

SimpleOnGestureListener มีเมธอดอื่นๆ ที่น่าสนใจ:

  • onSingleTapConfirmed() - Single Tap
  • onDoubleTap() - Double Tap
  • onFling() - Swipe/Fling
  • onScroll() - Drag/Scroll
  • onDown() - กดลง (return true เพื่อรับ Event ต่อ)

ใช้กับ View เฉพาะ (แทน Whole Activity)

View myView = findViewById(R.id.myView);
myView.setOnTouchListener(new View.OnTouchListener() {
    GestureDetector detector = new GestureDetector(MainActivity.this, new GestureListener());

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return detector.onTouchEvent(event);
    }
});

วิธีทันสมัย: OnLongClickListener (ง่ายกว่า)

view.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(MainActivity.this, "Long Click!", Toast.LENGTH_SHORT).show();
        return true; // true = consume event
    }
});

เมื่อไหร่ควรใช้ GestureDetector?

  • ต้องการตรวจจับ Gesture หลายแบบพร้อมกัน (Tap, Double Tap, Fling, Long Press)
  • ต้องการควบคุม Touch Event อย่างละเอียด
  • ทำ Custom View หรือ Game

สำหรับแค่ Long Click ให้ใช้ setOnLongClickListener จะง่ายและเพียงพอ