安卓开发环境下android studio简易的计算器

安卓APP简易的计算器

1.     Androidstduio布局及操作代码,采用线性布局,对按钮、文本、编辑框进行,表格布局,线性布局与表格布局相接合

2.布局代码在activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/container"
   
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="com.example.administrator.caluculator.MainActivity">

    <TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textSize="18dp"
       
android:text="2.1.1界面布局及控件介绍" />
     <LinearLayout
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:gravity="right">
    <TextView
       
android:id="@+id/textView1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="@string/textView1"/>
    <EditText
       
android:id="@+id/edit2"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:background="@android:drawable/editbox_background"
      
android:ems="10"
       
android:focusable="false"
       
android:gravity="right"
       
android:lines="1" >
    </EditText>
     </LinearLayout>
<LinearLayout
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
>
    <TextView
        
android:id="@+id/textView2"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="结果=" />
    <EditText
       
android:id="@+id/edit1"
       
android:layout_width="fill_parent"
       
android:layout_height="wrap_content"
       
android:background="@android:drawable/editbox_background"
       
android:focusable="false"
       
android:gravity="right"
       
android:lines="1" />
        </LinearLayout>
    <TableLayout
       
android:layout_width="fill_parent"
       
android:layout_height="fill_parent"
       
android:stretchColumns="0,1,2,3">
        <TableRow>
          <Button
             
android:id="@+id/buttonC"
             
android:layout_width="wrap_content"
             
android:layout_height="wrap_content"
             
android:text="归零" />
            <Button
               
android:id="@+id/buttonDel"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="退位" />
            <Button
               
android:id="@+id/buttonQau"
               
android:layout_width="fill_parent"
               
android:layout_height="wrap_content"
               
android:layout_span="2"
               
android:text="=" />
        </TableRow>
        <TableRow>
            <Button
               
android:id="@+id/button7"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="7"
               
/>
            <Button
               
android:id="@+id/button8"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="8"
               
/>
            <Button
               
android:id="@+id/button9"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="9"
               
/>
            <Button
               
android:id="@+id/buttonPlus"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="+"
               
/>
        </TableRow>
        <TableRow>
            <Button
               
android:id="@+id/button4"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="4"
               
/>
            <Button
               
android:id="@+id/button5"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="5"
               
/>
            <Button
               
android:id="@+id/button6"
               
android:layout_width="wrap_content"
                
android:layout_height="wrap_content"
               
android:text="6"
               
/>
            <Button
               
android:id="@+id/buttonDec"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="-"
               
/>
        </TableRow>
            <TableRow>
                <Button
                   
android:id="@+id/button1"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                   
android:text="1"
                   
/>
                <Button
                   
android:id="@+id/button2"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                   
android:text="2"
                   
/>
                <Button
                   
android:id="@+id/button3"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                   
android:text="3"
                   
/>
                <Button
                   
android:id="@+id/buttonMul"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                   
android:text="*"
                   
/>
            </TableRow>
            <TableRow>
                <Button
               
android:id="@+id/buttonMin"
               
android:layout_width="wrap_content"
               
android:layout_height="wrap_content"
               
android:text="+-"
                   
/>
                <Button
                   
android:id="@+id/button0"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                   
android:text="0"
                   
/>
                <Button
                   
android:id="@+id/buttonPoint"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                   
android:text="."
                   
/>
                <Button
                   
android:id="@+id/buttonDiv"
                   
android:layout_width="wrap_content"
                   
android:layout_height="wrap_content"
                    
android:text="/"
                   
/>
            </TableRow>
    </TableLayout>

</LinearLayout>

3.操作代码在只能在MainActivity中,不是所有的控件在MainActivity在包类中,红色字体不是项目自动生成的,手动添加进去。

package com.example.administrator.caluculator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    private EditText etInOut=null,etShowInfo=null;
    private Button bt0=null,bt1=null,bt2=null,bt3=null,
                    bt4=null,bt5=null, bt6=null,bt7=null,
                    bt8=null,bt9=null,btc=null,btequal=null,btplus=null;
    private int x=0,y=0,result=0, sign=0;
    private String operator="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click();
    }

    private void click() {
        etShowInfo=(EditText)findViewById(R.id.edit2);
        etInOut=(EditText)findViewById(R.id.edit1);
        bt1=(Button)findViewById(R.id.button1);
        bt2=(Button)findViewById(R.id.button2);
        bt3=(Button)findViewById(R.id.button3);
        bt4=(Button)findViewById(R.id.button4);
        bt5=(Button)findViewById(R.id.button5);
        bt6=(Button)findViewById(R.id.button6);
        bt7=(Button)findViewById(R.id.button7);
        bt8=(Button)findViewById(R.id.button8);
        bt9=(Button)findViewById(R.id.button9);
        bt0=(Button)findViewById(R.id.button0);
        btplus=(Button)findViewById(R.id.buttonPlus);
        btc=(Button)findViewById(R.id.buttonC);
        btequal=(Button)findViewById(R.id.buttonQau);
        String i=Integer.toString(x);
        etInOut.setText(i);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(1);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(2);
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(3);
            }
        });
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(4);
            }
        });
        bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(5);
            }
        });
        bt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(6);
            }
        });
        bt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(7);
            }
        });
        bt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(8);
            }
        });
        bt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(9);
            }
        });
        bt0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settext(0);
            }
        });
        btplus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setoperator("+");
            }
        });
        btequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                equal();
            }
        });
    }
    private void settext(int input){
      String i=Integer.toString(input) ;
      if(sign==0){
          etInOut.setText(i);
          x=input;
          sign=1;
      }else if(sign==2){
          etInOut.setText(i);
          y=input;
      }
    }

    private void setoperator(String op){
        if(sign==1||sign==0){
            operator=op;
            etShowInfo.setText(etInOut.getText()+op);
            etInOut.setText("");
            sign=2;
        }
    }
    private void equal(){
        if(operator.equals("+")&&sign==2){
            result=x+y;
            etShowInfo.setText(x+operator+y);
            etInOut.setText(""+result);
            sign=0;
            operator="";
            x=result;
        }
    }
}

5.浮点计算器完整程序

