본문 바로가기
안드로이드

[Android] 배열을 활용한 객체 등록

by 코딩히어로 2022. 7. 23.
728x90

1


안드로이드를 개발하면서 버튼이나 텍스트 등 여러 객체를 등록하는데

만약 레이아웃 내 객체가 반복적으로 여러 개가 있을 때 배열을 활용하여 간단하게 처리할 수 있습니다

 

먼저 Application을 제작하면서 한 화면에 10개의 버튼이 있다고 가정해보겠습니다

이럴 경우 다음과 같이 버튼을 등록하고 선언해주어야 합니다

 

private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10;

@Override
protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);
    btn5 = (Button) findViewById(R.id.btn5);
    btn6 = (Button) findViewById(R.id.btn6);
    btn7 = (Button) findViewById(R.id.btn7);
    btn8 = (Button) findViewById(R.id.btn8);
    btn9 = (Button) findViewById(R.id.btn9);
    btn10 = (Button) findViewById(R.id.btn10);
    
}

 

만약 등록하고자 하는 객체가 많이 없다면 해당 방법을 통해서도 가능하지만

예를 들어서 처리하는 구문까지 들어가게 된다면 다음과 같이 코드가 무자비하게 길어지게 됩니다

 

private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    btn4 = (Button) findViewById(R.id.btn4);
    btn5 = (Button) findViewById(R.id.btn5);
    btn6 = (Button) findViewById(R.id.btn6);
    btn7 = (Button) findViewById(R.id.btn7);
    btn8 = (Button) findViewById(R.id.btn8);
    btn9 = (Button) findViewById(R.id.btn9);
    btn10 = (Button) findViewById(R.id.btn10);
    
   btn1.setEnabled(false);
   btn1.setBackgroundResource(R.drawable.button_stop_color1);
   btn2.setEnabled(false);
   btn2.setBackgroundResource(R.drawable.button_stop_color1);
   btn3.setEnabled(false);
   btn3.setBackgroundResource(R.drawable.button_stop_color1);
   btn4.setEnabled(false);
   btn4.setBackgroundResource(R.drawable.button_stop_color1);
   btn5.setEnabled(false);
   btn5.setBackgroundResource(R.drawable.button_stop_color1);
   btn6.setEnabled(false);
   btn6.setBackgroundResource(R.drawable.button_stop_color1);
   btn7.setEnabled(false);
   btn7.setBackgroundResource(R.drawable.button_stop_color1);
   btn8.setEnabled(false);
   btn8.setBackgroundResource(R.drawable.button_stop_color1);
   btn9.setEnabled(false);
   btn9.setBackgroundResource(R.drawable.button_stop_color1);
   btn10.setEnabled(false);
   btn10.setBackgroundResource(R.drawable.button_stop_color1);
    
}

 

자 어떤가요 코드가 길어짐에 따라 효율성이 많이 떨어지게 됩니다

그래서 우리는 배열을 통한 선언과 등록을 통해서 이러한 비효율성을 줄일 수 있습니다

 

private Button[] btn = new Button[10];
private Integer[] btn_id = {R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,
			R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9,R.id.btn10};

@Override
protected void onCreate(Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   
}

 

먼저 Button의 객체배열을 선언하고 사이즈는 10개를 만들어 주고

등록을 위한 Id 또한 Integer 배열을 통해 선언합니다

 

private Button[] btn = new Button[10];
private Integer[] btn_id = {R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,
			R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9,R.id.btn10};

@Override
protected void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   
   for(int i=0;i<10;i++){
       btn[i] = (Button)findViewById(btn_id[i]);
   }
   
}

 

이제 위에서10줄에 가깝게 등록을 했던 코드가 for문을 통해 단 3줄로 줄어들게 됩니다

자 그럼 버튼 처리구문에 대해서도 줄일 수 있는지 보겠습니다

 

private Button[] btn = new Button[10];
private Integer[] btn_id = {R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,
			R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9,R.id.btn10};

@Override
protected void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   
   for(int i=0;i<10;i++){
       btn[i] = (Button)findViewById(btn_id[i]);
       btn[i].setEnabled(false);
       btn[i].setBackgroundResource(R.drawable.button_stop_color1);
   }
   
}

 

처리 구문 또한 위에 약 20줄이 단 2줄로 줄어들었습니다

이처럼 객체를 배열에 담아서 처리하게 되면 연속적인 처리를 할 때 코드의 효율성을 높일 수 있습니다

728x90
반응형

댓글