在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
使其最终或全局。两者都可以工作。您不会点击全局。我认为这是在内部创建的方法。 –
...以'''final''为首选方式。您可能还想考虑'''布尔'''而不是'''int'''那个场景。 –
@Surace当我申请final时:错误:(43,17)错误:无法为最终变量点击一个值 – Bigsortvids