初学android,仅能实现一些较为简单的功能,下面代码能实现加减乘除和清除功能,但是不能实现多位数的计算。

界面布置activity_main.xml:

 

<?xml version="1.0" encoding="utf-8"?>//网格布局,设置为6行4列                

 功能实现activity_main.java,主要实现按钮的监控功能和基本算法

package com.example.counter;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;public class MainActivity extends AppCompatActivity {    private EditText txt1;    private Button del, bu11, bu12, bu13, bu14, bu21, bu22, bu23, bu24, bu31, bu32, bu33, bu34, bu41, bu42, bu43, bu44;    boolean clr_flag;//判断文本框是否清空    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);//显示activity_main.xml定义的用户界面        txt1 = (EditText) findViewById(R.id.text1);        del = (Button) findViewById(R.id.delete);        bu11 = (Button) findViewById(R.id.but11);        bu12 = (Button) findViewById(R.id.but12);        bu13 = (Button) findViewById(R.id.but13);        bu14 = (Button) findViewById(R.id.but14);        bu21 = (Button) findViewById(R.id.but21);        bu22 = (Button) findViewById(R.id.but22);        bu23 = (Button) findViewById(R.id.but23);        bu24 = (Button) findViewById(R.id.but24);        bu31 = (Button) findViewById(R.id.but31);        bu32 = (Button) findViewById(R.id.but32);        bu33 = (Button) findViewById(R.id.but33);        bu34 = (Button) findViewById(R.id.but34);        bu41 = (Button) findViewById(R.id.but41);        bu42 = (Button) findViewById(R.id.but42);        bu43 = (Button) findViewById(R.id.but43);        bu44 = (Button) findViewById(R.id.but44);//与用户界面程序中的组件建立关联        del.setOnClickListener(new click());        bu11.setOnClickListener(new click());        bu12.setOnClickListener(new click());        bu13.setOnClickListener(new click());        bu14.setOnClickListener(new click());        bu21.setOnClickListener(new click());        bu22.setOnClickListener(new click());        bu23.setOnClickListener(new click());        bu24.setOnClickListener(new click());        bu31.setOnClickListener(new click());        bu32.setOnClickListener(new click());        bu33.setOnClickListener(new click());        bu34.setOnClickListener(new click());        bu41.setOnClickListener(new click());        bu42.setOnClickListener(new click());        bu43.setOnClickListener(new click());        bu44.setOnClickListener(new click());//注册监听接口    }    class click implements View.OnClickListener{        public void onClick(View v) {            String str = txt1.getText().toString();            switch (v.getId()) {                case R.id.but11:                case R.id.but12:                case R.id.but13:                case R.id.but21:                case R.id.but22:                case R.id.but23:                case R.id.but31:                case R.id.but32:                case R.id.but33:                case R.id.but42:                    if (clr_flag) {                        clr_flag = false;                        str = "";                        txt1.setText("");                    }                    txt1.setText(str + ((Button) v).getText());                    break;                case R.id.but14:                case R.id.but24:                case R.id.but34:                case R.id.but44:                    if (clr_flag) {                        clr_flag = false;                        str = "";                        txt1.setText("");                    }                    if (str.contains("+") || str.contains("-") || str.contains("*") || str.contains("/")) {                        str = str.substring(0, str.indexOf(" "));//如果文本框中包含算法符号,则将其删除                    }                    txt1.setText(str + " " + ((Button) v).getText() + " ");                    break;                case R.id.delete:                    if (clr_flag) {                        clr_flag = false;                        str = "";                        txt1.setText("");                    } else if (str != null && !str.equals("")) {                        txt1.setText(str.substring(0, str.length() - 1));//如果字符串不为空并且不包含空格,则删去文本框的最后一个字符                    }                    break;                case R.id.but43:                    getResult();                    break;            }            }            private void getResult()//等式算法            {            String exp=txt1.getText().toString();            if(exp==null||exp.equals(""))//文本框没有数字                return;            if(!exp.contains("")){//文本框只有数字                return;            }                if (clr_flag) {                    clr_flag = false;                    return;                }                clr_flag=true;                String s1=exp.substring(0,exp.indexOf(" "));//得到第一个数字                String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);//得到算法符号                String s2=exp.substring(exp.indexOf(" ")+3);//得到第二个数字                double cnt=0;                if(!s1.equals("")&&!s2.equals("")) {//正常情况计算                    double d1 = Double.parseDouble(s1);                    double d2 = Double.parseDouble(s2);                    if (op.equals("+")) {                        cnt = d1 + d2;                    }                    if (op.equals("-")) {                        cnt = d1 - d2;                    }                    if (op.equals("*")) {                        cnt = d1 * d2;                    }                    if (op.equals("/")) {                        if (d2 == 0) cnt = 0;                        else cnt = d1 / d2;                    }                    if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {                        int res = (int) cnt;                        txt1.setText(res + "");//没有小数点,输出整数                    } else {                        txt1.setText(cnt + "");//有小数点,输出double型                    }                }                    else if(s1.equals("")&&!s2.equals(".")) {//第一个数值为空,第二个数值为整数                    double d2 = Double.parseDouble(s2);                    if (op.equals("+")) {                        cnt = d2;                    }                    if (op.equals("-")) {                        cnt = 0 - d2;                    }                    if (op.equals("*")) {                        cnt = 0;                    }                    if (op.equals("/")) {                        cnt = 0;                    }                    if (!s2.contains(".")) {                        int res = (int) cnt;                        txt1.setText(res + " ");                    } else {                        txt1.setText(cnt + " ");                    }                }                else {                    txt1.setText("");//仅有算法符号,输出为空                }        }    }}

android studio 编写容易产生闪退,可以新建项目,查看会不会闪退,如果app没问题,就是代码编写错误了,这个时候可以试着注释掉一些代码,找出有问题的代码修改。

更多相关文章

  1. Android(安卓)- 开发实例(14):透明SystemBar
  2. android小功能实现之发送短信
  3. Android自动化测试初探(四): 模拟键盘鼠标事件(Socket+Instrumentat
  4. 在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)
  5. 【Android应用开发】-(19)Android(安卓)串口编程原理和实现方式
  6. android下拉刷新android-Ultra-Pull-To-Refresh使用
  7. SwipeRefreshLayout+RecyclerView实现下拉刷新上拉加载功能
  8. android 来电自动接听和自动挂断
  9. 浅谈Java中Collections.sort对List排序的两种方法

随机推荐

  1. 修改Android签名文件keystore作为eclipse
  2. Android之BroadcastReceiver的使用
  3. android使用aidl实现进程间通信的实例
  4. Android 利用程序实现GPS的打开或关闭
  5. Android(安卓)主题切换
  6. greendao3.2.0使用方法(超详细)
  7. android application access sdcard安卓
  8. 2019-08-20 Android 线性布局介绍-Linear
  9. Android:多个Activity和Intent
  10. 使用Android Studio 3.2编译android-seri