安卓实现一个计算器的功能
2018-12-13 本文已影响0人
晨曦诗雨
新建项目工程
第一步 布局的设置
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="60dp"
android:paddingRight="20dp"
android:gravity="right|center_vertical"
android:text=""
android:background="@drawable/whith_bg"
android:layout_weight="0.08" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn_c"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:background="@drawable/wight_selector"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:text="C" />
<Button
android:id="@+id/btn_del"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:background="@drawable/wight_selector"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:text="Del" />
<Button
android:id="@+id/btn_multiply"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:background="@drawable/wight_selector"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:text="*" />
<Button
android:id="@+id/btn_divide"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:background="@drawable/wight_selector"
android:textSize="20dp"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:text="÷" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
>
<Button
android:id="@+id/btn7"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:background="@drawable/wight_selector"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="7"
android:textSize="20dp" />
<Button
android:id="@+id/btn8"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/wight_selector"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="8"
android:textSize="20dp" />
<Button
android:id="@+id/btn9"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/wight_selector"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="9"
android:textSize="20dp" />
<Button
android:id="@+id/btn_decline"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:background="@drawable/wight_selector"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="-"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btn4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:background="@drawable/wight_selector"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:text="4" />
<Button
android:id="@+id/btn5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:background="@drawable/wight_selector"
android:paddingBottom="10dp"
android:text="5" />
<Button
android:id="@+id/btn6"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:gravity="right|bottom"
android:paddingLeft="10dp"
android:background="@drawable/wight_selector"
android:paddingBottom="10dp"
android:text="6" />
<Button
android:id="@+id/btn_add"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:textSize="20dp"
android:gravity="right|bottom"
android:background="@drawable/wight_selector"
android:paddingLeft="10dp"
android:paddingBottom="10dp"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<Button
android:id="@+id/btn1"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:background="@drawable/wight_selector"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="1"
android:textSize="20dp" />
<Button
android:id="@+id/btn2"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:background="@drawable/wight_selector"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="2"
android:textSize="20dp" />
<Button
android:id="@+id/btn3"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:background="@drawable/wight_selector"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="3"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginTop="10dp"
>
<Button
android:id="@+id/btn_zero"
android:layout_width="170dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:background="@drawable/wight_selector"
android:paddingLeft="10dp"
android:text="0"
android:textSize="20dp"/>
<Button
android:id="@+id/btn_point"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="."
android:background="@drawable/wight_selector"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_equit"
android:layout_width="80dp"
android:layout_height="170dp"
android:layout_marginLeft="10dp"
android:background="@drawable/yellow_bg"
android:gravity="right|bottom"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:text="="
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>
特别样式的设置 点击的背景效果展示
创建四个单独设计背景的xml文件
*按钮默认色
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--<gradient-->
<!--android:startColor="@color/colorAccent"-->
<!--android:endColor="@color/colorPrimaryDark">-->
<!--</gradient-->
>
<!--设置填充色-->
<solid android:color="#edbfe1"></solid>
<!--设置圆角-->
<corners android:radius="5dp"></corners>
<!--边框-->
<!--<stroke-->
<!--android:width="1dp"-->
<!--android:color="@color/colorAccent">-->
<!--</stroke>-->
</shape>
- 按钮点击时的背景色
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fc0505"></solid>
<corners android:radius="5dp"></corners>
</shape>
- 等于号的背景色
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fcec05"></solid>
<corners android:radius="5dp"></corners>
</shape>
- 等于号的点击背景色
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fc0505"></solid>
<corners android:radius="5dp"></corners>
</shape>
java代码的设置
初始化所有的按钮
每个按钮设置点击事件
给等于号写一个单独的方法
四种情况进行判断
1.俩个数据都为空
2.俩个数据都不为空
3.s数据为空
4.s1数据都为空
代码
private EditText editText;
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private Button btn_decline;
private Button btn_add;
private Button btn_equit;
private Button btn_c;
private Button btn_divide;
private Button btn_del;
private Button btn_multiply;
private Button btn_zero;
private Button btn_point;
boolean flag = false;//清空标识
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
btn1= (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4= (Button) findViewById(R.id.btn4);
btn5= (Button) findViewById(R.id.btn5);
btn6= (Button) findViewById(R.id.btn6);
btn7= (Button) findViewById(R.id.btn7);
btn8= (Button) findViewById(R.id.btn8);
btn9= (Button) findViewById(R.id.btn9);
btn_divide=(Button)findViewById(R.id.btn_divide);
btn_decline= (Button) findViewById(R.id.btn_decline);
btn_add= (Button) findViewById(R.id.btn_add);
btn_equit= (Button) findViewById(R.id.btn_equit);
btn_c= (Button) findViewById(R.id.btn_c);
btn_del= (Button) findViewById(R.id.btn_del);
btn_multiply= (Button) findViewById(R.id.btn_multiply);
btn_zero= (Button) findViewById(R.id.btn_zero);
btn_point= (Button) findViewById(R.id.btn_point);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_decline.setOnClickListener(this);
btn_add.setOnClickListener(this);
btn_equit.setOnClickListener(this);
btn_c.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_zero.setOnClickListener(this);
btn_point.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String str=editText.getText().toString();
switch (view.getId()){
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn4:
case R.id.btn5:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
case R.id.btn_point:
//
if (flag){
flag = false;
str="";
editText.setText("");
}
editText.setText(str+((Button)view).getText());
break;
case R.id.btn_add:
case R.id.btn_decline:
case R.id.btn_multiply:
case R.id.btn_divide:
if (flag){
flag = true;
str="";
editText.setText("");
}
editText.setText(str+" "+((Button)view).getText()+ "");
break;
case R.id.btn_del:
if (flag){
flag = true;
str="";
editText.setText("");
}
if(str!=null&& !(str.equals(""))){
editText.setText(str.substring(0,str.length() - 1));
}
break;
case R.id.btn_c:
flag = false;
str="";
editText.setText("");
break;
case R.id.btn_equit:
//防止点完等号再点等号
if (flag == true){
flag = false;
return;
}
flag = true;
gerResult();
break;
}
}
// 单独运算最后的结果
private void gerResult(){
String str=editText.getText().toString();
if(str ==null || str.equals("")){
return;
}
if (!str.contains(" ")){
return;
}
double result = 0.0;
// 有空格
String s=str.substring(0,str.indexOf(" "));//运算符前面的字符串空格
// 截取运算符
String obj=str.substring(str.indexOf(" ")+ 1,str.indexOf(" ")+2);
String s2=str.substring(str.indexOf(" ")+2);//运算符后面的字符串空格
if(!(s.equals("") && s2.equals(""))){
// 强制转换
Double dos1=Double.parseDouble(s);
Double dos2=Double.parseDouble(s2);
if(obj.equals("+")){
result = dos1 + dos2;
}else if (obj.equals("-")){
result = dos1 - dos2;
}else if (obj.equals("*")){
result = dos1 * dos2;
}else if (obj.equals("÷")){
if(dos2 == 0){
result = 0;
}else {
result = dos1 / dos2;
}
}
if (!(s.contains(".") && s2.contains(".")) && !obj.equals("÷")){
int i = (int)result;
editText.setText(i+"");
}else {
editText.setText(result+"");
}
}else if (s.equals("") && !s2.equals("")){
//强制转换
Double dos1=Double.parseDouble(s);
Double dos2=Double.parseDouble(s2);
if(obj.equals("+")){
result = 0 + dos2;
}else if (obj.equals("-")){
result = 0 - dos2;
}else if (obj.equals("*")){
result = 0;
}else if (obj.equals("÷")){
if(dos2 == 0){
result = 0;
}else {
result = dos1 / dos2;
}
}
if (!(s.contains(".") && s2.contains("."))){
int i = (int)result;
}else {
editText.setText(result+"");
}
}else if (!s.equals("") && s2.equals("")){
//强制转换
Double dos1=Double.parseDouble(s);
Double dos2=Double.parseDouble(s2);
if(obj.equals("+")){
result = dos1 + 0;
}else if (obj.equals("-")){
result = dos1- 0;
}else if (obj.equals("*")){
result = 0;
}else if (obj.equals("÷")){
if(dos2 == 0){
result = 0;
}else {
result = dos1 / dos2;
}
}
if (!(s.contains(".") && s2.contains("."))){
int i = (int)result;
}else {
editText.setText(result+"");
}
}else if (!s.equals("") && !s2.equals("")){
editText.setText("");
}
}
最后输出结果
![](https://img.haomeiwen.com/i11895905/3f0c3ce4123c3419.png)