Android的工作室 - 上的任何按钮Clicklistener? [英] Android Studio - Clicklistener on any button?
问题描述
所以我还在学习Android的工作室,我试图通过制作一个简单的捶一个痣类型游戏蒙混过关我的路。 9按钮网格是隐藏的,当点击一个按钮一个随机数生成器设置一个int变量,然后通过设置一个按钮的可视性交换机出现。
So I'm still learning Android Studio and I'm trying to muddle my way through making a simple whack-a-mole type game. A grid of 9 buttons are hidden and when one button is clicked a random number generator set an int variable which then goes through a switch which sets the visibility of the next button.
这一直很好,直到我要添加+1到比分INT。每个按钮都有它自己的clicklistener用if语句检查它是否已经显现等,我不能让他们都来,因为它需要被声明为final,但是当更新同一个变量开关和它自己的(非常低效当然)副本我这样做,只是说不能分配变量。
This has been fine until I want to add +1 to a score int. Each button has it's own clicklistener with it's own (very inefficient of course) copy of the switch and if statement checking whether it's already visible etc and I can't get them all to update the same variable as it needs to be declared final but when I do it just says cannot assign variable.
所以是有可能做一个像上的任何按钮,而不是特定的一个而clicklistener?然后,我可以定义和更新一个方法变量(如果这是正确的术语)。
So is it possible to do a something like a clicklistener on ANY button rather than a specific one? Then I could define and update the variable in that one method (if that's the right terminology).
下面是第一个按钮的外观进行快速复印就像是可见的,直到第一次点击 -
Here's a quick copy of what the first button looks like which is visible until first clicked -
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button0.setVisibility(View.INVISIBLE);
Random rand = new Random();
int n = rand.nextInt(9);
switch (n) {
case 0:
if(button0.getVisibility() == View.INVISIBLE){
button0.setVisibility(View.VISIBLE);
}else{
button0.setVisibility(View.INVISIBLE);
}
break; ... and so on
所以想什么,我是这样的(这显然作品) -
And so what I'd like is something like this (that obviously works) -
button0 or button1 or button2 or button3 or button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int scoreint = 0;
scoreint++;
scoreText.setText(""+scoreint);
};
就像我说的,我知道这是做事情的最没有效率的和最慢的方式,但我仍然在学习和喜欢做的事情最简单的,愚蠢的方式,使我能得到的核心概念。我看教程太多,但我像一个小方项目要进了。感谢您的帮助!
Like I said, I know this is the most inefficient and slowest way of doing things but I'm still learning and like doing things the simplest, stupidest way so I can get the core concepts. I am watching tutorials too but I like a little side project to get into too. Thanks for any help!
推荐答案
我一跳code这个皮斯帮助您
i hop this pease of code helps you
public class MainActivity extends ActionBarActivity implements View.OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button0 = (Button)findViewById(R.id.button0);
button0.setOnClickListener(this);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
//and on ....
}
public void onClick(View view)
{
switch (view.getId())
{
case R.id.button0 :
// do sum thing
break;
case R.id.button1 :
// do sum thing
break;
}
}
}
这篇关于Android的工作室 - 上的任何按钮Clicklistener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!