Java 如何在Android中引用多个按钮来执行相同的操作
我想做的是,当我单独单击这些按钮时,让它们执行相同的操作。我刚从C#net转学过来,所以我不知道该怎么做。我将所有的按钮安卓:onClick设置为“UserPush”,但我不知道从那里该做什么。我还试图让他们在按下时禁用Java 如何在Android中引用多个按钮来执行相同的操作,java,android,for-loop,Java,Android,For Loop,我想做的是,当我单独单击这些按钮时,让它们执行相同的操作。我刚从C#net转学过来,所以我不知道该怎么做。我将所有的按钮安卓:onClick设置为“UserPush”,但我不知道从那里该做什么。我还试图让他们在按下时禁用 public class GameScreen extends Activity { Button A1,A2,A3,B1,B2,B3,C1,C2,C3; @Override protected void onCreate(Bundle saved
public class GameScreen extends Activity {
Button A1,A2,A3,B1,B2,B3,C1,C2,C3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_screen);
A1 = (Button) findViewById(R.id.A1);
A2 = (Button) findViewById(R.id.A2);
A3 = (Button) findViewById(R.id.A3);
B1 = (Button) findViewById(R.id.B1);
B2 = (Button) findViewById(R.id.B2);
B3 = (Button) findViewById(R.id.B3);
C1 = (Button) findViewById(R.id.C1);
C2 = (Button) findViewById(R.id.C2);
C3 = (Button) findViewById(R.id.C3);
}
public void UserPush() {
for(Button c : GameScreen.class) {
c.Disable
}
}
}
如果我没弄错的话,你可以这样做:
public void UserPush(View v){
((Button)v).setEnabled(false);
}
因此,您想要实现的是使用相同的操作创建多个按钮,而该操作是禁用所有按钮?您需要UserPush()方法中的View v参数才能工作。nayoso,yes和其他一些东西,但如果我不能做到这一点,我可以想出来joao2fast4u,我试过了,Game.class变红了你需要检查红色下划线是怎么说的,然后