2016-07-18 3 views
0

나는 안드로이드 이클립스에이 코드를 가지고 있고 나는 내가 아마추어 해요 방법에 대한하지만 짧은 코드 수 있다고 생각 파일 크기를 줄일 수 코드 짧은해야합니다많은 동일한 코드 (짧은 그런 다음)

 Button P1 = (Button) findViewById(R.id.Part1); 
    Button P2 = (Button) findViewById(R.id.Part2); 
    Button P3 = (Button) findViewById(R.id.Part3); 
    Button P4 = (Button) findViewById(R.id.Part4); 
    Button P5 = (Button) findViewById(R.id.Part5); 
    Button P6 = (Button) findViewById(R.id.Part6); 
    Button P7 = (Button) findViewById(R.id.Part7); 
    Button P8 = (Button) findViewById(R.id.Part8); 
    Button P9 = (Button) findViewById(R.id.Part9); 
    Button P10 = (Button) findViewById(R.id.Part10); 
    Button P11 = (Button) findViewById(R.id.Part11); 
    Button P12 = (Button) findViewById(R.id.Part12); 
    Button P13 = (Button) findViewById(R.id.Part13); 
    Button P14 = (Button) findViewById(R.id.Part14); 
    Button P15 = (Button) findViewById(R.id.Part15); 
    Button P16 = (Button) findViewById(R.id.Part16); 
    Button P17 = (Button) findViewById(R.id.Part17); 
    Button P18 = (Button) findViewById(R.id.Part18); 
    Button P19 = (Button) findViewById(R.id.Part19); 
    Button P20 = (Button) findViewById(R.id.Part20); 
    Button P21 = (Button) findViewById(R.id.Part21); 
    Button P22 = (Button) findViewById(R.id.Part22); 
    Button P23 = (Button) findViewById(R.id.Part23); 
    Button P24 = (Button) findViewById(R.id.Part24); 
    Button P25 = (Button) findViewById(R.id.Part25); 
    Button P26 = (Button) findViewById(R.id.Part26); 
    Button P27 = (Button) findViewById(R.id.Part27); 
    Button P28 = (Button) findViewById(R.id.Part28); 
    Button P29 = (Button) findViewById(R.id.Part29); 
    Button P30 = (Button) findViewById(R.id.Part30); 
    Button P31 = (Button) findViewById(R.id.Part31); 
    Button P32 = (Button) findViewById(R.id.Part32); 
    Button P33 = (Button) findViewById(R.id.Part33); 
    Button P34 = (Button) findViewById(R.id.Part34); 
    Button P35 = (Button) findViewById(R.id.Part35); 
    Button P36 = (Button) findViewById(R.id.Part36); 
    Button P37 = (Button) findViewById(R.id.Part37); 
    Button P38 = (Button) findViewById(R.id.Part38); 
    Button P39 = (Button) findViewById(R.id.Part39); 
    Button P40 = (Button) findViewById(R.id.Part40); 
    Button P41 = (Button) findViewById(R.id.Part41); 
    Button P42 = (Button) findViewById(R.id.Part42); 
    Button P43 = (Button) findViewById(R.id.Part43); 
    Button P44 = (Button) findViewById(R.id.Part44); 
    Button P45 = (Button) findViewById(R.id.Part45); 
    Button P46 = (Button) findViewById(R.id.Part46); 
    Button P47 = (Button) findViewById(R.id.Part47); 
    Button P48 = (Button) findViewById(R.id.Part48); 
    Button P49 = (Button) findViewById(R.id.Part49); 
    Button P50 = (Button) findViewById(R.id.Part50); 
    Button P51 = (Button) findViewById(R.id.Part51); 
    Button P52 = (Button) findViewById(R.id.Part52); 
    Button P53 = (Button) findViewById(R.id.Part53); 
    Button P54 = (Button) findViewById(R.id.Part54); 
    Button P55 = (Button) findViewById(R.id.Part55); 
    Button P56 = (Button) findViewById(R.id.Part56); 
    Button P57 = (Button) findViewById(R.id.Part57); 
    Button P58 = (Button) findViewById(R.id.Part58); 
    Button P59 = (Button) findViewById(R.id.Part59); 
    Button P60 = (Button) findViewById(R.id.Part60); 
    Button P61 = (Button) findViewById(R.id.Part61); 
    Button P62 = (Button) findViewById(R.id.Part62); 
    Button P63 = (Button) findViewById(R.id.Part63); 
    Button P64 = (Button) findViewById(R.id.Part64); 
    Button P65 = (Button) findViewById(R.id.Part65); 
    Button P66 = (Button) findViewById(R.id.Part66); 
    Button P67 = (Button) findViewById(R.id.Part67); 
    Button P68 = (Button) findViewById(R.id.Part68); 
    Button P69 = (Button) findViewById(R.id.Part69); 
    Button P70 = (Button) findViewById(R.id.Part70); 
    Button P71 = (Button) findViewById(R.id.Part71); 
    Button P72 = (Button) findViewById(R.id.Part72); 
    Button P73 = (Button) findViewById(R.id.Part73); 
    Button P74 = (Button) findViewById(R.id.Part74); 
    Button P75 = (Button) findViewById(R.id.Part75); 
    Button P76 = (Button) findViewById(R.id.Part76); 
    Button P77 = (Button) findViewById(R.id.Part77); 
    Button P78 = (Button) findViewById(R.id.Part78); 
    Button P79 = (Button) findViewById(R.id.Part79); 
    Button P80 = (Button) findViewById(R.id.Part80); 
    Button P81 = (Button) findViewById(R.id.Part81); 
    Button P82 = (Button) findViewById(R.id.Part82); 
    Button P83 = (Button) findViewById(R.id.Part83); 
    Button P84 = (Button) findViewById(R.id.Part84); 
    Button P85 = (Button) findViewById(R.id.Part85); 
    Button P86 = (Button) findViewById(R.id.Part86); 
    Button P87 = (Button) findViewById(R.id.Part87); 
    Button P88 = (Button) findViewById(R.id.Part88); 
    Button P89 = (Button) findViewById(R.id.Part89); 

