2016-12-28 63 views
0

在Android Studio中的应用程序,我有以下代码:Java的全球INT

boolean clicked = true; 
    Button btnumb1 = (Button) this.findViewById(R.id.numb1); 
    Button btnumb2 = (Button) this.findViewById(R.id.numb2); 
    Button btnumb3 = (Button) this.findViewById(R.id.numb3); 
    Button btnumb4 = (Button) this.findViewById(R.id.numb4); 
    btnumb1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      clicked = false; 
     } 
    }); 

    Button Start = (Button) this.findViewById(R.id.Start); 
    Start.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (clicked == false) { 
       Random rand = new Random(); 
       int n = rand.nextInt(4) + 1; 
       TextView textView = (TextView) findViewById(R.id.randOutput); 
       textView.setText("" + n); 
      } 
      else{ 
       Toast.makeText(getApplicationContext(), "You have to choose a number", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

的想法是,当点击了4个按钮之一,INT单击了被设置为1,因此最后按钮可以只有当它是1时才被点击。 但是代码不能像这样工作; int clicked = 0;不能在其他公共空间访问。 If one of the numbers 1,2,3,4 is clicked then final button can be clicked

+1

使其最终或全局。两者都可以工作。您不会点击全局。我认为这是在内部创建的方法。 –

+0

...以'''final''为首选方式。您可能还想考虑'''布尔'''而不是'''int'''那个场景。 –

+0

@Surace当我申请final时:错误:(43,17)错误:无法为最终变量点击一个值 – Bigsortvids

回答

1

的修复:将public static boolean clicked = false;

下你public class YourClassName {线。

推理:你需要学习如何适当地确定变量的范围。你在onCreate()函数里声明了布尔点击,所以在onCreate()运行完毕后变量消失了。

您应该通过public boolean clickedpublic static boolean clickedclicked变量放在类作用域级别内,以便即使在函数返回后,也会保存该值。

在继续您的项目之前,我强烈建议您使用java初学者课程或教科书。

0

Java没有任何全局变量的概念。有一个实例变量或一个类变量。要定义全局变量,您可以使用静态关键字。

static boolean clicked = true; 
Button btnumb1 = (Button) this.findViewById(R.id.numb1); 
Button btnumb2 = (Button) this.findViewById(R.id.numb2); 
Button btnumb3 = (Button) this.findViewById(R.id.numb3); 
Button btnumb4 = (Button) this.findViewById(R.id.numb4); 
btnumb1.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     clicked = false; 
    } 
}); 

Button Start = (Button) this.findViewById(R.id.Start); 
Start.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     if (clicked == false) { 
      Random rand = new Random(); 
      int n = rand.nextInt(4) + 1; 
      TextView textView = (TextView) findViewById(R.id.randOutput); 
      textView.setText("" + n); 
     } 
     else{ 
      Toast.makeText(getApplicationContext(), "You have to choose a number", Toast.LENGTH_LONG).show(); 
     } 
    } 
}); 
+0

错误:修饰语“静态”不允许在这里 – Bigsortvids

0

是变量int clicked = 0;必须在其他公共无效功能可用之前声明为最终的。

试一下这个声明的类说ClassClicked

public class ClassClicked { 
    int clicked; 

    public int getClicked() { 
     return clicked; 
    } 

    public void setClicked(int clicked) { 
     this.clicked = clicked; 
    } 

} 

然后改变

int clicked = 0; 

final ClassClicked clicked = new ClassClicked(); 
clicked.setClicked(0); 

而且

clicked = 1; 

clicked.setClicked(1); 

最后

clicked == 1 

clicked.getClicked() == 1