美文网首页
安卓实现一个计算器的功能

安卓实现一个计算器的功能

作者: 晨曦诗雨 | 来源:发表于2018-12-13 08:58 被阅读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("");
        }
    }

最后输出结果

image.png

相关文章

  • 安卓实现简单计算器

    实现一个计算器 ,有加减乘除功能,小数点和清除操作。 这是学校安卓老师布置的作业,计算器说实话实现起来挺多坑的,之...

  • 掌握前缀表达式丶后缀表达式

    制作安卓小项目比如一个简单计算器时,为了实现计算器‘计算’的逻辑功能,我们往往首先需要弄清楚后缀表达式丶前缀表达式...

  • 安卓实现一个计算器的功能

    新建项目工程 第一步 布局的设置 特别样式的设置 点击的背景效果展示 创建四个单独设计背景的xml文件 *按...

  • 简单实现安卓app自动更新功能

    简单实现安卓自动更新 一般的安卓app都有自动更新功能,实现app的更新,以让用户体验新版本的功能,这里也是项目中...

  • 移植wifidog至android5.1平台

    实现的功能 先使用nanohttpd在安卓内实现http server的功能,然后在已经移植并开启wifidog的...

  • 安卓黑板功能实现

    前两年的疫情爆发让线上教育特别火,很多公司选择线上办公,很多学校线上教学,不过后来受政策影响很大。这些机构学校依赖...

  • 最全面的计算器APP,小米计算器

    小米计算器,房贷计算,利率计算,科学计算,甚至还有亲戚称呼换算。如果只用一个计算器APP,就是小米计算器啦。安卓和...

  • 安卓优化系列之安卓应用的启动

    前言 目前的安卓应用基本上都趋于成熟了,所以目前对安卓开发的要求不再是实现功能了,而是要能够优化应用。然而安卓应用...

  • 安卓百度地图(五)百度地图路线规划

    其他文章:安卓百度离线地图的下载以及使用安卓百度地图(一)定位功能的实现、周边POI的检索安卓百度地图(二)地图显...

  • 安卓百度地图(七)地理围栏的建立与使用

    其他文章:安卓百度离线地图的下载以及使用安卓百度地图(一)定位功能的实现、周边POI的检索安卓百度地图(二)地图显...

网友评论

      本文标题:安卓实现一个计算器的功能

      本文链接:https://www.haomeiwen.com/subject/nhmphqtx.html