/[projects]/android/Calculator/src/dk/thoerup/calculator/CalculatorActivity.java
ViewVC logotype

Contents of /android/Calculator/src/dk/thoerup/calculator/CalculatorActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 940 - (show annotations) (download)
Mon Jun 28 14:12:41 2010 UTC (13 years, 10 months ago) by torben
File size: 5357 byte(s)
More math functions
1 package dk.thoerup.calculator;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.text.Editable;
6 import android.view.View;
7 import android.widget.EditText;
8
9 public class CalculatorActivity extends Activity {
10 enum Ops {
11 None,
12 Addition,
13 Subtraction,
14 Multiplication,
15 Division,
16 Sqr,
17 Sqrt,
18 Pow,
19 Mroot
20 }
21
22
23 EditText mEdit;
24
25
26 double mVal1;
27 Ops mOperation = Ops.None;
28 boolean mClear = false;
29
30 @Override
31 public void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33 setContentView(R.layout.main);
34
35 findViewById(R.id.btn0).setOnClickListener( new NumericClick('0') );
36 findViewById(R.id.btn1).setOnClickListener( new NumericClick('1') );
37 findViewById(R.id.btn2).setOnClickListener( new NumericClick('2') );
38 findViewById(R.id.btn3).setOnClickListener( new NumericClick('3') );
39 findViewById(R.id.btn4).setOnClickListener( new NumericClick('4') );
40 findViewById(R.id.btn5).setOnClickListener( new NumericClick('5') );
41 findViewById(R.id.btn6).setOnClickListener( new NumericClick('6') );
42 findViewById(R.id.btn7).setOnClickListener( new NumericClick('7') );
43 findViewById(R.id.btn8).setOnClickListener( new NumericClick('8') );
44 findViewById(R.id.btn9).setOnClickListener( new NumericClick('9') );
45
46 findViewById(R.id.btnback).setOnClickListener( new BackClick() );
47 findViewById(R.id.btnclear).setOnClickListener( new ClearClick() );
48 findViewById(R.id.btnpoint).setOnClickListener( new PointClick() );
49
50 findViewById(R.id.btnadd).setOnClickListener( new OperationClick(Ops.Addition) );
51 findViewById(R.id.btnsub).setOnClickListener( new OperationClick(Ops.Subtraction) );
52 findViewById(R.id.btnmul).setOnClickListener( new OperationClick(Ops.Multiplication) );
53 findViewById(R.id.btndiv).setOnClickListener( new OperationClick(Ops.Division) );
54 findViewById(R.id.btnpow).setOnClickListener( new OperationClick(Ops.Pow) );
55 findViewById(R.id.btnmroot).setOnClickListener( new OperationClick(Ops.Mroot) );
56
57 findViewById(R.id.btnsqr).setOnClickListener( new SingleOperationClick(Ops.Sqr) );
58 findViewById(R.id.btnsqrt).setOnClickListener( new SingleOperationClick(Ops.Sqrt) );
59
60 findViewById(R.id.btnsum).setOnClickListener( new SumClick() );
61
62 mEdit = (EditText) findViewById(R.id.edit);
63 mEdit.setCursorVisible(false);
64
65
66 }
67
68 void setResult(double res) {
69 if ( Double.isInfinite(res) || Double.isNaN(res) ) {
70 mClear = true;
71 }
72
73
74 mEdit.setText( Double.toString(res) );
75 mOperation = Ops.None;
76 mVal1 = res;
77 }
78
79
80 class NumericClick implements View.OnClickListener {
81 char mNumval;
82
83 public NumericClick(char numval) {
84 mNumval = numval;
85 }
86
87 @Override
88 public void onClick(View v) {
89 Editable tempText = mEdit.getText();
90
91 if (tempText.toString().equals("0") || mClear==true ) {
92 mClear=false;
93 tempText.clear();
94 }
95 tempText.append(mNumval);
96 }
97
98 }
99
100 class BackClick implements View.OnClickListener {
101
102 @Override
103 public void onClick(View v) {
104 if (mClear) {
105 mClear = false;
106 mEdit.setText("0");
107 return;
108 }
109
110 Editable tempText = mEdit.getText();
111 if (tempText.length() > 0) {
112 CharSequence cs = tempText.subSequence(0, tempText.length() -1);
113 mEdit.setText(cs);
114 }
115 }
116 }
117
118 class ClearClick implements View.OnClickListener {
119
120 @Override
121 public void onClick(View v) {
122 mClear = false;
123 mEdit.setText("0");
124 mOperation = Ops.None;
125 mVal1 = 0;
126 }
127 }
128
129 class PointClick implements View.OnClickListener {
130
131 @Override
132 public void onClick(View v) {
133
134 Editable tempText = mEdit.getText();
135 if (tempText.toString().indexOf('.') == -1) {
136 tempText.append('.');
137 }
138 }
139 }
140
141 class OperationClick implements View.OnClickListener {
142 Ops mOp;
143 public OperationClick(Ops op) {
144 mOp = op;
145 }
146
147 @Override
148 public void onClick(View v) {
149 mVal1 = Double.parseDouble( mEdit.getText().toString() );
150 mEdit.setText("0");
151
152 mOperation = mOp;
153
154 }
155 }
156
157 class SingleOperationClick implements View.OnClickListener {
158 Ops mOp;
159 public SingleOperationClick(Ops op) {
160 mOp = op;
161 }
162
163 @Override
164 public void onClick(View v) {
165 mVal1 = Double.parseDouble( mEdit.getText().toString() );
166
167 double res = 0.0;
168 switch (mOp) {
169 case Sqr:
170 res = mVal1 * mVal1;
171 break;
172 case Sqrt:
173 res = Math.sqrt(mVal1);
174 break;
175 }
176 setResult(res);
177 }
178 }
179
180 class SumClick implements View.OnClickListener {
181 @Override
182 public void onClick(View v) {
183 if (mOperation == Ops.None) {
184 return;
185 }
186
187 double val2 = Double.parseDouble( mEdit.getText().toString() );
188
189 double res = 0.0;
190 switch (mOperation) {
191 case Addition:
192 res = mVal1 + val2;
193 break;
194 case Subtraction:
195 res = mVal1 - val2;
196 break;
197 case Multiplication:
198 res = mVal1 * val2;
199 break;
200 case Division:
201 res = mVal1 / val2;
202 break;
203 case Pow:
204 res = Math.pow(mVal1, val2);
205 break;
206 case Mroot:
207 res = Math.pow(val2, (1.0/mVal1) );
208 }
209
210 setResult(res);
211 }
212 }
213 }

  ViewVC Help
Powered by ViewVC 1.1.20