के साथ एक सरणी के रूप में बटन वैरिएबल घोषित करना मैंने लूप में बटन बनाने में कामयाब रहे और कोई कारण नहीं देखा कि मेरे अंदर मेरी 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));
}
});
}
}
'bt [i]' 'से बदल दिया जाएगा bt [1]' और 'i = 1' के लिए' bt1' नहीं। आप इस तरह चर का उपयोग नहीं कर सकते हैं। –
@ रोहितजैन यही कारण है कि ओपी सवाल पूछता है। –
आपकी समस्या के लिए यहां एक समाधान है http://stackoverflow.com/questions/3648942/dynamic-resource-loading-android – evilone