gpt4 book ai didi

android - 使用 onTouch 获取触摸位置,然后以编程方式触摸此位置

转载 作者:行者123 更新时间:2023-11-29 16:01:25 25 4
gpt4 key购买 nike

我做了一个获取触摸位置的应用程序,现在我想让它点击这个地方..

代码如下:

public class MainActivity extends Activity {
@Override
//OnCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//GetTouch location
@Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(this, "Location Located",Toast.LENGTH_SHORT).show();
int eventaction = event.getAction();
Point point = new Point();
point.x = (int) event.getX();
point.y = (int) event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
break;
}
return false;
}
}
}

我只希望它以编程方式触摸这个地方..

我该怎么做?

最佳答案

您需要使用一些 View 来注册您的触摸监听器。我选择了默认的 RelativeLayout。我将回调点击的延迟设置为 2 秒。

请注意,您需要保存最后一次模拟触摸 (simulationEvent) 并检查 onTouchListener 内部是否不是该事件。如果没有此检查,您会一遍又一遍地在同一个地方重复点击,因为模拟会自行回调。

如果只想注册模拟触摸,将onTouchListener的最后一个返回值改为true

package com.example.virtualclicker;

import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

/**
* Created by Simon on 2014 Jul 09.
*/
public class MainActivity extends ActionBarActivity {

private MotionEvent simulationEvent;

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

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myLayout);

relativeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event == simulationEvent)
return false;
int action = event.getAction();
int x = (int)event.getX();
int y = (int)event.getY();
Log.e("onTouchListener", "User touch at X:" + x + " Y:" + y);
long length = 0;
if (action == MotionEvent.ACTION_DOWN) {
click(v, x, y);
}
return false;
}
});
}

public void click(final View view, final int x, final int y) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// SIMULATION BEGINS HERE AFTER 2000 Millis ///
long touchTime = SystemClock.uptimeMillis();
simulationEvent = MotionEvent.obtain(touchTime, touchTime,
MotionEvent.ACTION_DOWN, x, y, 0);
view.dispatchTouchEvent(simulationEvent);

simulationEvent = MotionEvent.obtain(touchTime, touchTime,
MotionEvent.ACTION_UP, x, y, 0);
view.dispatchTouchEvent(simulationEvent);
Log.e("simulatedTouch","simulated touch executed at X:"+x+" Y:"+y);
}
}, 2000);
}

}

希望这对你有用。

编辑:

layout/activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>

关于android - 使用 onTouch 获取触摸位置,然后以编程方式触摸此位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24460696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com