簡體   English   中英

向左/向右滑動不起作用

[英]Left/Right Swipe not working

我正在嘗試實現向左向右滑動,這將發送一個吐司,以確認滑動方向。 我在網上找到了有關如何執行此操作的簡單代碼,並且僅使用一個文本視圖就可以完美地工作。 但是,當我嘗試將這個想法實現到我正在從事的項目中時,在滑動屏幕時並沒有得到任何結果。 下面的許多代碼可能與該問題無關,但是我決定展示整個代碼,以防萬一。 謝謝。

   package com.example.final_project;

   import java.util.Map;


import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button B1,B2;
    EditText Text1;
    String Text;
    String Preference;
    String RestarauntChoices= "https://maps.google.com/maps?q=";
    private GestureDetector gestureDetector;
    Double Latitude, Longitude;
    public static String MY_PREFS = "finalproject.my_prefs";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        gestureDetector = new GestureDetector(
                new SwipeGestureDetector());        
        Text1=(EditText)findViewById(R.id.editText1);

    }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
          return true;
        }
        return super.onTouchEvent(event);
      }

      private void onLeftSwipe() {
        Toast.makeText(getApplicationContext(), "Left", Toast.LENGTH_LONG).show();
      }

      private void onRightSwipe() {
            Toast.makeText(getApplicationContext(), "Right", Toast.LENGTH_LONG).show();
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /*
        //Must Be Ran on a Device. If using Emulator it crashes
        //Due to lack of Google Maps Application
        if(item.getItemId() == R.id.menu_mapLocation){
        String uriBegin = "geo:" + Latitude + "," + Longitude;
        String query = Latitude + "," + Longitude;
        String encodedQuery = Uri.encode(query);
        String uriString = uriBegin + "?q=" + encodedQuery + "&z=16";
        Uri uri = Uri.parse(uriString);
        Intent intent =new Intent(android.content.Intent.ACTION_VIEW, uri); 
        startActivity(intent); 
        }
        */
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
    /*
        menu.clear();
        getMenuInflater().inflate(R.menu.main, menu);
        SharedPreferences prefs = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);

        for(Map.Entry<String,?> entry : prefs.getAll().entrySet()){
        menu.add(entry.getValue().toString());
        }
    */
        return super.onPrepareOptionsMenu(menu);
    } 

    public void GpsClick(View view){
    /*
        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 10000, 0, mlocListener);
        Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
    */
    }

    public void FindPartyClick(View view){

/*      
        Preference=Text1.getText().toString();
        SharedPreferences prefs = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(Preference, Preference);
        editor.commit();


        Intent intent = new Intent(MainActivity.this, SearchClass.class); 
        String Address=Text1.getText().toString();
        if(Address.length()!=5 && Address.matches("\\d+"))
            Toast.makeText( getApplicationContext(),"Zip Code must be 5 digits" , Toast.LENGTH_SHORT ).show();
        else{
        intent.putExtra("message", Address);
        Bundle bndlanimation = ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation,R.anim.animation2).toBundle();
        startActivity(intent, bndlanimation);
        }
        //startActivity(intent);
 */
}

      // Private class for gestures
      private class SwipeGestureDetector 
              extends SimpleOnGestureListener {
        // Swipe properties, you can change it to make the swipe 
        // longer or shorter and speed
        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_MAX_OFF_PATH = 200;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                             float velocityX, float velocityY) {
          try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
              return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
               MainActivity.this.onLeftSwipe();

            // Right swipe
            } else if (-diff > SWIPE_MIN_DISTANCE
            && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
              MainActivity.this.onRightSwipe();
            }
          } catch (Exception e) {
            Log.e("YourActivity", "Error on gestures");
          }
          return false;
        }
      }


/* Class My Location Listener */
/*
    public class MyLocationListener implements LocationListener{

    @Override

        public void onLocationChanged(Location loc){
            loc.getLatitude();
            loc.getLongitude();
            setGPS(loc.getLatitude(),loc.getLongitude());
        }

        public String setGPS(double Lat, double Long){
            Latitude=Lat;
            Longitude=Long;
            Text = "My current location is: " + "Latitude = " + Lat + "Longitude = " + Long;
            return Text;
        }

        @Override
        public void onProviderDisabled(String provider)
        {
        Toast.makeText( getApplicationContext(),"Gps Disabled" , Toast.LENGTH_SHORT ).show();

        }

        @Override
        public void onProviderEnabled(String provider)
        {
        Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }


    }/* End of Class MyLocationListener */


}/* End of Main Activity */

不同文件中的類

package com.example.final_project;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class SearchClass extends Activity{

    String msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchpage);
        Intent intent2 = getIntent();
        msg = intent2.getStringExtra("message");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void onResume(){
        super.onResume();
    }

}

將OnTouchListener設置為Activity可能不是一個好主意,但是,如果您基本上只想使用整個屏幕來捕捉滑動, 請看一下

所以問題是我有滾動視圖。 當我取下ScrollView時,滑動工作正常...轉到下一個問題。

暫無
暫無

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

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