2012-12-19 25 views
5

के साथ एक सरणी के रूप में बटन वैरिएबल घोषित करना मैंने लूप में बटन बनाने में कामयाब रहे और कोई कारण नहीं देखा कि मेरे अंदर मेरी varibles घोषित न करें। दुर्भाग्य से ग्रहण केवल "बीटी" की पहचान करता है और मेरे [i] को लूप में प्रदर्शित संख्या के साथ प्रतिस्थापित नहीं करना चाहता है और नतीजतन मेरे लेआउट में सही आईडी मिलती है। इस काम को कैसे बनाया जाए इस पर कोई विचार? मैं भी, मेरा रूप खूबसूरत जो काम नहीं करता है के रूप में किसी अन्य समाधान के लिए आभारी हूँ,)लूप एंड्रॉइड

Button [] bt = new Button[6]; 

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

    bt[0] = (Button) findViewById(R.id.bt0); 
    bt[1] = (Button) findViewById(R.id.bt1);//This is what i'm trying to replace 
    bt[2] = (Button) findViewById(R.id.bt2); 
    bt[3] = (Button) findViewById(R.id.bt3); 
    bt[4] = (Button) findViewById(R.id.bt4); 
    bt[5] = (Button) findViewById(R.id.bt5); 


    for (int i=0; i<6; i++) { 
     final int b = i; 
     bt [i] = (Button)findViewById(R.id.bt[i]); <----//Whith this 
     bt [i].setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent myIntent = new Intent(Start.this, MainActivity.class); 
       myIntent.putExtra("players", b); 
       startActivity(myIntent); 

       //startActivity(new Intent(Start.this, MainActivity.class)); 
      } 
     }); 

    } 
} 
+3

'bt [i]' 'से बदल दिया जाएगा bt [1]' और 'i = 1' के लिए' bt1' नहीं। आप इस तरह चर का उपयोग नहीं कर सकते हैं। –

+0

@ रोहितजैन यही कारण है कि ओपी सवाल पूछता है। –

+0

आपकी समस्या के लिए यहां एक समाधान है http://stackoverflow.com/questions/3648942/dynamic-resource-loading-android – evilone

उत्तर

11

मैं निम्नलिखित करना होगा:

private static final int[] idArray = {R.id.bt0, R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5}; 

private Button[] bt = new Button[idArray.length]; 

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

    for (int i=0; i<idArray.length; i++) { 
     final int b = i; 
     bt [b] = (Button)findViewById(idArray[b]); // Fetch the view id from array 
     bt [b].setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent myIntent = new Intent(Start.this, MainActivity.class); 
       myIntent.putExtra("players", b); 
       startActivity(myIntent); 

       //startActivity(new Intent(Start.this, MainActivity.class)); 
      } 
     }); 

    } 
} 

आप जोड़ सकते हैं या बटन को निकालना चाहते हैं, बस इसे idArray पर जोड़ें और अन्य सभी चीजें पहले ही गतिशील हैं।

+0

अच्छा जवाब .... +1 – SilentKiller

+0

समझने के लिए काफी सरल और वास्तव में सुंदर। जैसे ही मैं अपना कंप शुरू करता हूं, मैं इसे आज़मा दूंगा। आने वाला कल! – Epcilon

+0

धन्यवाद। खुशी अगर यह मदद करता है :) –

0

मुझे लगता है कि यदि आपके पास समान बटनों का समूह है - वे सभी लेआउट (लीनियरलाउट या रिलेटिवलाउट या कुछ और) पर 1 पैरेंट के अंदर रखे गए हैं। आप माता-पिता को ले सकते हैं और सभी बच्चों को पुनः प्राप्त कर सकते हैं। इस तरह आपको प्रत्येक बटन के लिए आईडी निर्दिष्ट करने की आवश्यकता नहीं है।

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons); 
List<Button> buttons = new ArrayList<Button>(); 
for (int i = 0; i < buttonsView.getChildCount(); i++) { 
    buttons.add((Button) buttonsView.getChildAt(i)); 
} 

इसके अलावा, आप यह के टैग में बटन के नंबर स्टोर कर सकते हैं ताकि आप final int चर बनाने की जरूरत नहीं है:

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons); 
List<Button> buttons = new ArrayList<Button>(); 
View.OnClickListener listener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent myIntent = new Intent(Start.this, MainActivity.class); 
     myIntent.putExtra("players", (Integer) v.getTag()); 
     startActivity(myIntent); 
     //startActivity(new Intent(Start.this, MainActivity.class)); 
    } 
}; 
for (int i = 0; i < buttonsView.getChildCount(); i++) { 
    Button button = (Button) buttonsView.getChildAt(i); 
    button.setTag(i); 
    button.setOnClickListener(listener); 
    buttons.add(buttons); 
} 
+0

बस स्पष्ट करना चाहते हैं, यह केवल तभी काम करता है जब 'व्यू ग्रुप' में सभी बटन नहीं हैं। इसमें कोई अन्य बच्चा नहीं हो सकता है। –

+0

मेरे सवाल निकिता के लिए समय लेने के लिए धन्यवाद, लेकिन यह मेरी समझ से थोड़ा सा है क्योंकि मैं प्रोग्रामिंग के लिए काफी नया हूं। – Epcilon

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^