因此,我尝试在此页面上的Fling功能上实现手势。我已经使用过手势,但由于某些原因,这段代码无法运行。我确实得到了警告,但我不知道如何修复它,它说代码中没有使用变量手势,尽管你可以在onCreate中清楚地看到它。帮助
以下是代码:
package com.example.finisher;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.TextView;
public class newclass extends Activity implements OnGestureListener{
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gesture;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textView=new TextView(this);
setContentView(textView);
gesture = new GestureDetector(this);
AssetManager assetManager = getAssets();
InputStream inputStream =null;
try {
inputStream = assetManager.open("texts/myawesometext.txt");
String text = loadTextFile(inputStream);
textView.setText(text);
} catch(IOException e){
textView.setText("Couldn't Load the file.");
}
finally{
if (inputStream!=null){
try{
inputStream.close();
} catch(IOException e){
textView.setText("Couldn't Close the File");
}
}
}
}
private String loadTextFile(InputStream inputStream)throws IOException {
ByteArrayOutputStream bytestream=new ByteArrayOutputStream();
byte[] bytes= new byte[4096];
int len=0;
while((len= inputStream.read(bytes))>0)
bytestream.write(bytes, 0, len);
return new String(bytestream.toByteArray(),"UTF8");
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent start, MotionEvent finish, float xv,
float yv) {
Log.v("msg","msg");
//Y motion only
if(Math.abs(start.getRawY()-finish.getRawY())>SWIPE_MIN_DISTANCE
&&Math.abs(yv)>SWIPE_THRESHOLD_VELOCITY){
//y motion
if (start.getRawY()>finish.getRawY()){
//gesture down, page from top
Intent myIntent = new Intent(newclass.this,MainActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(myIntent);
//animation done in pairs.Bringing top view in
this.overridePendingTransition(R.anim.slide_bottom_in,
R.anim.slide_bottom_out);
}
}
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
}