我是Android编程的新手,我正在尝试向左或向右滑动执行计数。
我想要的是当我从右向左滑动时,整数 ++ 最多可计算10次,但不会超过10次。
当我从左向右滑动时,它会将整数 - 计算为1而不会低于1.
注意:我不是在谈论循环;我想要的是当我每次刷它增加或减少int计数时。
我尝试的是:
package com.usd.swipegesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;
public class Gesture extends Activity {
float x1, x2;
float y1, y2;
static int b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);
}
// onTouchEvent () method gets called when User performs any touch event on
// screen
// Method to handle touch event like left to right swap and right to left
// swap
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = touchevent.getX();
y2 = touchevent.getY();
// if left to right sweep event on screen
if (x1 < x2) {
// Toast.makeText(this, "Left to Right Swap Performed",
// Toast.LENGTH_LONG).show();
//
b--;
if (b == 1) {
b = 1;
if (b == 1) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("1");
}
if (b == 2) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("2");
}
if (b == 3) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("3");
}
if (b == 4) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("4");
}
if (b == 5) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("5");
}
if (b == 6) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("6");
}
if (b == 7) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("7");
}
if (b == 8) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("8");
}
if (b == 9) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("9");
}
if (b == 10) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("10");
}
}
//
}
// if right to left sweep event on screen
if (x1 > x2) {
// Toast.makeText(this, "Right to Left Swap Performed",
// Toast.LENGTH_LONG).show();
//
b++;
if (b == 10) {
b = 10;
if (b == 1) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("1");
}
if (b == 2) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("2");
}
if (b == 3) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("3");
}
if (b == 4) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("4");
}
if (b == 5) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("5");
}
if (b == 6) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("6");
}
if (b == 7) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("7");
}
if (b == 8) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("8");
}
if (b == 9) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("9");
}
if (b == 10) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("10");
}
}
//
}
break;
}
}
return false;
}
}
但它只显示1和10的值。它没有显示1到10之间的任何值。
我需要帮助来纠正我的代码。
答案 0 :(得分:0)
再次检查您的代码:
见这里:
if (b == 1) {
b = 1; // b is now 1
if (b == 1) { // if b is 1 then do this
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("1");
}
.....
b始终为1,因此显示值为1。
同样在这里:
if (b == 10) {
b = 10; // b is now 10
......
if (b == 10) { // if b is 10 then do this
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("10");
}
b始终为10,因此显示值为10。
这两个是您的代码未显示介于1到10之间的其他值的原因。
答案 1 :(得分:0)
我得到了答案:
package com.usd.swipegesture;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;
public class Gesture extends Activity {
float x1, x2;
float y1, y2;
static int b = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture);
}
// onTouchEvent () method gets called when User performs any touch event on
// screen
// Method to handle touch event like left to right swap and right to left
// swap
public boolean onTouchEvent(MotionEvent touchevent) {
switch (touchevent.getAction()) {
// when user first touches the screen we get x and y coordinate
case MotionEvent.ACTION_DOWN: {
x1 = touchevent.getX();
y1 = touchevent.getY();
break;
}
case MotionEvent.ACTION_UP: {
x2 = touchevent.getX();
y2 = touchevent.getY();
// if left to right sweep event on screen
if (x1 < x2) {
// Toast.makeText(this, "Left to Right Swap Performed",
// Toast.LENGTH_LONG).show();
//
b--;
if (b < 1) {
b = 1;
}
if (b == 1) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("1");
}
if (b == 2) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("2");
}
if (b == 3) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("3");
}
//
}
// if right to left sweep event on screen
if (x1 > x2) {
// Toast.makeText(this, "Right to Left Swap Performed",
// Toast.LENGTH_LONG).show();
//
b++;
if (b == 1) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("1");
}
if (b == 2) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("2");
}
if (b == 3) {
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("3");
}
if (b > 3) {
b = 3;
}
//
}
break;
}
}
return false;
}
}
答案 2 :(得分:0)
使用switch语句(几乎不显着)有效地提高(每一点帮助)。
默认是如果没有一个案例匹配。
if (x1 < x2) {
b--;
switch(b){
case 1:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("1");
break;
case 2:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("2");
break;
case 3:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("3");
break;
case 4:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("4");
break;
case 5:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("5");
break;
case 6:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("6");
break;
case 7:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("7");
break;
case 8:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("8");
break;
case 9:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("9");
break;
case 10:
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("10");
break;
default:
if (b < 1) {
b = 1;
}
}
}