@Override public void onTextChanged(CharSequence input, int start, int before, int count) { if (!destinationEditNumber.hasWindowFocus() || destinationEditNumber.hasFocus() || input == null) { return; } String str = input.toString(); if ("".equals(str)) { destinationEditNumber.setText(""); return; } try { double temp = Double.parseDouble(str); double result = (option == Option.C2F) ? TemperatureConverter.celsiusToFahrenheit(temp) : TemperatureConverter.fahrenheitToCelsius(temp); String resultString = String.format("%.2f", result); destinationEditNumber.setNumber(result); destinationEditNumber.setSelection(resultString.length()); } catch (NumberFormatException ignore) { // WARNING this is generated whilst numbers are being entered, // for example just a '-' so we don't want to show the error just yet } catch (Exception e) { sourceEditNumber.setError("ERROR: " + e.getLocalizedMessage()); } }
/* * (non-Javadoc) * @see android.text.TextWatcher#onTextChanged(java.lang.CharSequence, * int, int, int) */ @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (DEBUG) { Log.d(TAG, "onTextChanged(" + s + ", " + start + ", " + before + ") ###############"); } if (!mDest.hasWindowFocus() || mDest.hasFocus() || s == null) { return; } final String str = s.toString(); if ("".equals(str)) { mDest.setText(""); return; } try { android.util.Log.v(TAG, "converting temp=" + str + "{" + Double.parseDouble(str) + "}"); final double result = convert(Double.parseDouble(str)); android.util.Log.v("TemperatureChangeWatcher", "result=" + result); // final String resutlStr = String.format("%.2f", result); mDest.setNumber(result); } catch (NumberFormatException e) { // WARNING: // this is thrown while a number is entered // for example just a '-' } catch (Exception e) { Log.e(TAG, "ERROR", e); mSource.setError("ERROR: " + e.getLocalizedMessage()); } }