zoukankan      html  css  js  c++  java
  • Android 计算器

    首先在activity_main.xml加入一个EditText

    通过xml的方式来沈成一个图像
    在drawable中新建一个white_bg.xml文件,同时选择一个shape标签
    corners设置圆角

    <corners android:radius="5dp"/>

    gradient设置颜色渐变

    <gradient 
            android:startColor="#FFFFFF"
            android:endColor="#00FFFF"
            />

    stroke设置边框的宽度和颜色

    <stroke 
            android:color="#FFFFFF"
            android:width="1dp"
            />

    将white_bg.xml部署到activity_main.xml的EditText的android:background属性中

    android:background="@drawable/white_bg"

    但是我们现在要做的计算器的EditText有特定的要求,所以注释掉white_bg.xml里面的gradient和stroke属性,添加solid属性。

    <solid android:color="#FFFFFF"/>

    需要对当前这个activity更改一下背景颜色为黑色并且没有标题栏,所以需要修改一下AndroidManifest.xml中activity的android:theme。

    android:theme="@android:style/Theme.Black.NoTitleBar"

    这里遇到了一个问题,就是在AndroidManifest.xml中修改了activity的android:theme属性后调试的时候程序出现闪退的情况,原来是因为我的MainActivity类继承自ActionBarActivity,将MainActivity类改为继承自FragmentActivity类即可解决这个问题。

    但是EditText中的内容是不可以编辑的,并且文字需要显示在右下方,所以需要设置EditText的editable和gravity属性 

    android:editable="false"
    android:gravity="right|bottom"

    然后就是繁琐的进行Button的布局的管理,因为这里用的是LinearLayout,所以要特别注意layout_weight的使用

    android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比!
    Google官方推荐,当使用weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。这样weight就可以理解为占比了。 

    修改按钮样式
    修改按钮默认颜色白色,点击后颜色红色。
    修改“=”键默认颜色绿色,点击后颜色蓝色。
    在drawable/目录下新建集中不同颜色对应的xml文件(并在创建时选择shape)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="5dp"/>
        <solid android:color="#FFFFFF"/>
    </shape>
    white_bg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="5dp"/>
        <solid android:color="#FFFF00"/>
    </shape>
    yellow_bg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="5dp"/>
        <solid android:color="#00FF00"/>
    
    </shape>
    green_bg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <corners android:radius="5dp"/>
        <solid android:color="#0000FF"/>
    
    </shape>
    blue_bg.xml

    在drawable/目录下再创建两个样式xml文件(并在创建时选择selector),一个样式要求在没有按下时显示白色,按下时显示绿色。
        <item android:drawable="@drawable/white_bg" android:state_pressed="false" />
        <item android:drawable="@drawable/green_bg" android:state_pressed="true" />
    另一个样式要求在没有按下时显示黄色,按下时显示蓝色。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/white_bg" android:state_pressed="false" />
        <item android:drawable="@drawable/green_bg" android:state_pressed="true" />
    
    </selector>
    white_selector.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/yellow_bg" android:state_pressed="false" />
        <item android:drawable="@drawable/blue_bg" android:state_pressed="true" />
    </selector>
    yellow_selector.xml

    最后给每一个button添加“android:background="@drawable/white_selector"”实现样式的转换。

    因为文字太靠边了,需设置一下内边距:android:paddingRight和android:padding:bottom。

    实例化对象

            button_0 = (Button) findViewById(R.id.btn_0);
            button_1 = (Button) findViewById(R.id.btn_1);
            button_2 = (Button) findViewById(R.id.btn_2);
            button_3 = (Button) findViewById(R.id.btn_3);
            button_4 = (Button) findViewById(R.id.btn_4);
            button_5 = (Button) findViewById(R.id.btn_5);
            button_6 = (Button) findViewById(R.id.btn_6);
            button_7 = (Button) findViewById(R.id.btn_7);
            button_8 = (Button) findViewById(R.id.btn_8);
            button_9 = (Button) findViewById(R.id.btn_9);
            button_divide = (Button) findViewById(R.id.btn_divide);
            button_multiply = (Button) findViewById(R.id.btn_multiply);
            button_minus = (Button) findViewById(R.id.btn_minus);
            button_plus = (Button) findViewById(R.id.btn_plus);
            button_clear = (Button) findViewById(R.id.btn_clear);
            button_delete = (Button) findViewById(R.id.btn_delete);
            button_dot = (Button) findViewById(R.id.btn_dot);
            button_equals = (Button) findViewById(R.id.btn_equals);

    然后就是业务逻辑的实现

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            String s = editText.getText().toString();
            if (view == button_clear) {
                s = "";
            } else if (view == button_delete) {
                s = s.length() == 0 ? s : s.substring(0, s.length()-1);
            } else if (view == button_equals) {
                int index = -1;
                for (String ss : new String[] { "x", "/", "+", "-" }) {
                    int tmpIndex = s.indexOf(ss);
                    if (tmpIndex != -1) {
                        index = tmpIndex;
                        break;
                    }
                }
                if (index != -1) {
                    String outputFormat = "%.2f";
                    try {
                        double x = Double.parseDouble(s.substring(0, index));
                        double y = Double.parseDouble(s.substring(index+1));
                        switch (s.charAt(index)) {
                            case 'x': 
                                s = String.format(outputFormat, x * y);
                                break;
                            case '/':
                                s = String.format(outputFormat, x / y);
                                break;
                            case '+':
                                s = String.format(outputFormat, x + y);
                                break;
                            case '-':
                                s = String.format(outputFormat, x - y);
                                break;
                            default: break;
                        }
                    } catch (Exception e) {
                        s = "我只会算两个数的运算,这题我不会";
                    }
                }
            } else {
                s += ((Button) view).getText() + "";
            }
            editText.setText(s);
        }
    package com.example.calculator;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends FragmentActivity implements OnClickListener {
        
        private Button button_0;
        private Button button_1;
        private Button button_2;
        private Button button_3;
        private Button button_4;
        private Button button_5;
        private Button button_6;
        private Button button_7;
        private Button button_8;
        private Button button_9;
        private Button button_clear;
        private Button button_delete;
        private Button button_divide;
        private Button button_multiply;
        private Button button_minus;
        private Button button_plus;
        private Button button_dot;
        private Button button_equals;
        private EditText editText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button_0 = (Button) findViewById(R.id.btn_0);
            button_1 = (Button) findViewById(R.id.btn_1);
            button_2 = (Button) findViewById(R.id.btn_2);
            button_3 = (Button) findViewById(R.id.btn_3);
            button_4 = (Button) findViewById(R.id.btn_4);
            button_5 = (Button) findViewById(R.id.btn_5);
            button_6 = (Button) findViewById(R.id.btn_6);
            button_7 = (Button) findViewById(R.id.btn_7);
            button_8 = (Button) findViewById(R.id.btn_8);
            button_9 = (Button) findViewById(R.id.btn_9);
            button_divide = (Button) findViewById(R.id.btn_divide);
            button_multiply = (Button) findViewById(R.id.btn_multiply);
            button_minus = (Button) findViewById(R.id.btn_minus);
            button_plus = (Button) findViewById(R.id.btn_plus);
            button_clear = (Button) findViewById(R.id.btn_clear);
            button_delete = (Button) findViewById(R.id.btn_delete);
            button_dot = (Button) findViewById(R.id.btn_dot);
            button_equals = (Button) findViewById(R.id.btn_equals);
            editText = (EditText) findViewById(R.id.editText1);
            
            button_0.setOnClickListener(this);
            button_1.setOnClickListener(this);
            button_2.setOnClickListener(this);
            button_3.setOnClickListener(this);
            button_4.setOnClickListener(this);
            button_5.setOnClickListener(this);
            button_6.setOnClickListener(this);
            button_7.setOnClickListener(this);
            button_8.setOnClickListener(this);
            button_9.setOnClickListener(this);
            button_divide.setOnClickListener(this);
            button_plus.setOnClickListener(this);
            button_minus.setOnClickListener(this);
            button_multiply.setOnClickListener(this);
            button_clear.setOnClickListener(this);
            button_delete.setOnClickListener(this);
            button_dot.setOnClickListener(this);
            button_equals.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            String s = editText.getText().toString();
            if (view == button_clear) {
                s = "";
            } else if (view == button_delete) {
                s = s.length() == 0 ? s : s.substring(0, s.length()-1);
            } else if (view == button_equals) {
                int index = -1;
                for (String ss : new String[] { "x", "/", "+", "-" }) {
                    int tmpIndex = s.indexOf(ss);
                    if (tmpIndex != -1) {
                        index = tmpIndex;
                        break;
                    }
                }
                if (index != -1) {
                    String outputFormat = "%.2f";
                    try {
                        double x = Double.parseDouble(s.substring(0, index));
                        double y = Double.parseDouble(s.substring(index+1));
                        switch (s.charAt(index)) {
                            case 'x': 
                                s = String.format(outputFormat, x * y);
                                break;
                            case '/':
                                s = String.format(outputFormat, x / y);
                                break;
                            case '+':
                                s = String.format(outputFormat, x + y);
                                break;
                            case '-':
                                s = String.format(outputFormat, x - y);
                                break;
                            default: break;
                        }
                    } catch (Exception e) {
                        s = "我只会算两个数的运算,这题我不会";
                    }
                }
            } else {
                s += ((Button) view).getText() + "";
            }
            editText.setText(s);
        }
    
    }
    MainActivity.java
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/white_bg"
            android:editable="false"
            android:gravity="right|bottom"
            android:textSize="40sp"
            android:layout_margin="10dp"
            >
        </EditText>
    
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            >
        
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="CLS"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_clear"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="DEL"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_delete"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="/"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_divide"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="x"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_multiply"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
               
        </LinearLayout>
        
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            >
        
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="7"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_7"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="8"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_8"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="9"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_9"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="-"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_minus"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
               
        </LinearLayout>
        
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_gravity="center_horizontal"
            >
        
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="4"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_4"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="5"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_5"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="6"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_6"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            <Button 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="+"
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_plus"
                android:background="@drawable/white_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
               
        </LinearLayout>
        
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:layout_gravity="center_vertical"
            android:orientation="horizontal"
            >
            
            <LinearLayout 
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:orientation="vertical"
                >
                
                <LinearLayout 
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal"
                    >
                    
                    <Button 
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="1"
                        android:textSize="20sp"
                        android:gravity="right|bottom"
                        android:id="@+id/btn_1"
                        android:background="@drawable/white_selector"
                        android:paddingRight="10dp"
                        android:paddingBottom="10dp"
                        />
                    <Button 
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="2"
                        android:textSize="20sp"
                        android:gravity="right|bottom"
                        android:id="@+id/btn_2"
                        android:background="@drawable/white_selector"
                        android:paddingRight="10dp"
                        android:paddingBottom="10dp"
                        />
                    <Button 
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="3"
                        android:textSize="20sp"
                        android:gravity="right|bottom"
                        android:id="@+id/btn_3"
                        android:background="@drawable/white_selector"
                        android:paddingRight="10dp"
                        android:paddingBottom="10dp"
                        />
                    
                </LinearLayout>
                
                <LinearLayout 
                    android:layout_width="fill_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_gravity="center_vertical"
                    android:orientation="horizontal"
                    >
                    
                    <Button 
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="2"
                        android:text="0"
                        android:textSize="20sp"
                        android:gravity="right|bottom"
                        android:id="@+id/btn_0"
                        android:background="@drawable/white_selector"
                        android:paddingRight="10dp"
                        android:paddingBottom="10dp"
                        />
                    <Button 
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:text="."
                        android:textSize="20sp"
                        android:gravity="right|bottom"
                        android:id="@+id/btn_dot"
                        android:background="@drawable/white_selector"
                        android:paddingRight="10dp"
                        android:paddingBottom="10dp"
                        />
                    
                </LinearLayout>
                
                
            </LinearLayout>
            
            <Button 
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="="
                android:textSize="20sp"
                android:gravity="right|bottom"
                android:id="@+id/btn_equals"
                android:background="@drawable/yellow_selector"
                android:paddingRight="10dp"
                android:paddingBottom="10dp"
                />
            
        </LinearLayout>
        
    </LinearLayout>
    activity_main.xml

    项目代码:https://github.com/moonlightpoet/Calculator

    apk下载:https://raw.githubusercontent.com/moonlightpoet/Calculator/master/bin/resources.ap_

    效果:

  • 相关阅读:
    如何在VIM中编辑并保存退出!!!
    MapReduce---数据清洗
    构建之法阅读笔记03
    人月神话阅读笔记03
    学习进度报告16
    大道至简阅读笔记03
    大道至简阅读笔记02
    个人总结
    大道至简阅读笔记01
    学习进度报告15
  • 原文地址:https://www.cnblogs.com/moonlightpoet/p/5410430.html
Copyright © 2011-2022 走看看