Android简易计算器(破烂Alpha版,后续更新)
界面布局如下:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:background="#000000"
android:textColor="#ffffff"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"
/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"
/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"
/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"
/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"
/>
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"
/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"
/>
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"
/>
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C"
/>
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
/>
<Button
android:id="@+id/btnResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
/>
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
/>
</TableRow>
</TableLayout>
</LinearLayout>
自定义操作资源类:
Type.java
public class Type {
public static final int ADD = 1;
public static final int SUB = 2;
public static final int MUL = 3;
public static final int DIV = 4;
public static final int NUM = 5;
}
Item.java
public class Item {
public Item(double value, int type) {
this.value = value;
this.type = type;
}
public double value = 0;
public int type = 0;
}
主Activity类业务操作如下:
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
private TextView textView;
private List<Item> items = new ArrayList<Item>();
private boolean judgeClear = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
findViewById(R.id.btn0).setOnClickListener(this);
findViewById(R.id.btn1).setOnClickListener(this);
findViewById(R.id.btn2).setOnClickListener(this);
findViewById(R.id.btn3).setOnClickListener(this);
findViewById(R.id.btn4).setOnClickListener(this);
findViewById(R.id.btn5).setOnClickListener(this);
findViewById(R.id.btn6).setOnClickListener(this);
findViewById(R.id.btn7).setOnClickListener(this);
findViewById(R.id.btn8).setOnClickListener(this);
findViewById(R.id.btn9).setOnClickListener(this);
findViewById(R.id.btnAdd).setOnClickListener(this);
findViewById(R.id.btnSub).setOnClickListener(this);
findViewById(R.id.btnMul).setOnClickListener(this);
findViewById(R.id.btnDiv).setOnClickListener(this);
findViewById(R.id.btnResult).setOnClickListener(this);
findViewById(R.id.btnClear).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn0:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("0");
break;
case R.id.btn1:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("1");
break;
case R.id.btn2:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("2");
break;
case R.id.btn3:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("3");
break;
case R.id.btn4:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("4");
break;
case R.id.btn5:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("5");
break;
case R.id.btn6:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("6");
break;
case R.id.btn7:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("7");
break;
case R.id.btn8:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("8");
break;
case R.id.btn9:
if(judgeClear) {
textView.setText("");
judgeClear = false;
}
textView.append("9");
break;
case R.id.btnAdd:
items.add(new Item(Double.parseDouble(textView.getText().toString()),
Type.NUM));
checkAndCompute();
items.add(new Item(0, Type.ADD));
textView.setText("");
break;
case R.id.btnSub:
items.add(new Item(Double.parseDouble(textView.getText().toString()),
Type.NUM));
checkAndCompute();
items.add(new Item(0, Type.SUB));
textView.setText("");
break;
case R.id.btnMul:
items.add(new Item(Double.parseDouble(textView.getText().toString()),
Type.NUM));
checkAndCompute();
items.add(new Item(0, Type.MUL));
textView.setText("");
break;
case R.id.btnDiv:
items.add(new Item(Double.parseDouble(textView.getText().toString()),
Type.NUM));
checkAndCompute();
items.add(new Item(0, Type.DIV));
textView.setText("");
break;
case R.id.btnResult:
items.add(new Item(Double.parseDouble(textView.getText().toString()),
Type.NUM));
checkAndCompute();
textView.setText(items.get(0).value + "");
items.clear();
judgeClear = true;
break;
case R.id.btnClear:
textView.setText("");
items.clear();
judgeClear = false;
break;
}
}
public void checkAndCompute() {
if(items.size() >= 3) {
double a = items.get(0).value;
double b = items.get(2).value;
int opt = items.get(1).type;
items.clear();
switch (opt) {
case Type.ADD:
items.add(new Item(a+b, Type.NUM));
break;
case Type.SUB:
items.add(new Item(a-b, Type.NUM));
break;
case Type.MUL:
items.add(new Item(a*b, Type.NUM));
break;
case Type.DIV:
items.add(new Item(a/b, Type.NUM));
break;
}
}
}
}