首先,我是英语的废话,我希望你能理解一切。
我正在尝试创建一个快速的Android应用程序,每次用户在应用程序上滑动时,增加(计数器++)变量“计数器”。这是非常基本的东西,因此代码非常原始。
工作原理:我试图在onCreate活动中调用我的“swipe”类;幻灯片正在为变量“compteur”添加+1,然后通过在主屏幕上设置文本来显示它。
这是我的代码:
MainActivity.java
package com.example.slideandcount;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
int compteur = 0;
private static final int SWIPE_MIN_DISTANCE = 5;
private static final int SWIPE_MAX_OFF_PATH = 125000;
private static final int SWIPE_THRESHOLD_VELOCITY = 1;
private static final int SWIPE_THRESHOLD_VELOCITY_Y = 1;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView)findViewById(R.id.textView1);
text1.setText("Debug 1");
gestureDetector = new GestureDetector(this, new MyGestureDetector());
text1.setText("Debug 2");
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
text1.setText("Debug3");
return gestureDetector.onTouchEvent(event);
}
};
}
@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;
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e1) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
text1.setText("fils de pute");
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH && Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH)
return false;
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
text1.setText(String.valueOf(++compteur));
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
text1.setText(String.valueOf(++compteur));
} else if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY_Y) {
text1.setText(String.valueOf(++compteur));
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY_Y) {
text1.setText(String.valueOf(++compteur));
}
} catch (Exception e) {}
return false;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
}}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.brosselesdentsbatards.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
有趣的是,在“onCreate”活动中,我可以进入调试行“debug 2”但是我不能去“debug 3”,所以我猜问题就在这里。但我找不到如何解决它。
你有什么想法解决这个问题吗?
非常感谢。
答案 0 :(得分:0)
修改:语法强>
首先在布局中添加一个ID:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.brosselesdentsbatards.MainActivity"
android:id="@+id/layout">
<TextView ...
然后在你的onCreate方法:
RelativeLayout rLayout= (RelativeLayout) findViewById(R.id.layout);
rLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
text1.setText("Debug3");
return gestureDetector.onTouchEvent(event);
}
});