0

I am making a basic calculator for Android by eclipse and Java. The task is there are two values, like input and result. So when the first number is pressed, it saves it, then a plus or minus sign is pressed ad then the second value which is result.

My idea was to create a method for every button something like this:

public void number1(View view){
    value1 = value2;
    value2 = 1;
    displayValue();
}

But did not work and also I am repeating a few lines many times which isnt nice. So what about using switch, or any idea to have one single method and put it on all android:onClick"" for buttons.

So if you have any idea just explain it or write an example. Thanks in advance.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
Dan Ask
  • 1
  • 3

2 Answers2

0

So what about using switch,

Yes, you can use a switch statement inside of your method and switch on the id of the Button clicked. Something like

public void someMethod(View v)
{
     switch (v.getId())     // get the id of the View clicked
     {
        case R.id.idOfButton1:     // and compare it to your Button ids
             // do stuff;
             break;
        case R.id.idOfButton2:
             // do stuff;
             break;
      }
      // put common code before or after switch statement
}

Here v is the Button clicked so you get its id and compare it.

any idea to have one single method and put it on all android:onClick"" for buttons.

In your xml for each Button add that line

<Button
    ...
    android:onClick="someMethod"/>

The Button Docs explain how this works and the rules associated with doing it this way but fairly straightforward.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

I would really advise you to use ButterKnife for this case as best practice. All you have to do is to use @OnClick annotation with requested ids.

@OnClick({ R.id.door1, R.id.door2, R.id.door3 })
public void pickDoor(DoorView door) {
     if (door.hasPrizeBehind()) {
        Toast.makeText(this, "You win!", LENGTH_SHORT).show();
     } else {
        Toast.makeText(this, "Try again", LENGTH_SHORT).show();
     }
}   

And inject this views at onCreate of your Activity or Fragment.

@Override 
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main_activity);
     Views.inject(this);
}

See this site for additional examples.

Sergii Pechenizkyi
  • 22,227
  • 7
  • 60
  • 71
  • Why use an external library for something that the Android framework has already built-in functions/properties for? – codeMagic Oct 18 '13 at 14:15
  • It saves time for typing and makes your code look cleaner, which is good. Under the hood it generates usual android click listeners. Check site, it has explanations with code comparing. – Sergii Pechenizkyi Oct 18 '13 at 14:17
  • 1
    I glanced over the site already and I just don't think in this situation it is going to save much for something this simple which Android already has an easy way to do this. It just doesn't seem that adding the library and doing all of this is going to be ebtter than using the framework compnents...that's just my opinion. – codeMagic Oct 18 '13 at 14:30