2013-10-26 48 views
-4

我尝试为android设计一个应用程序。在java中更改变量值

我想,轻敲按钮1,x设置为1,并为BUTTON2 y设置为1,等等。当

然后在if语句中使用这些变量。

int z; 
int y; 
int w; 
final Button m = (Button) findViewById(R.id.button1); 
final Button m1 = (Button) findViewById(R.id.button4); 
final Button m2 = (Button) findViewById(R.id.button5); 
m.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     // some code here 
     z == 1; 
     return false; 
    } 
}); 
m1.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     // some code here 
     z == 1; 
     return false; 
    } 
}); 
m2.setOnTouchListener(new OnTouchListener() { 
    @Override 
    public boolean onTouch(View arg0, MotionEvent arg1) { 
     // some code here 
     z == 1; 
     return false; 
    } 
}); 
if (x == 1 & y == 1 & z == 1) { 
    finish(); 
    //some code here 
} 

但是“if”内部块永远不会到达;我该如何解决这个问题?

谢谢。

+0

你应该学会如何正确地格式化代码。关于你的问题:Hexafraction是正确的。你必须使用一个等号来为你的变量赋值。 – Ahmad

+0

您应该使用'&&'作为逻辑,并且像这样'if(x == 1 && y == 1 && z == 1)'。一个'&'是一个按位运算符,如果一切都是奇数,但不会按照您的想法操作,则该运算符将起作用。 – Simon

+0

==只是错的类型 我的问题:“如果”没有工作(我试过&&但还没有工作) – meysam

回答

1
z==1; 

是错误的。 ==用于比较值,不用于设置。使用方法:

z=1; 

改为。无论如何,在跳入Android之前,我会学习更多的Java基础知识。


这可能会发生,所以我最好现在而不是以后告诉你。不应使用==而是使用string1.equals(string2)来比较字符串。

+0

==是错误的类型我知道它的比较我只是赶紧键入记事本并粘贴在这里 我的问题是“如果”不起作用! – meysam

+0

@meysam如果你指定错误,会导致ifs无法正常工作。 – hexafraction