Android手势onFling无法正常工作

我的onFling手势代码完美运行。 然后我添加了一些if语句,并在Activity代码中做了一些小的其他调整。 Plus在另一个类PassGen中写了几个新的方法。 以及一些代码重新排序。

除了被他们调用之外,我没有添加手势接口,但是现在onFling手势甚至不会触发。 双击手势仍然有效。

日志中没有错误。

以下是来自Activity的代码

package com.mystraldesign.memorable;

import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import com.mystraldesign.memorable.PassGen;

public class MemorableActivity extends Activity implements android.view.GestureDetector.OnGestureListener,OnDoubleTapListener  
{
    //Define text views
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;

    //Previous password holder
    private String prevPass;

    //Gesture Detectors
    private GestureDetector gTap; 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        gTap  = new GestureDetector(this,(android.view.GestureDetector.OnGestureListener) this);

        //Remove title bar
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);




        //Define textView
        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        textView4 = (TextView) findViewById(R.id.textView4);
        textView5 = (TextView) findViewById(R.id.textView5);

        //Load font file
        Typeface type = Typeface.createFromAsset(getAssets(),"fonts/optima.ttf"); 

        //Set various textViews to font
        textView1.setTypeface(type);
        textView2.setTypeface(type);
        textView3.setTypeface(type);
        textView4.setTypeface(type);

        prevPass = "Memorable";

    }


    //Password call
    public void newPass()
    {
        //Store Return
        String retn = null;
        PassGen passWord = new PassGen();


        //Generate password
        try 
        {
            retn = passWord.passwordGen(this);
        } 
        catch (IOException e) 
        {

            //Message about Error
            Context context = getApplicationContext();
            CharSequence text = "Ooops Something Went Wrong!";
            int duration = Toast.LENGTH_SHORT;

            //Display message
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            textView1.setText("Memorable");


            e.printStackTrace();
        }

        //Update prevpass
        prevPass = textView1.getText().toString();

        textView1.setText(retn);
    }


    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
              float velocityY) 
    {


        if(velocityX == 0.0 && velocityY > 0.0001)
        {
            //Call new password generation or generate random if set
            if(textView4.getText() == "Memorable")
            {
                newPass();
            }
            else if(textView4.getText() == "Random")
            {
                //create new password method
                PassGen pass = new PassGen();

                //Set password
                textView4.setText(pass.randomPassword());
            }
        }
        else if(velocityY == 0 && velocityX > 0.0001)
        {
            if(textView4.getText() == "Memorable")
            {
                textView4.setText("Random");
            }
            else if(textView4.getText() == "Random")
            {
                textView4.setText("Memorable");
            }
            else if(velocityX == 0.0 && velocityY > -0.0001)
            {
                textView4.setText(prevPass);
            }


            textView5.setText("VelocityX: " + velocityX + " VelocityY: " + velocityY);
        }

              return false;
    }


    //Method to copy password
    public boolean onDoubleTapEvent(MotionEvent e) {

        //clipboard shite
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
        clipboard.setText(textView1.getText());

        //Message about coping
        Context context = getApplicationContext();
        CharSequence text = "Password has been copied to clipboard.";
        int duration = Toast.LENGTH_SHORT;

        //Display message
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

      return false;
    }



    /*--------------------------------------*/
    /*Additional geasture code below. */
    /* */
    /*J. Krawczyk 3/5/12*/
    /*--------------------------------------*/






    public boolean onTouchEvent(MotionEvent me){ 
      this.gTap.onTouchEvent(me);
     return super.onTouchEvent(me); 
    }

    public boolean onDown(MotionEvent e) {

      return false;
    }




    public void onLongPress(MotionEvent e) 
    {

    }


    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
      float distanceY) {

     return false;
    }


    public void onShowPress(MotionEvent e) {

    }

    public boolean onSingleTapUp(MotionEvent e) {

     return false;
    }


    //Method to copy password - Depreciated
    public boolean onDoubleTap(MotionEvent e) {

     return false;
    }



    public boolean onSingleTapConfirmed(MotionEvent e) {

     return false;
    }





}

编辑:

我将onTouchEvent移动到了onCreate下面,现在它触发onFling函数,但if语句不做任何事情,尽管velocityX和velocityY分别为1410.8032和0.0,它们应该触发一个。 它也没有检测到正确的,向上或向下的。 只剩下了


原来是代码顺序和if语句。 我需要移动onFling回落到下面onTouchEvent ,添加额外的()进入if语句来隔离&&的

链接地址: http://www.djcxy.com/p/91227.html

上一篇: Android Gesture onFling not working

下一篇: Swipe listeners in Android