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

新建项目工程

第一步 布局的设置

<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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明先生_X自主阅读 15,981评论 3 119
  • 看完蔡崇达先生写的《皮囊》之后,内心感触也挺大,在书最后的扉页中写着类似于这样的一句话:读书就像是一台光学仪器,你...
    去去去hhhh阅读 201评论 0 0
  • 今天国庆放假了 回到了一个月未归的家,见到了许久未见的父母 内心很激动,很想他们,我想无论我今后到哪里,心中始终最...
    Lemon_XIXI阅读 254评论 0 0
  • 算来迄今为止,有近一半日子是在城市度过的。在家乡的前半段日子,童年青少年,上学读书长大。真正了解社会,认识生活,是...
    昇夫阅读 456评论 3 3