2013-11-25 56 views
0

我試圖執行一個左右滑動操作,它會發送一個確認滑動方向的嚮導。我在網上找到了一個簡單的代碼,它只用一個文本視圖就可以很好地工作。但是,當我嘗試將這個想法應用到我一直在研究的項目中時,在刷屏時我沒有得到任何結果。下面的許多代碼可能與問題無關,但我決定展示我的整個代碼以防萬一。謝謝。左/右滑動不起作用

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

} 

回答

0

也許它不是設置一個OnTouchListener一個活動是個好主意,但如果你基本上只是想使用整個屏幕捕捉揮筆,have a look at this

0

所以問題是我有一個滾動視圖。當我將ScrollView關閉滑動工作時...開啓下一個問題。