Created
May 29, 2019 09:50
-
-
Save gordinmitya/548712d475e6c105d341be3afac854db to your computer and use it in GitHub Desktop.
Пример задания обработчика нажатия нескольким кнопкам. Калькулятор.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private View.OnClickListener numberClickListener = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.button0: | |
// обработка нажатия на 0 | |
break; | |
// остальные кнопки … | |
} | |
} | |
}; | |
// Вызвать этот метод в onCreate после setContentView(…); | |
private void initViews() { | |
int[] buttonIds = new int[] { | |
R.id.button0, | |
R.id.button1, | |
// … | |
R.id.buttonPoint, | |
}; | |
for (int buttonId : buttonIds) { | |
findViewById(buttonId).setOnClickListener(numberClickListener); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment