MainActivity.java文件
package com.example.calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
String str = "";
EditText et;
int c = 0, flag = 0;
double b = 0.0, g = 0.0, f = 0.0;
View vi;
// 计算方法
public double calculater() {
switch (c) {
case 0:
f = g;
break;
case 1:
f = b + g;
break;
case 2:
f = b - g;
break;
case 3:
f = b * g;
break;
case 4:
f = b / g;
break;
}
b = f;
c = 0;
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得按键
final Button number[] = new Button[10];
final Button fuhao[] = new Button[11];
fuhao[0] = (Button) findViewById(R.id.button01);
fuhao[1] = (Button) findViewById(R.id.button02);
fuhao[2] = (Button) findViewById(R.id.button03);
fuhao[3] = (Button) findViewById(R.id.button04);
fuhao[4] = (Button) findViewById(R.id.button05);
fuhao[5] = (Button) findViewById(R.id.button06);
fuhao[6] = (Button) findViewById(R.id.buttonce);
fuhao[7] = (Button) findViewById(R.id.buttonc);
fuhao[8] = (Button) findViewById(R.id.zheng);
fuhao[9] = (Button) findViewById(R.id.kaifang);
fuhao[10] = (Button) findViewById(R.id.pingfang);
number[0] = (Button) findViewById(R.id.button0);
number[1] = (Button) findViewById(R.id.button1);
number[2] = (Button) findViewById(R.id.button2);
number[3] = (Button) findViewById(R.id.button3);
number[4] = (Button) findViewById(R.id.button4);
number[5] = (Button) findViewById(R.id.button5);
number[6] = (Button) findViewById(R.id.button6);
number[7] = (Button) findViewById(R.id.button7);
number[8] = (Button) findViewById(R.id.button8);
number[9] = (Button) findViewById(R.id.button9);
et = (EditText) findViewById(R.id.textView1);
et.setText(str);
//shanchu
fuhao[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
str = "";
et.setText(str);
vi = v;
}
});
//shan
fuhao[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
str = "";
et.setText(str);
vi = v;
}
});
// +/-
fuhao[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (vi != fuhao[5] && str != "") {
char ch = str.charAt(0);
if (ch == '-')
str = str.replace("-", "");
else
str = "-" + str;
et.setText(str);
}
}
});
//kaifang
fuhao[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "") {
double a = Double.parseDouble(str);
str = Math.sqrt(a) + "";
et.setText(str);
}
}
});
//pingfang
fuhao[10].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "") {
double a = Double.parseDouble(str);
str = "" + a * a;
et.setText(str);
}
}
});
// 设定数字按键
number[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 0;
et.setText(str);
flag = 0;
} else {
char ch1[];
ch1 = str.toCharArray();
if (!(ch1.length == 1 && ch1[0] == '0')) {
str += 0;
et.setText(str);
}
}
vi = v;
}
});
number[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 1;
et.setText(str);
flag = 0;
} else {
str += 1;
et.setText(str);
}
vi = v;
}
});
number[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 2;
et.setText(str);
flag = 0;
} else {
str += 2;
et.setText(str);
}
vi = v;
}
});
number[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 3;
et.setText(str);
flag = 0;
} else {
str += 3;
et.setText(str);
}
vi = v;
}
});
number[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 4;
et.setText(str);
flag = 0;
} else {
str += 4;
et.setText(str);
}
vi = v;
}
});
number[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 5;
et.setText(str);
flag = 0;
} else {
str += 5;
et.setText(str);
}
vi = v;
}
});
number[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 6;
et.setText(str);
flag = 0;
} else {
str += 6;
et.setText(str);
}
vi = v;
}
});
number[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 7;
et.setText(str);
flag = 0;
} else {
str += 7;
et.setText(str);
}
vi = v;
}
});
number[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 8;
et.setText(str);
flag = 0;
} else {
str += 8;
et.setText(str);
}
vi = v;
}
});
number[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (flag == 1) {
str = "";
str += 9;
et.setText(str);
flag = 0;
} else {
str += 9;
et.setText(str);
}
vi = v;
}
});
// 设定符号键
// 加
fuhao[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "") {
if (vi == fuhao[0] || vi == fuhao[1]
|| vi == fuhao[2]|| vi == fuhao[3]) {
c = 1;
}
else {
g = Double.parseDouble(str);
calculater();
str = "" + f;
et.setText(str);
c = 1;
flag = 1;
vi = v;
}
}
}
});
// 减
fuhao[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "") {
if (vi == fuhao[0] || vi == fuhao[1] || vi == fuhao[2]
|| vi == fuhao[3]) {
c = 2;
}
else {
g = Double.parseDouble(str);
calculater();
str = "" + f;
et.setText(str);
c = 2;
flag = 1;
vi = v;
}
}
}
});
// 乘
fuhao[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "") {
if (vi == fuhao[0] || vi == fuhao[1] || vi == fuhao[2]
|| vi == fuhao[3]) {
c = 3;
}
else {
g = Double.parseDouble(str);
calculater();
str = "" + f;
et.setText(str);
c = 3;
flag = 1;
vi = v;
}
}
}
});
// 除
fuhao[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "") {
if (vi == fuhao[0] || vi == fuhao[1] || vi == fuhao[2]
|| vi == fuhao[3]) {
c = 4;
}
else {
g = Double.parseDouble(str);
calculater();
str = "" + f;
et.setText(str);
c = 4;
flag = 1;
vi = v;
}
}
}
});
// 等号
fuhao[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str != "" && vi != fuhao[0] && vi != fuhao[1]
&& vi != fuhao[2] && vi != fuhao[3]) {
g = Double.parseDouble(str);
calculater();
str = "" + f;
et.setText(str);
flag = 1;
vi = v;
}
}
});
// 小数点
fuhao[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (str == "") {
str += ".";
et.setText(str);
} else {
char ch1[];
int x = 0;
ch1 = str.toCharArray();
for (int i = 0; i < ch1.length; i++){
if (ch1[i] == '.')
x++;
}
if (x == 0) {
str += ".";
et.setText(str);
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menu.add(0, 1, 1, "退出");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText android:text="TextView"
android:id="@+id/textView1"
android:textSize="25sp"
android:textColor="#000000"
android:layout_gravity="center"
android:gravity="right"
android:cursorVisible="false"
android:editable="false"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:layout_marginTop="4dip"
android:layout_weight="1.5">
</EditText>
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dip"
android:layout_weight="10">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:id="@+id/buttonc"
android:text="@string/shan"/>
<Button
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:id="@+id/buttonce"
android:text="@string/shanchu"/>
</LinearLayout>
<TableRow
android:id="@+id/row0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/one"
android:id="@+id/button1"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/two"
android:id="@+id/button2"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/three"
android:id="@+id/button3"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/jia"
android:id="@+id/button01"/>
</TableRow>
<TableRow
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/four"
android:id="@+id/button4"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/five"
android:id="@+id/button5"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/six"
android:id="@+id/button6"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/jian"
android:id="@+id/button02"/>
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/seven"
android:id="@+id/button7"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/eight"
android:id="@+id/button8"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/nine"
android:id="@+id/button9"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/cheng"
android:id="@+id/button03"/>
</TableRow>
<TableRow
android:id="@+id/row3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/zero"
android:id="@+id/button0"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/dihao"
android:id="@+id/button04"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/dengyu"
android:id="@+id/button05"/>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp"
android:text="@string/chu"
android:id="@+id/button06"/>
</TableRow>
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:textSize="25sp"
android:layout_height="fill_parent"
android:layout_width="50dp"
android:layout_weight="1"
android:text="@string/zhengfu"
android:id="@+id/zheng"/>
<Button
android:id="@+id/pingfang"
android:textSize="25sp"
android:layout_height="fill_parent"
android:layout_width="50dp"
android:layout_weight="1"
android:text="@string/ping"/>
<Button
android:id="@+id/kaifang"
android:textSize="25sp"
android:layout_height="fill_parent"
android:layout_width="50dp"
android:layout_weight="1"
android:text="@string/kai"/>
</LinearLayout>
</TableLayout>
</LinearLayout>
<resources>
<string name="app_name">Calculator</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="zero">0</string>
<string name="jia">+</string>
<string name="jian">-</string>
<string name="cheng">*</string>
<string name="chu">/</string>
<string name="dengyu">=</string>
<string name="zhengfu">+/-</string>
<string name="dihao">.</string>
<string name="ping">x²</string>
<string name="kai">√x</string>
<string name="shanchu">ce</string>
<string name="shan">c</string>
</resources>