Is there a way to shorten the code if you have a lot of buttons with similar use? There have to be ways, however I'm not yet experienced enough to come up with a different solution. Take the following code for example:
btn_0 = findViewById(R.id.btn_0);
btn_1 = findViewById(R.id.btn_1);
btn_2 = findViewById(R.id.btn_2);
btn_3 = findViewById(R.id.btn_3);
btn_4 = findViewById(R.id.btn_4);
btn_5 = findViewById(R.id.btn_5);
btn_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view_input.setText("0");
function();
}
});
btn_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view_input.setText("1");
function();
}
});
btn_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view_input.setText("2");
function();
}
});
btn_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view_input.setText("3");
function();
}
});
btn_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view_input.setText("4");
function();
}
});
btn_5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
view_input.setText("5");
function();
}
});
What options are there to write this more efficient and with less repetitive code? I have seen an option like this, however I have no clue how this is actually working:
private Button[] btns = new Button[4];
for (int i = 0; i < 4; i++) {
this.btns[i] = super.findViewById(new int[]{R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4}[i]);
}
If you come up with a solution I would really appreciate a short explanation why this way is working. Thanks for your effort :)