栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android设计实现一个简单计算器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android设计实现一个简单计算器

记录下自己写的作业
本来是用double类型的,有小数点的功能
但是经过老师的测试bug一堆,所以还是换成int了。
有小数点的有机会在实现吧

注释里面都有,写完我自己都不想看,希望以后还是能规范码字,该写成函数的写成函数

MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    final String TAG = "zx";
    //第一个数
    int FirstNum = 0;
    //第二个数
    int SecondNum = 0;
    //结果
    int result = 0;
    EditText editText1 = null;
    EditText editText2 = null;
    String text ="";
    String text2 = "";
    //判断是否是第一个数
    Boolean isFirstNum = true;
    //是否是 + 号
    Boolean isAdd = false;
    //是否是 - 号
    Boolean isMinus = false;
    // 是否是小数
    Boolean isDecimal = false;
    // 是否清屏
    Boolean isClear = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1 = (EditText)findViewById(R.id.editText1);
        editText2 = (EditText)findViewById(R.id.editText2);
    }


    @Override
    public void onClick(View v) {
        if (v instanceof Button){
            String text = ((Button)v).getText().toString();
            switch (text) {
                case "0" :
                    //判断是否需要清屏 如果计算后直接输入+号就不用清屏
                    if (isClear){
                        Clear();
                    }
                    //判断是不是第一个数
                    if (isFirstNum){
                        //如果等于第一位0则返回
                        if (FirstNum == 0){
                            break;
                        }else {
                            //不是第一位 则将第一个数×10
                            FirstNum *= 10;
                            text2 += text;
                            editText1.setText(text2);
                        }
                    } else {
                        //第二个数 同上
                        if (SecondNum == 0){
                            break;
                        }else {
                            SecondNum *= 10;
                            text2 += text;
                            editText1.setText(text2);
                        }
                    }
                    break;
                case "1" :
                case "2" :
                case "3" :
                case "4" :
                case "5" :
                case "6" :
                case "7" :
                case "8" :
                case "9" :
                    //判断是否需要清屏
                    if (isClear){
                        Clear();
                    }
                    // 判断是不是第一个数
                    if (isFirstNum){
                        FirstNum *= 10 ;
                        FirstNum += Integer.parseInt(text);
                        Log.d(TAG, "onClick:FirstNum "+FirstNum);
                        text2 += text;
                        editText1.setText(text2);
                    } else {
                        SecondNum *= 10 ;
                        SecondNum += Integer.parseInt(text);
                        Log.d(TAG, "onClick:SecondNum "+SecondNum);
                        text2 += text;
                        editText1.setText(text2);
                    }
                    break;
                case "+" :
                    //如果已经计算一次 ,且再次按了运算符号,则将上一次运算结果赋值给FirstNum,且不需要清屏
                    if (result !=0 && FirstNum ==0){
                        FirstNum = result;
                        isClear = false;
                    }
                    //如果多次按 + 号 无反应
                    if (isAdd == true){
                    //按了 - 号再按 + 号,则修改逻辑,并且将屏幕上的运算符号换成最新的
                    } else if (isMinus == true){
                        isMinus = false;
                        text2 = text2.substring(0,text2.length()-1);
                        text2 += text;
                        editText1.setText(text2);
                        isAdd = true;
                        isFirstNum = false;
                    } else {
                        //第一次按 + 号
                        isAdd = true;
                        isFirstNum = false;
                        text2 += text;
                        editText1.setText(text2);
                    }
                    break;
                case "-" :
                    //逻辑同上
                    if (result !=0 && FirstNum ==0){
                        FirstNum = result;
                        isClear = false;
                    }
                    if (isMinus == true){

                    } else if (isAdd == true){
                        isAdd = false;
                        text2 = text2.substring(0,text2.length()-1);
                        text2 += text;
                        editText1.setText(text2);
                        isMinus = true;
                        isFirstNum = false;

                    } else {
                        isMinus = true;
                        isFirstNum = false;
                        text2 += text;
                        editText1.setText(text2);

                    }

                    break;
                case "=" :
                    //如果都没有值 则不作计算
                    if (FirstNum == 0 && SecondNum == 0){
                        break;
                    }
                    if (isAdd){
                        result = FirstNum + SecondNum;
                    } else if (isMinus){
                        result = FirstNum - SecondNum;
                    }else {
                        if (FirstNum != 0){
                            result = FirstNum;
                        } else {
                            result = SecondNum;
                        }
                    }
                    FirstNum = 0;
                    SecondNum = 0;
                    isAdd = false;
                    isMinus = false;
                    isFirstNum = true;
                    isClear = true;
                    text2 = String.valueOf(result);
                    editText2.setText(String.valueOf(result));
                    break;
                //清除数据
                case "AC" :
                    text = "";
                    text2 = "";
                    FirstNum = 0;
                    SecondNum = 0;
                    editText1.setText(text);
                    editText2.setText(text);
                    isFirstNum = true;
                    isAdd = false;
                    isMinus = false;
                    isClear = true;
                    break;
            }
        }
    }
    //清空text2
    public void Clear(){
        text2 = "";
        editText1.setText(text2);
        isClear = false;
    }
}

还是用笨办法给每个button设置了点击事件,老师的那种给所有控件设置监听然后在判断其类型是不是button还是不会,找了好多资料都不知道。。

布局文件


    
    
    
        
            
                
测试运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/681282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号