对于我自己的练习,我在实例字段中创建了一个3个按钮的数组,并且我希望所有这些按钮都具有setOnClickListeners,它允许每个按钮更改一个按钮的BackGround Color文字View.Can任何人请指导我朝着正确的direction.Here是我的代码:按钮阵列的监听器
public class MainActivity extends Activity {
Button b = {(Button)findViewById(R.id.button1),
(Button)findViewById(R.id.button2),
(Button)findViewById(R.id.button3),};
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
for(int i=0; i < b.length;i++){
b[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(butt[0].isPressed()){
tv.setBackgroundColor(Color.BLACK);
}
if(b[1].isPressed()){
tv.setBackgroundColor(Color.BLUE);
}
if(b[2].isPressed()){
tv.setBackgroundColor(Color.RED);
}
}
});
}
}
}
类似下面你设置一个监听器按钮,按下了
switch
。因此,当按下该按钮时,只包括*您想要在特定按钮上单击的代码。 – christopher 2013-05-06 15:52:30'按钮b = ...'应该是按钮[] b = ...' – drunkenRabbit 2013-05-06 15:53:50