手势检测器不工作

发布于 2024-10-18 06:17:44 字数 1674 浏览 6 评论 0原文

我有一组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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声无音无过去 2024-10-25 06:17:44

三件事:

  • 您使用此代码所做的就是在堆上创建一堆对象并设置一些属性。您创建的这些视图对象甚至不会附加到视图层次结构中。
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);
  • 您的 Activity 的视图层次结构来自布局文件夹中的 main.xml 文件,因为您在代码中编写了:setContentView(R.layout.main);

  • 您尚未将 GestureListener 附加到任何东西。您期望如何调用回调?

Three things:

  • All you are doing with this code is creating bunch of objects on the heap and setting some properties. These view objects that you've created are not even appended to the View hierarchy.
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);
  • 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?

愚人国度 2024-10-25 06:17:44

我不确定您是否需要 GestureDetector 的手势覆盖,但我确实知道如果您使用 OnGesturePerformedListener,则需要向视图添加手势覆盖。手势叠加充当用户的绘图板。

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);

以及 XML:

<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"
android:orientation="vertical">

<ListView
    android:id="@android:id/list"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  />

有关它的更多信息此处

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.

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);

And the XML:

<android.gesture.GestureOverlayView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"
android:orientation="vertical">

<ListView
    android:id="@android:id/list"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  />

More information about it here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文