簡體   English   中英

Android代碼可在2.3上使用,但不能在4.0上使用

[英]Android Code Works On 2.3 But Not On 4.0

我編寫了以下針對API 2.3.3最低版本設計的代碼,並且在為此設計的仿真器上可以正常工作。 我剛剛嘗試在API 4.0上測試相同的代碼,但為控制應用程序而實現的onFling手勢不起作用。 他們甚至似乎都不被稱為。

這是代碼。

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;

    //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);

        //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 = "";
        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);
    }









    /*--------------------------------------*/
    /*Additional gesture 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 boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
          float velocityY) 
  {

    String test = textView4.getText().toString();
    if ((velocityX == 0) && (velocityY > 0))
    {

        //Call new password generation or generate random if set
        if(test.equals("Memorable"))
        {
            newPass();
        }
        else if(test.equals("Random"))
        {
            //create new password method
            PassGen pass = new PassGen();

            //Set password
            textView1.setText(pass.randomPassword());
        }
    }
    else if((velocityX == 0) && (velocityY < 0))
    {
        textView1.setText(prevPass);
    }
    else if((velocityY == 0) && (velocityX > 0))
    {

        if(test.equals("Memorable"))
        {
            textView4.setText("Random");
        }
        else if(test.equals("Random"))
        {
            textView4.setText("Memorable");
        }   
    }


    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;
    }

    //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;
    }



    public boolean onSingleTapConfirmed(MotionEvent e) {

     return false;
    }





}

控制台一直說它無法安裝,但出現在模擬器上並運行。 在實際的4.0設備上進行測試時,也會發生相同的情況。

2012-05-03 05:57:06 - Emulator] 2012-05-03 05:57:06.471 emulator-arm[5445:1107] Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz.
[2012-05-03 05:57:06 - Emulator] emulator: emulator window was out of view and was recentered
[2012-05-03 05:57:06 - Emulator] 
[2012-05-03 05:57:06 - Memorable] New emulator found: emulator-5554
[2012-05-03 05:57:06 - Memorable] Waiting for HOME ('android.process.acore') to be launched...
[2012-05-03 05:59:24 - Memorable] HOME is up on device 'emulator-5554'
[2012-05-03 05:59:24 - Memorable] Uploading Memorable.apk onto device 'emulator-5554'
[2012-05-03 05:59:26 - Memorable] Installing Memorable.apk...
[2012-05-03 06:01:34 - Memorable] Failed to install Memorable.apk on device 'emulator-5554!
[2012-05-03 06:01:34 - Memorable] (null)
[2012-05-03 06:01:34 - Memorable] Failed to install Memorable.apk on device 'emulator-5554': EOF
[2012-05-03 06:01:34 - Memorable] com.android.ddmlib.InstallException: EOF
[2012-05-03 06:01:34 - Memorable] Launch canceled!

編輯:

它現在可以運行並安裝在所有AVD(2.3.3-4.0)上,但手勢仍然僅在2.3.3上有效

我對你沒有什么建議,

1)您首先打開avd,然后等待其完全加載,然后運行apk。

2)更改您的setContentView(tv); 通過this.setContentView(tv);

3)關閉/刪除其他舊的AVD

4)如果設備隨身攜帶,請對其進行測試。


鏈接

似乎手勢錯誤未得到識別是由於我使用速度來確定運動,但在Android 4.0上受到了更好的控制,因此從未達到VelocityX = 0的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM