我创建了一个获取触摸位置的应用,现在我想让它点击这个地方..
以下是代码:
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;
}
}
}
我只想以编程方式触摸这个地方..
我该怎么办?
答案 0 :(得分:4)
您需要使用一些视图来注册您的触摸侦听器。我选择了默认的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);
}
}
希望这适用于你。
编辑:
<强>布局/ 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"/>
答案 1 :(得分:0)
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;
}
}
答案 2 :(得分:0)
模拟触摸事件:
IWindowManager mWm = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
//Here you'll need to create a touch event using obtain method of TouchEvent.
//Check this : http://stackoverflow.com/questions/5867059/android-how-to-create-a-motionevent
//Once you have the motion event,m, just use it to simulate click.
mWm.injectPointerEvent(m, false);
答案 3 :(得分:0)
您的问题有点令人困惑,但如果您想听取触摸事件而不管任何视图,即在Activity
的整个屏幕上,那么您可以覆盖
public boolean dispatchTouchEvent(MotionEvevt me){
// Dispatch this touch to a View of your choice as follows
mView.dispatchTouchEvent(me);
}
在SubActivity
课程上。它将为您提供设备屏幕上触摸的绝对x,y位置。