I have a toy app that contains:
editText1
for entering a number. UsesinputType
ofnumber
.editText2
for entering a second number. UsesinputType
ofnumber
.adderButton
for triggering addition.textVeiw
for displaying the result of the addition.
I know:
- The naming could be better
- I should check for overflow/underflow on the addition and elsewhere.
This code is working for simple cases.
public void adder_click(View view) {
EditText et1 = (EditText) findViewById(R.id.editText1);
EditText et2 = (EditText) findViewById(R.id.editText2);
Integer s = Integer.valueOf(et1.getText().toString()) + Integer.valueOf(et2.getText().toString());
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(s.toString());
}
Is there a more direct way of reading the values and performing the addition?
2 Answers 2
No, that's as direct as it's going to be with an EditText
(you could try it with a NumberPicker
), as you cannot directly get an integer from it.
But you could extract the process into its own method:
private int getInt(EditText editText) throws NumberFormatException {
return Integer.valueOf(editText.getText().toString());
}
If you also use what @mleyfman said, your code would look like this:
public void adder_click() {
tv.setText(getInt(et1) + getInt(et2));
}
Also note that I removed the method argument, as it's unused.
And for the record, yes, the naming isn't very good. It's easy to just leave the names as editText1
, editText2
, etc, but this will get confusing really fast (as will tv
, et1
, et2
, etc). Also, use camelCase (adder_click
should be adderClick
)
That code is a bit dangerous. Integer.valueOf()
can throw NumberFormatException
which you make no effort to catch. This will happen if you have commas in numbers or non-digit characters. Your other comments are also very important.
As for simplifying the method, it is not good that you call findViewById
3 times every time you press a button. I would store references to editText1
, editText2
and textView
externally (of this method), as then you can reuse all three here and elsewhere in your code with subtract/multiply etc. You can then remove three lines of code and just have:
Integer s = Integer.valueOf(et1.getText().toString()) + Integer.valueOf(et2.getText().toString());
tv.setText(s.toString());
Remember that you should probably wrap the code in a try catch block unless you can guarantee the text field will only contain decimal numbers. Also, add in your overflow/underflow logic as well.
-
\$\begingroup\$ For
edtitText1
andeditText2
theinputType
is set tonumber
in the activity's XML. So only digits can be input from the onscreen keyboard in the emulator. But could non-digits be pasted into the text field? I am using Android Studio if this context doesn't make sense in Eclipse or another development environment. \$\endgroup\$ben rudgers– ben rudgers2014年08月20日 19:40:11 +00:00Commented Aug 20, 2014 at 19:40 -
1\$\begingroup\$ If that is the case, then the code is safe. If at some point someone touches the XML and removes that, then the code might blow up then. In general, I always code with the philosophy: better safe than sorry, so that is why I urge you to be diligent. \$\endgroup\$mleyfman– mleyfman2014年08月20日 19:58:58 +00:00Commented Aug 20, 2014 at 19:58
-
\$\begingroup\$ I appreciate the wisdom. The adder program was literally just my very first attempt to wire up an event. And so I stumbled blindly into the the realization that I had to use reflection. \$\endgroup\$ben rudgers– ben rudgers2014年08月20日 20:21:31 +00:00Commented Aug 20, 2014 at 20:21
-
1\$\begingroup\$ @benrudgers you could also add checks like
if(!TextUtils.isEmpty(et1.getText().toString())){//do something}
to check emptiness \$\endgroup\$Raghunandan– Raghunandan2014年08月27日 06:31:37 +00:00Commented Aug 27, 2014 at 6:31