When I create an Android app, I use a switch statement in a separate class to handle the behavior of multiple buttons. When I click a button, how can I detect which button was pressed, from another class?
The other class is not an activity class. Can anyone comment on the code below, so I'll better understand how to implement this correctly.
My first method in class A is
public void onClick(View v){
switch (v.getId()){
case R.id.button1 :
GifView.mymethod(R.id.button1);
final Dialog dialog1=new Dialog(context);
dialog1.setContentView(R.layout.dialog);
Button finish1= (Button) dialog1.findViewById(R.id.finish);
finish1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog1.dismiss();
}
});
dialog1.show();
break;
I would like to pass this button id to another class, which would have the business logic for handling all button clicks.
private void init(Context context) {
switch(?????){
case R.id.button1:
setFocusable(true);
gifInputStream = getResources().openRawResource(R.drawable.hen);
gifMovie = Movie.decodeStream(gifInputStream);
movieWidth = gifMovie.width();
movieHeight = gifMovie.height();
movieDuration = gifMovie.duration();
break;
what can i do inside the switch statement?
public static int mymethod(int id) {
int buttonId=id;
return buttonId;
}