手势检测器不工作
我有一组java代码,我尝试检测用户的触摸手势。当用户进行简单的触摸/向下滑动等操作时,文本视图将显示用户当前所做的操作。但是,当我在模拟器上运行代码时,它只是一个黑屏,显示 Hello World!当我触摸时,没有任何显示。为什么会这样?附件是代码。感谢您的帮助...
package org.tp.iit.cds.BrailleTypeSend;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.widget.TextView;
import android.graphics.Color;
public class BrailleSend extends Activity implements OnGestureListener {
/** Called when the activity is first created. */
public LinearLayout main;
public TextView viewA;
public GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.WHITE);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
main.addView(viewA);
setContentView(R.layout.main);
}
@Override
public boolean onDown(MotionEvent e) {
viewA.setText("Down Stroke");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
viewA.setText("tap");
return true;
}
}
I have a set of java code which i try to detect touch gestures by users. When a user do a simple touch/swipe down etc, a text view will display out what the user has currently done. However when i run the code on my emulator, it is just a black screen which show Hello World! and when i do a touch, nothing is display.. why is that so? Attached is the code. Thanks for your help...
package org.tp.iit.cds.BrailleTypeSend;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.widget.TextView;
import android.graphics.Color;
public class BrailleSend extends Activity implements OnGestureListener {
/** Called when the activity is first created. */
public LinearLayout main;
public TextView viewA;
public GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.WHITE);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
main.addView(viewA);
setContentView(R.layout.main);
}
@Override
public boolean onDown(MotionEvent e) {
viewA.setText("Down Stroke");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
viewA.setText("tap");
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
三件事:
您的 Activity 的视图层次结构来自布局文件夹中的 main.xml 文件,因为您在代码中编写了:
setContentView(R.layout.main);
。您尚未将 GestureListener 附加到任何东西。您期望如何调用回调?
Three things:
The View heirarchy of your activity comes from the main.xml file in your layout folder since you've written:
setContentView(R.layout.main);
in your code.You have not attached GestureListener to anything. How do you expect Callbacks to be invoked?
我不确定您是否需要 GestureDetector 的手势覆盖,但我确实知道如果您使用 OnGesturePerformedListener,则需要向视图添加手势覆盖。手势叠加充当用户的绘图板。
以及 XML:
有关它的更多信息此处
I am not sure if you need a gesture overlay for GestureDetector, but I do know that you need to add a gesture overlay to your view if you use OnGesturePerformedListener. A gesture overlay acts as a drawing board for the user.
And the XML:
More information about it here