내 자신의 연습을 위해 인스턴스 필드에 3 개의 버튼 배열을 만들고 각 버튼이 BackGround Color를 변경할 수있는 setOnClickListeners를 갖고 싶습니다.버튼 배열을위한 리스너 (들)
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'Button b = ...'는'Button [] b = ...'여야합니다. – drunkenRabbit