1 | /** |
2 | * Copyright (C) 2010-2011 Diego Torres Milano |
3 | */ |
4 | package com.example.aatg.tc; |
5 | |
6 | import android.app.Activity; |
7 | import android.os.Bundle; |
8 | import android.os.Message; |
9 | import android.text.Editable; |
10 | import android.text.TextWatcher; |
11 | import android.view.View; |
12 | import android.widget.Button; |
13 | |
14 | import com.example.aatg.tc.TemperatureConverter.OP; |
15 | |
16 | |
17 | public class TemperatureConverterActivity extends Activity { |
18 | |
19 | /** |
20 | * Changes fields values when text changes applying the corresponding method. |
21 | * |
22 | */ |
23 | public class TemperatureChangedWatcher implements TextWatcher { |
24 | |
25 | private final EditNumber mSource; |
26 | private final EditNumber mDest; |
27 | private OP mOp; |
28 | |
29 | /** |
30 | * @param mDest |
31 | * @param convert |
32 | * @throws NoSuchMethodException |
33 | * @throws SecurityException |
34 | */ |
35 | public TemperatureChangedWatcher(TemperatureConverter.OP op) { |
36 | if ( op == OP.C2F ) { |
37 | this.mSource = mCelsius; |
38 | this.mDest = mFahrenheit; |
39 | } |
40 | else { |
41 | this.mSource = mFahrenheit; |
42 | this.mDest = mCelsius; |
43 | } |
44 | this.mOp = op; |
45 | } |
46 | |
47 | /* (non-Javadoc) |
48 | * @see android.text.TextWatcher#afterTextChanged(android.text.Editable) |
49 | */ |
50 | public void afterTextChanged(Editable s) { |
51 | // TODO Auto-generated method stub |
52 | |
53 | } |
54 | |
55 | /* (non-Javadoc) |
56 | * @see android.text.TextWatcher#beforeTextChanged(java.lang.CharSequence, int, int, int) |
57 | */ |
58 | public void beforeTextChanged(CharSequence s, int start, int count, |
59 | int after) { |
60 | // TODO Auto-generated method stub |
61 | |
62 | } |
63 | |
64 | /* (non-Javadoc) |
65 | * @see android.text.TextWatcher#onTextChanged(java.lang.CharSequence, int, int, int) |
66 | */ |
67 | public void onTextChanged(CharSequence s, int start, int before, |
68 | int count) { |
69 | if (!mDest.hasWindowFocus() || mDest.hasFocus() || s == null ) { |
70 | return; |
71 | } |
72 | |
73 | final String str = s.toString(); |
74 | if ( "".equals(str) ) { |
75 | mDest.setText(""); |
76 | return; |
77 | } |
78 | |
79 | try { |
80 | final double temp = Double.parseDouble(str); |
81 | final double result = (mOp == OP.C2F) ? TemperatureConverter.celsiusToFahrenheit(temp) : |
82 | TemperatureConverter.fahrenheitToCelsius(temp); |
83 | final String resultString = String.format("%.2f", result); |
84 | mDest.setNumber(result); |
85 | mDest.setSelection(resultString.length()); |
86 | } catch (NumberFormatException e) { |
87 | // WARNING |
88 | // this is generated while a number is entered, |
89 | // for example just a '-' |
90 | // so we don't want to show the error |
91 | } catch (Exception e) { |
92 | mSource.setError("ERROR: " + e.getLocalizedMessage()); |
93 | } |
94 | } |
95 | |
96 | } |
97 | |
98 | private EditNumber mCelsius; |
99 | private EditNumber mFahrenheit; |
100 | private Button mErrorButton; |
101 | private int mString; |
102 | |
103 | /** Called when the activity is first created. */ |
104 | @Override |
105 | public void onCreate(Bundle savedInstanceState) { |
106 | super.onCreate(savedInstanceState); |
107 | setContentView(R.layout.main); |
108 | |
109 | mCelsius = (EditNumber) findViewById(R.id.celsius); |
110 | mFahrenheit = (EditNumber) findViewById(R.id.fahrenheit); |
111 | |
112 | mCelsius.addTextChangedListener(new TemperatureChangedWatcher(OP.C2F)); |
113 | mFahrenheit.addTextChangedListener(new TemperatureChangedWatcher(OP.F2C)); |
114 | |
115 | } |
116 | } |