做一个简单的计算器
计算器的功能是每个手机上都有自带的功能,下面我们来学习一下如何在代码里实现计算器的效果:
首先我们现在布局里面布出计算器的界面效果:
activity-main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/zhengfu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:background="#ff808080"
android:orientation="vertical"
tools:context="text.bwei.com.jisuanqi.MainActivity">
<EditText
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:layout_marginTop="4dip"
android:layout_weight="1.5"
android:background="#fff"
android:cursorVisible="false"
android:editable="false"
android:gravity="left"
android:text="TextView"
android:textColor="#000000"
android:textSize="25sp"></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:id="@+id/linearlayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/buttonc"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/shan"
android:textSize="25sp"></Button>
<Button
android:id="@+id/buttonce"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/shanchu"
android:textSize="25sp"></Button>
</LinearLayout>
<TableRow
android:id="@+id/row0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/button1"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/one"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button2"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/two"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button3"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/three"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button01"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/jia"
android:textSize="25sp"></Button>
</TableRow>
<TableRow
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/button4"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/fore"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button5"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/five"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button6"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/six"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button02"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/jian"
android:textSize="25sp"></Button>
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/button7"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/seven"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button8"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/eight"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button9"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/nine"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button03"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/cheng"
android:textSize="25sp"></Button>
</TableRow>
<TableRow
android:id="@+id/row3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/button0"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/zero"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button06"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/dihao"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button05"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/dengyu"
android:textSize="25sp"></Button>
<Button
android:id="@+id/button04"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/chu"
android:textSize="25sp"></Button>
</TableRow>
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/zheng"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/zhengfu"
android:textSize="25sp"></Button>
<Button
android:id="@+id/pingfang"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/ping"
android:textSize="25sp"></Button>
<Button
android:id="@+id/kaifang"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/kai"
android:textSize="25sp"></Button>
</LinearLayout>
</TableLayout>
</LinearLayout>
然后是在MainActivity里面添加如下代码,也就是在activity里面定义数字的加减乘除的算法,最后计算出来的结果显示出来:
package text.bwei.com.jisuanqi;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
/**
* Called when the activity is first created.
*/
String str = "";
EditText et;
int c = 0, flag = 0;
double b = 0.0, g = 0.0, f = 0.0;
View vi;
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "退出");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId() == 1) {
finish();
}
return super.onOptionsItemSelected(item);
}
//计算方法
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
protected 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);
fuhao[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
str = "";
et.setText(str);
vi = v;
}
});
fuhao[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
b = 0.0;
c = 0;
g = 0.0;
str = "";
et.setText(str);
}
});
fuhao[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (vi != fuhao[5] && str != "") {
char ch = str.charAt(0);
if (ch == '-')
str = str.replace("-", "");
else
str = "-" + str;
et.setText(str);
}
}
});
fuhao[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (str != "") {
double a = Double.parseDouble(str);
str = Math.sqrt(a) + "";
et.setText(str);
}
}
});
fuhao[10].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (str != "") {
double a = Double.parseDouble(str);
str = "" + a * a;
et.setText(str);
}
}
});
//设定数字按键
number[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
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);
}
}
}
});
}
}
最后加上自己在String里面定义的一些参数:
<string name="app_name">简单计算器</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="fore">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>
完成,现在可以运行起来看看自己坚持的1+1是不是等于2
效果图: