gpt4 book ai didi

java - 需要帮助来判断向左或向右滑动

转载 作者:太空宇宙 更新时间:2023-11-04 14:44:49 25 4
gpt4 key购买 nike

我是 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 之间的任何值。

我需要帮助来更正我的代码。

最佳答案

再次检查您的代码:

请参阅此处:

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 is always 1, hence it is showing value 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 is always 10, hence it is showing value 10.

这两个原因是您的代码未显示 1 到 10 之间的其他值的原因。

关于java - 需要帮助来判断向左或向右滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24528745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com