Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách mô phỏng sự kiện chạm tại vị trí xác định trong Android

Giới thiệu

Ví dụ này minh họa cách xử lý và mô phỏng sự kiện chạm (touch event) tại vị trí xác định trong Android bằng cách override onTouchEvent().

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Thiết kế giao diện (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"
    android:padding="4dp"
    tools:context=".MainActivity">
</RelativeLayout>

Bước 3: Viết code MainActivity.java

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

public class MainActivity extends AppCompatActivity {
    private boolean isTouch = false;

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(this, "ACTION_DOWN - X: " + x + " Y: " + y, Toast.LENGTH_SHORT).show();
                isTouch = true;
                break;

            case MotionEvent.ACTION_MOVE:
                Toast.makeText(this, "MOVE - X: " + x + " Y: " + y, Toast.LENGTH_SHORT).show();
                break;

            case MotionEvent.ACTION_UP:
                Toast.makeText(this, "ACTION_UP - X: " + x + " Y: " + y, Toast.LENGTH_SHORT).show();
                break;

            case MotionEvent.ACTION_CANCEL:
                Toast.makeText(this, "ACTION_CANCEL", Toast.LENGTH_SHORT).show();
                break;
        }
        return true; // Trả về true để nhận tiếp các event MOVE, UP
    }
}

Để mô phỏng touch theo chương trình (programmatically):

// Cần quyền android.permission.INJECT_EVENTS (chỉ system apps)
// Cách 1: Sử dụng Instrumentation (chỉ test)
Instrumentation instrumentation = new Instrumentation();
instrumentation.sendPointerSync(MotionEvent.obtain(
    SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
    MotionEvent.ACTION_DOWN, 100, 100, 0));
instrumentation.sendPointerSync(MotionEvent.obtain(
    SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
    MotionEvent.ACTION_UP, 100, 100, 0));

// Cách 2: Root shell (cần root)
// Runtime.getRuntime().exec("input tap 100 200");

Bước 4: Cấu hình AndroidManifest.xml

<?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>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Chạm vào màn hình để xem tọa độ và loại sự kiện.

Cách mô phỏng sự kiện chạm tại vị trí xác định trong Android

Cách mô phỏng sự kiện chạm tại vị trí xác định trong Android