I create 8 checkboxes with a loop.
And I want to check a random unchecked checkboxes with a click on one of them.
public class LevelActivity extends AppCompatActivity implements View.OnClickListener {
int wrapContent = LinearLayout.LayoutParams.WRAP_CONTENT;
LinearLayout llLevel;
int iNumCheckButtons;
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
i = 0;
llLevel = (LinearLayout) findViewById(R.id.llLevel);
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(wrapContent, wrapContent);
int btnGravity = Gravity.CENTER;
lParams.gravity = btnGravity;
iNumCheckButtons = 8;
while(i < iNumCheckButtons) {
CheckBox chbNew = new CheckBox(this);
chbNew.setText("CheckBox #"+i);
chbNew.setId(i);
llLevel.addView(chbNew, lParams);
i++;
}
}
@Override
public void onClick(View v) {
CheckBox chbView;
Random r = new Random();
int i1 = r.nextInt(3);
int i2 = r.nextInt(8);
switch (v.getId()) {
case 0:
for (int i = 0; i < i1; i++) {
if (i == i2) {
// How TODO this code //
////////////////////////
chbView = findViewById(R.id.i2);
chbView.setChecked(true);
}
}
break;
case 1:
// ...
break;
// ...
}
}
}
How to find view which has integer ID. Is could be right type like "findViewById(R.id.0);"
Create a views with current ID's in layout?
Aucun commentaire:
Enregistrer un commentaire