我试图学习android(工作室),希望创建我自己的应用程序,其他人将在不久的将来享受。我试图编写一个简单的手势应用程序,当用户向左或向右滑动时,textView小部件将会改变。
提前致谢。
这不是正常工作的代码,但我想我在我希望做的一般空间里希望有人可以让它更清晰一些 我目前所拥有的:
MainActivity.java
package com.example.chris.gestureproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
public class MainActivity extends AppCompatActivity implements SimpleOnGestureListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tapButton = (Button)findViewById(R.id.tapButton);
tapButton.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
TextView messageText = (TextView)findViewById(R.id.messageText);
messageText.setText("I Got Tap");
}
}
);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float sensitvity = 50;
if ((e1.getX() - e2.getX()) > sensitvity) {
SwipeLeft();
} else if ((e2.getX() - e1.getX()) > sensitvity) {
SwipeRight();
}
return true;
}
};
GestureDetector gestureDetector = new GestureDetector(
simpleOnGestureListener);
private void SwipeLeft() {
TextView messageText = (TextView)findViewById(R.id.messageText);
messageText.setText("Left Side Swiped");
}
private void SwipeRight() {
TextView messageText = (TextView)findViewById(R.id.messageText);
messageText.setText("Right Side Swiped");
}
}