package com.example.administrator.caluculator;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView mTextMessage;
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    return true;
            }
            return false;
        }
    };

    private EditText etInOut=null,etShowInfo=null;
    private Button bt0=null,bt1=null,bt2=null,bt3=null,btdec=null,btdel=null,
                    bt4=null,bt5=null, bt6=null,bt7=null,btmul=null,btdiv=null,
                    bt8=null,bt9=null,btc=null,btequal=null,btplus=null,btpoint=null;
    private double x=0,y=0,result=0;
    private int sign=0;
    private int ptflag=0;
    private String operator="";
    private String op1="",op2="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        click();
    }

    private void click() {
        etShowInfo=(EditText)findViewById(R.id.edit2);
        etInOut=(EditText)findViewById(R.id.edit1);
        bt1=(Button)findViewById(R.id.button1);
        bt2=(Button)findViewById(R.id.button2);
        bt3=(Button)findViewById(R.id.button3);
        bt4=(Button)findViewById(R.id.button4);
        bt5=(Button)findViewById(R.id.button5);
        bt6=(Button)findViewById(R.id.button6);
        bt7=(Button)findViewById(R.id.button7);
        bt8=(Button)findViewById(R.id.button8);
        bt9=(Button)findViewById(R.id.button9);
        bt0=(Button)findViewById(R.id.button0);
        btplus=(Button)findViewById(R.id.buttonPlus);
        btc=(Button)findViewById(R.id.buttonC);
        btequal=(Button)findViewById(R.id.buttonQau);
        btdec=(Button)findViewById(R.id.buttonDec);
        btmul=(Button)findViewById(R.id.buttonMul);
        btdiv=(Button)findViewById(R.id.buttonDiv);
        btdel=(Button) findViewById(R.id.buttonDel);
        btpoint=(Button)findViewById(R.id.buttonPoint);
        String i=Double.toString(x);
        etInOut.setText(i);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(1);
                settexting("1");
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(2);
                settexting("2");
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(3);
                settexting("3");

            }
        });
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(4);
                settexting("4");
            }
        });
        bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(5);
                settexting("5");
            }
        });
        bt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //  settext(6);
                settexting("6");
            }
        });
        bt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(7);
                settexting("7");
            }
        });
        bt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(8);
                settexting("8");
            }
        });
        bt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(9);
                settexting("9");
            }
        });
        bt0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //settext(0);
                settexting("0");
            }
        });
        btplus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setoperator("+");
            }
        });
        btequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                equal();
            }
        });
        btdec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setoperator("-");
            }
        });//侦听“-”法按钮
        btmul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setoperator("*");
            }
        });
        btdiv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setoperator("/");
            }
        });
        btdel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
        btc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datac();
            }
        });
        btpoint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                settexting(".");
            }
        });
    }
    private void settext(int input){
      String i=Integer.toString(input) ;
      if(sign==0){
          etInOut.setText(i);
          x=input;
          sign=1;
      }else if(sign==2){
          etInOut.setText(i);
          y=input;
      }
    }

    private  void settexting(String input) {
        double i=0;

            if (sign == 0 || sign == 1) {
                op1 = op1 + input;
                i = Double.parseDouble(op1);
                if (i <= 3.4028235e38) {
                    etInOut.setText(op1);
                    x = (double) i;//强制类型转换,将字符串型转化为整型
                    sign = 1;
                    if(input.equals(".")){
                        ptflag=1;
                    }
                } else {
                    Toast.makeText(MainActivity.this, "输入的数据超出最大可计算的范围,请重新输入", Toast.LENGTH_LONG).show();
                    etInOut.setText("0.0");
                    x = 0;
                    op1="";
                }
            }else if(sign==2){
              op2=op2+input;
              i=Double.parseDouble(op2);
              if(i<=3.4028235e38){
                  etInOut.setText(op2);
                  y=(double) i;//强制类型转换,将字符串型转化为整型
                  if(input.equals(".")){
                      ptflag=1;
                  }
              }else{
                  Toast.makeText(MainActivity.this, "输入的数据超出最大可计算的范围,请重新输入", Toast.LENGTH_LONG).show();
                  etInOut.setText("0.0");
                  y = 0;
                  op2="";
              }
            }

    }
    private void setoperator(String op){
        if(ptflag==1){
            ptflag=0;
        }
        //if(sign==1||sign==0)
        if(sign!=0&&sign!=2) {
            operator=op;
                    etShowInfo.setText(etInOut.getText()+op);
                   etInOut.setText("");
                    sign=2;

        }
        if(sign==0){
            operator=op;
            etShowInfo.setText(etInOut.getText()+op);
            etInOut.setText("");
            sign=2;
        }
    }
    private void equal() {
        if (operator.equals("+") && sign == 2) {
            result = x + y;
            etShowInfo.setText(x + operator + y);
            etInOut.setText("" + result);
            sign = 0;
            operator = "";
            x = result;
            op1="";
            op2="";
        } else if (operator.equals("-") && sign == 2) {
            result = x - y;
            etShowInfo.setText(x + operator + y);
            etInOut.setText("" + result);
            sign = 0;
            operator = "";
            x = result;
            op1="";
            op2="";
        } else if (operator.equals("*") && sign == 2) {
            result = x * y;
            etShowInfo.setText(x + operator + y);
            etInOut.setText("" + result);
            sign = 0;
            operator = "";
            x = result;
            op1="";
            op2="";
        } else if (operator.equals("/") && sign == 2) {
            try {
                result = x / y;
                etShowInfo.setText(x + operator + y);
                etInOut.setText("" + result);
                sign = 0;
                operator = "";
                x = result;
                op1="";
                op2="";
            } catch (Exception e) {
            Toast.makeText(MainActivity.this,"除数不能为0,请重新输入", Toast.LENGTH_LONG).show();
            }
        }
    }
    private void  delete(){
            double i;
            if(sign==1){
                if(op1.length()-1>0){
                    char pointcheck=op1.charAt(op1.length()-1);
                    if(pointcheck=='.')ptflag=0;
                    op1=op1.substring(0,op1.length()-1);
                    etInOut.setText(op1);
                    i=Double.parseDouble(op1);
                    x=i;
                }else{
                    op1="0.0";
                    etInOut.setText(op1);
                    i=Integer.parseInt(op1);
                    x=i;
                    op1="";
                    sign=0;
                }
            }else if(sign==2){
                if(op2.length()-1>0){
                    char pointcheck=op1.charAt(op1.length()-1);//取长度"-"去前面的字符
                    if(pointcheck=='.')ptflag=0;
                    op2=op2.substring(0,op2.length()-1);//字符串从0位到退位前一位重新赋值给op2
                    etInOut.setText(op2);
                    i=Double.parseDouble(op2);
                    y=i;
                }else{
                    op1="0.0";
                    etInOut.setText(op2);
                    i=Integer.parseInt(op2);//分析的字符转化为整数
                    y=i;
                    op2="";
                    sign=0;
                }
            }
    }
    private void  datac(){
        x=0.0;y=0.0;op1="";op2="";sign=0;result=0;
        etInOut.setText("0.0");
        etShowInfo.setText("");
        operator="";
        ptflag=0;

    }


}


猜你喜欢

转载自blog.csdn.net/laiqiang9511/article/details/80191023