첫째, 나는 영어에 쓰레기입니다. 모든 것을 이해하기를 바랍니다.
사용자가 앱에서 슬라이딩 할 때마다 변수 "카운터"를 증가시키는 (counter ++) 빠른 안드로이드 앱을 만들려고합니다. 정말 기본적인 것이므로 코드는 매우 원시적입니다.
작동 방식 : onCreate 활동에서 "스 와이프"클래스를 호출하려고했습니다. 슬라이드는 변수 "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"으로 이동할 수 없으므로 여기에 문제가있는 것 같습니다. 그러나 나는 그것을 고치는 방법을 찾을 수 없습니다.
이것을 고칠 생각이 있습니까?
대단히 감사합니다.
편집 : 구문
먼저 레이아웃에 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);
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다