2016-07-18 10 views
0

は、私は、AndroidのEclipse上でこのコードを持っていると私は私が素人だ方法のためではなく、とのショートコードすることができると思うのファイルサイズを小さくするためにコードの短いする必要があります。多くの同じコード(ショート続い)

 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を共有している場合は、 clerViewまたは適切なアダプタを持つListView型アダプターはそのようなタスクのためのものです。 –

+1

このコードのforループを作成すると、ファイルサイズがまったく減少しません。 – Jokab

+0

簡単な解決策は、あなたが見つけることができるボタンのforloopを作ることでした –

答えて

0

親のレイアウトにのみ、これらのボタンのビューが含まれている場合は、

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

すべてのボタンが共通の機能とUIを共有する場合は、RecyclerViewまたはListViewタイプを適切なアダプタで使用する必要があります。アダプターはそのようなタスクのためのものです。問題の理解を深めるために、あなたのアプリのスクリーンショットをアップロードしてください。しかし90ボタンIDを見つけることが良い方法ではないことを確実にするための1つのこと。

0

あなたは、リストのレイアウトのためのリストビューまたはRecyclerViewを使用して検討すべきで行うことができます。

またはデータバインディングを使用しましょう。データバインディングを使用すると、findByIdメソッドを回避できます。あなたにhttps://developer.android.com/topic/libraries/data-binding/index.html

はbuild.gradle:あなたのレイアウトファイルで

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

、このような使用タグ:あなたのJavaプログラムで

<?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(...); 
} 
関連する問題