iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 20
0

前言

此篇功能只有講述如何在單一 View 上偵測點擊事件,若 View 上有其他元件他將會偵測不到

操作

第一步:在布局文件創建一個 View

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

  <View
      android:id="@+id/myView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

第二步:製作備按下時的偵測及事件

package com.example.testr;
import android.app.Activity;
import android.os.Build;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import androidx.annotation.RequiresApi;
public class MainActivity extends Activity implements View.OnClickListener {
    private VelocityTracker mVelocityTracker = null;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        int pointerId = event.getPointerId(index);
        switch(action) {
       //當被按下時
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                //將速動追蹤器的速動歸0      
                    mVelocityTracker.clear();
                }
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
            //當被按下並移動時
                mVelocityTracker.addMovement(event);
                //當按下時呼叫 computeCurrentVelocity()和 getYVelocity()
                mVelocityTracker.computeCurrentVelocity(1000);
                Log.d("X velocity: " + mVelocityTracker.getXVelocity(pointerId));
                Log.d("Y velocity: " + mVelocityTracker.getYVelocity(pointerId));
                break;
            case MotionEvent.ACTION_UP:
            //當手指離開時
            case MotionEvent.ACTION_CANCEL:
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onClick(View view) {
    //偵測被按下的事件
        view.requestPointerCapture();
    }

}

完整程式碼▼▼▼

package com.example.testr;
import android.app.Activity;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;

import androidx.annotation.RequiresApi;
public class MainActivity extends Activity implements View.OnClickListener {

    private VelocityTracker mVelocityTracker = null;
    private Button Copyplace;
    private int pointerId;
    float x,y;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = event.getActionIndex();
        int action = event.getActionMasked();
        pointerId = event.getPointerId(index);
        Copyplace=findViewById(R.id.Copyplace);
        Copyplace.setOnClickListener(this);
        switch(action) {
            //當被按下時
            case MotionEvent.ACTION_DOWN:
                if(mVelocityTracker == null) {
                    mVelocityTracker = VelocityTracker.obtain();
                }
                else {
                    //將速動追蹤器的速動歸0
                    mVelocityTracker.clear();
                }
                mVelocityTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                //當被按下並移動時
                mVelocityTracker.addMovement(event);
                //當按下時呼叫 computeCurrentVelocity()和 getYVelocity()
                mVelocityTracker.computeCurrentVelocity(1000);
                Log.d("TAG", "X velocity: " + mVelocityTracker.getXVelocity(pointerId));
                Log.d("TAG", "Y velocity: " + mVelocityTracker.getYVelocity(pointerId));
                break;
            case MotionEvent.ACTION_UP:
                //當手指離開時
            case MotionEvent.ACTION_CANCEL:
                mVelocityTracker.recycle();
                break;
        }
        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onClick(View view) {
        view.requestPointerCapture();}

   }
  

上一篇
第19篇:RecyclerView
下一篇
第21篇:自動點擊
系列文
Android的30天學習歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言