대단히 감사합니다. 코드를 맨 위에 표시해야합니다.

+1

모든 버튼이 공통된 유형의 기능과 UI를 공유하는 경우 Recy를 사용해야합니다 clerView 또는 적절한 어댑터가있는 일부 ListView 형식 어댑터는 이러한 작업을위한 것입니다. –

+1

이 코드에 대한 for 루프를 작성하면 파일 크기가 전혀 줄어들지 않습니다. – Jokab

+0

간단한 해결책은 u를 찾을 수있는 버튼의 forloop을 만들었습니다. –

답변

0

부모 레이아웃 만 단추보기를 포함하는 경우는

for(int i = 0; i < layout.getChildCount(); i++) { 
         Button button = (Button)layout.getChildAt(i); 
        } 
0

모든 단추가 공통된 유형의 기능과 UI를 공유하는 경우 적절한 어댑터를 사용하여 RecyclerView 또는 ListView 형식을 사용해야합니다. 어댑터는 이러한 작업을위한 것입니다. 문제에 대한 이해를 높이려면 앱의 스크린 샷을 업로드하십시오. 그러나 90 버튼 ID를 찾는 것이 좋은 방법이 아님을 확실히 한 가지.

0

당신은 목록 레이아웃의 ListView 또는 RecyclerView을 사용하는 것이 좋습니다 할 수 있습니다.

또는 데이터 바인딩을 사용해 보겠습니다. 데이터 바인딩을 사용하면 findById 메소드를 피할 수 있습니다. 당신에 https://developer.android.com/topic/libraries/data-binding/index.html

가 build.gradle : 레이아웃 파일에서

android { 
    .... 
    dataBinding { 
     enabled = true 
    } 
} 

,이 같은 사용 태그 : 당신이 자바 프로그램에서

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 

    ... your original layout tags ... 

</layout> 

이 같은 DataBindingUtil를 사용

MainActivityBinding binding; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    binding = DataBindingUtil.setContentView(this, R.layout.main_activity); 

    //you can access buttons like this: 
    binding.Part1.setOnClickListener(...); 
} 
관련 문제