Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 允许满足“if语句”的命令_Android - Fatal编程技术网

Android 允许满足“if语句”的命令

Android 允许满足“if语句”的命令,android,Android,我目前有一个命令,如果单击按钮,用户可以进入下一页。这个按钮有一个特性android:onClick=advancenext,我在.java文件中定义如下 public void advancenext (View view) { Intent intent = new Intent(Prompt1.this, Prompt2.class); Prompt1.this.startActivity(intent); } 但是,我只希望用户在变量I大于5时调用advancenext

我目前有一个命令,如果单击按钮,用户可以进入下一页。这个按钮有一个特性android:onClick=advancenext,我在.java文件中定义如下

public void advancenext (View view) {
    Intent intent = new Intent(Prompt1.this, Prompt2.class);
    Prompt1.this.startActivity(intent);
}
但是,我只希望用户在变量I大于5时调用advancenext。这就是我试过的

if (i>5) {
    public void advancenext (View view) {
            Intent intent = new Intent(Prompt1.this, Prompt2.class);
            Prompt1.this.startActivity(intent);
    }
}
但是,当我运行应用程序时,即使我不超过5,我仍然可以前进。有没有人知道如何解决这个问题,或者更好地以不同的方式限制advancenext

编辑感谢您的精彩回答,但我在更改语句时遇到了另一个错误。当我尝试时:

            public void advancenext (View view) {
                if (i>5) {
                    Intent intent = new Intent(Prompt1.this, Prompt2.class);
                    Prompt1.this.startActivity(intent);
                }
            }
我得到了令牌上的错误语法错误;令牌上应存在语法错误;应在视图和周围显示

编辑dos这是我的全部代码

public class Prompt1 extends Activity {

    int i; //Variable i stores the touch number
    float[] X, Y;  //Array that will store the touchpoints
    int ultscore1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_prompt1);

        i=1;

        touchLayout.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){

                i++; //increasing i value

                public void advancenext (View view) {
                    if (i>5) {
                        Intent intent = new Intent(Prompt1.this,     Prompt2.class);
                        Prompt1.this.startActivity(intent);
                    }
                }

                }               
                return true;
            }
        });

    }

您必须在函数中计算if语句。确保您有权访问i变量

public void advancenext(View view) {
    if (i>5) {
         Intent intent = new Intent(Prompt1.this, Prompt2.class);
         Prompt1.this.startActivity(intent);
    }
}

你的陈述倒过来了:

public void advancenext (View view) {
    if (i>5) {
        Intent intent = new Intent(Prompt1.this, Prompt2.class);
        Prompt1.this.startActivity(intent);
    }
}
无论发生什么情况,都应该调用onClick函数,然后检查条件。按照现在的方式,您完全忽略了if语句。

我会这样做

public class Prompt1 extends Activity implements OnTouchListener{

int i; //Variable i stores the touch number
float[] X, Y;  //Array that will store the touchpoints
int ultscore1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prompt1);

    i=1;

    touchLayout.setOnTouchListener(this);

}

@Override
public boolean onTouch(View v, MotionEvent event){
    i++; //increasing i value
    advancenext(v);
    return true;
} 

public void advancenext (View view) {
    if (i>5) {
       Intent intent = new Intent(Prompt1.this,     Prompt2.class);
        Prompt1.this.startActivity(intent);
    }
}
}              
这允许您的类直接处理触摸事件以及“类集内容”视图中包含的视图的任何其他触摸事件,只需将触控侦听器添加到视图中即可。您可以像以前一样处理onTouch中变量i的递增,并使用传递给onTouch的视图调用advancenext方法。advancenext方法可以很容易地将其功能移动到onTouch,即

@Override
public boolean onTouch(View v, MotionEvent event){
    i++; //increasing i value
    if (i>5) {
       Intent intent = new Intent(Prompt1.this,     Prompt2.class);
        Prompt1.this.startActivity(intent);
    }
    return true;
}  

但是将功能从onTouch移到它自己的方法将允许您将advancenext与其他io事件(如onClick)一起使用。

谢谢您的建议!但是,当我尝试此操作时,出现了另一个错误,请查看问题中的编辑。缺少的返回语句onTouch@Lal布拉今天编码太多了谢谢你的建议!但是当我尝试这个,我得到了一个不同的错误,请看我在问题中的编辑。谢谢你的建议!但是当我尝试这个时,我得到了一个不同的错误,请查看问题中我的编辑。你的代码在哪里?它是在onCreate还是其他方法中?如果是这样的话,它需要像任何其他函数一样跳出一个方法。就因为这似乎是一个格式问题,为什么不发布整个类的代码,这样我们就可以看到缺少括号或其他东西的地方。我想一定是这样的。否则IF语句会提前给出编译错误。将advancenext函数放在setOnTouchListener之外,然后重试。@Lal我将它移到onTouchListener之外,但仍然得到相同的错误