目录
Android Studio 简单项目——简易计算器
一、前言
二、设计界面
三、主要代码
四、完整资源
软件:Android Studio
需求:学习Android Studio,做一个简易计算器深入学习
(附:刚开始学习,部分代码参考,在此基础上增删改)
主要功能实现:
1.简单计算器加减乘除
2.错误提示
3.追加模式
4.写入读取
1.主界面
2.功能解释
①第一行输入数据
第二行为输出结果
②为错误提示行
如果除数为0则算式无意义,输出错误。如下图
③计算器键盘
CE 为清零 , BA 为清除
①写入读取提示
② 读取成功,会出现读取算是
③计算表达式
1.MainActivity
package com.example.administrator.helloworld; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends Activity { /** * Called when the activity is first created. */ private EditText output = null; private EditText input = null; private Button btn0 = null; private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; private Button btn5 = null; private Button btn6 = null; private Button btn7 = null; private Button btn8 = null; private Button btn9 = null; private Button btnadd = null; private Button btnsubtract = null; private Button btnmultiply = null; private Button btndivide = null; private Button btnclear = null; private Button btnresult = null; private Button btndot = null; private Button btnback = null; private EditText errorzero = null; private EditText resultText = null; private Button writeButton = null; private Button readButton = null; private CheckBox appendBox = null; private EditText textView = null; private EditText displayView = null; public String FILE_NAME = "fileDemo.txt"; private String str = "";//保存数字 private String strold = "";//原数字 private char act = ' ';//记录“加减乘除等于”符号 private int count = 0;//判断要计算的次数,如果超过一个符号,先算出来一部分 private Float result = null;//计算的输出结果 private Boolean errBoolean = false;//有错误的时候为true,无错为false private Boolean flagBoolean = false;//一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息 private Boolean flagDot = false; //小数点标志位 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); output = (EditText) findViewById(R.id.output); input = (EditText) findViewById(R.id.input); errorzero = (EditText) findViewById(R.id.errorzero); resultText = (EditText) findViewById(R.id.resultText); writeButton = (Button) findViewById(R.id.writeButton); readButton = (Button) findViewById(R.id.readButton); textView = (EditText) findViewById(R.id.textView); displayView = (EditText) findViewById(R.id.displayView); appendBox = (CheckBox) findViewById(R.id.appendBox); btn0 = (Button) findViewById(R.id.zero); btn1 = (Button) findViewById(R.id.one); btn2 = (Button) findViewById(R.id.two); btn3 = (Button) findViewById(R.id.three); btn4 = (Button) findViewById(R.id.four); btn5 = (Button) findViewById(R.id.five); btn6 = (Button) findViewById(R.id.six); btn7 = (Button) findViewById(R.id.seven); btn8 = (Button) findViewById(R.id.eight); btn9 = (Button) findViewById(R.id.nine); btnadd = (Button) findViewById(R.id.add); btnsubtract = (Button) findViewById(R.id.subtract); btnmultiply = (Button) findViewById(R.id.multiply); btndivide = (Button) findViewById(R.id.divide); btnclear = (Button) findViewById(R.id.clear); btnback = (Button) findViewById(R.id.back); btnresult = (Button) findViewById(R.id.result); btndot = (Button) findViewById(R.id.dot); //设置按钮侦听事件 btn0.setOnClickListener(listener); btn1.setOnClickListener(listener); btn2.setOnClickListener(listener); btn3.setOnClickListener(listener); btn4.setOnClickListener(listener); btn5.setOnClickListener(listener); btn6.setOnClickListener(listener); btn7.setOnClickListener(listener); btn8.setOnClickListener(listener); btn9.setOnClickListener(listener); //执行运算 btnadd.setOnClickListener(listener); btnsubtract.setOnClickListener(listener); btnmultiply.setOnClickListener(listener); btndivide.setOnClickListener(listener); btnclear.setOnClickListener(listener); btnresult.setOnClickListener(listener); btnback.setOnClickListener(listener); btndot.setOnClickListener(listener); writeButton.setOnClickListener(writelistener); readButton.setOnClickListener(readlistener); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. } private OnClickListener listener = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { //输入数字 case R.id.zero: num(0); break; case R.id.one: num(1); break; case R.id.two: num(2); break; case R.id.three: num(3); break; case R.id.four: num(4); break; case R.id.five: num(5); break; case R.id.six: num(6); break; case R.id.seven: num(7); break; case R.id.eight: num(8); break; case R.id.nine: num(9); break; case R.id.dot: dot(); break; //执行运算 case R.id.add: add(); break; case R.id.subtract: sub(); break; case R.id.multiply: multiply(); break; case R.id.divide: divide(); break; case R.id.clear: clear(); break; case R.id.back: back(); break; //计算结果 case R.id.result: result(); if (!errBoolean && flagBoolean) { output.setText(String.valueOf(result)); } resultText.setText(strold + act + str + "=" + result+" "); break; default: break; } input.setText(strold + act + str); output.setText(String.valueOf(result)); } }; private OnClickListener writelistener = new OnClickListener() { @Override public void onClick(View view) { //textView.setText(""); FileOutputStream fos = null; try { if (appendBox.isChecked()) { fos = openFileOutput(FILE_NAME, Context.MODE_APPEND); } else { fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE); } String text = resultText.getText().toString(); fos.write(text.getBytes()); textView.setText("文件写入成功,写入长度:" + text.length()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) try { fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }; private OnClickListener readlistener = new OnClickListener() { @Override public void onClick(View view) { displayView.setText(""); FileInputStream fis = null; try { fis = openFileInput(FILE_NAME); if (fis.available() == 0) { return; } byte[] readBytes = new byte[fis.available()]; while (fis.read(readBytes) != -1) { } String text = new String(readBytes); displayView.setText(text); textView.setText("文件读取成功,写入长度:" + text.length()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; private void dot() { // TODO Auto-generated method stub if (!flagDot) { str = str + "."; flagBoolean = false; flagDot = true; } } private void back() { str = strold = ""; count = 0; act = ' '; result = null; flagBoolean = false; flagDot = false; resultText.setText(""); input.setText(strold + act + str); /* if(str==null){ str=""; input.setText(""); } else if(str!=null&&!str.equals("")){ input.setText(str.substring(0,str.length()-1)); } */ } private void clear() { // TODO Auto-generated method stub str = strold = ""; count = 0; act = ' '; result = null; flagBoolean = false; flagDot = false; input.setText(strold + act + str); output.setText(""); errorzero.setText(""); displayView.setText(""); textView.setText(""); resultText.setText(""); } private void divide() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '/'; flagBoolean = false; } } private void multiply() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '*'; flagBoolean = false; } } private void sub() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '-'; flagBoolean = false; } } private void add() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '+'; flagBoolean = false; } } private void check() { // TODO Auto-generated method stub if (count >= 1) { result(); str = String.valueOf(result); } strold = str; str = ""; count++; flagDot = false; errorzero.setText(""); } //计算输出结果 private void result() { // TODO Auto-generated method stub if (flagBoolean) { Float a, b; a = Float.parseFloat(strold); b = Float.parseFloat(str); if (b == 0 && act == '/') { clear(); errorzero.setText("除数不能为零!"); //errBoolean=true; } if (!errBoolean) { switch (act) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; default: break; } } } } private void num(int i) { // TODO Auto-generated method stub str = str + String.valueOf(i); flagBoolean = true; errorzero.setText(""); } }2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/input" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:editable="false" android:hint="@string/shuru" /> <EditText android:id="@+id/output" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:editable="true" android:gravity="right" android:hint="@string/shuchu" /> <EditText android:id="@+id/errorzero" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:editable="false" android:gravity="center" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/seven" android:layout_width="80dp" android:layout_height="70dp" android:layout_alignParentLeft="true" android:text="@string/seven" android:textSize="40sp" /> <Button android:id="@+id/eight" android:layout_width="80dp" android:layout_height="70dp" android:layout_toRightOf="@id/seven" android:text="@string/eight" android:textSize="40sp" /> <Button android:id="@+id/nine" android:layout_width="80dp" android:layout_height="70dp" android:layout_toRightOf="@id/eight" android:text="@string/nine" android:textSize="40sp" /> <Button android:id="@+id/add" android:layout_width="80dp" android:layout_height="70dp" android:layout_alignParentRight="true" android:layout_toRightOf="@id/nine" android:background="#FF9800" android:text="@string/add" android:textSize="40sp" /> <Button android:id="@+id/four" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/seven" android:layout_alignParentLeft="true" android:text="@string/four" android:textSize="40sp" /> <Button android:id="@+id/five" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/eight" android:layout_toRightOf="@id/four" android:text="@string/five" android:textSize="40sp" /> <Button android:id="@+id/six" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/nine" android:layout_toRightOf="@id/five" android:text="@string/six" android:textSize="40sp" /> <Button android:id="@+id/subtract" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/add" android:layout_alignParentRight="true" android:layout_toRightOf="@id/six" android:background="#FF9800" android:text="@string/subtract" android:textSize="40sp" /> <Button android:id="@+id/one" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/four" android:layout_alignParentLeft="true" android:text="@string/one" android:textSize="40sp" /> <Button android:id="@+id/two" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/five" android:layout_toRightOf="@id/one" android:text="@string/two" android:textSize="40sp" /> <Button android:id="@+id/three" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/six" android:layout_toRightOf="@id/two" android:text="@string/three" android:textSize="40sp" /> <Button android:id="@+id/multiply" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/subtract" android:layout_alignParentRight="true" android:layout_toRightOf="@id/three" android:background="#FF9800" android:text="@string/multiply" android:textSize="40sp" /> <Button android:id="@+id/zero" android:layout_width="240dp" android:layout_height="70dp" android:layout_below="@id/three" android:layout_alignParentLeft="true" android:text="@string/zero" android:textSize="40sp" /> <Button android:id="@+id/clear" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/zero" android:layout_alignParentLeft="true" android:text="@string/clear" android:textSize="40sp" /> <Button android:id="@+id/back" android:layout_width="80dp" android:layout_height="70dp" android:layout_toRightOf="@id/clear" android:layout_below="@id/zero" android:text="@string/back" android:textSize="40sp" /> <Button android:id="@+id/result" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/divide" android:layout_alignParentRight="true" android:layout_toRightOf="@id/dot" android:background="#FFC107" android:text="@string/result" android:textSize="40sp" /> <Button android:id="@+id/divide" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/multiply" android:layout_alignParentRight="true" android:layout_toRightOf="@id/zero" android:background="#FF9800" android:text="@string/divide" android:textSize="40sp" /> <Button android:id="@+id/dot" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/zero" android:layout_toRightOf="@id/back" android:text="@string/dot" android:textSize="40sp" /> <Button android:id="@+id/writeButton" android:layout_width="160dp" android:layout_height="70dp" android:layout_below="@id/result" android:layout_alignParentLeft="true" android:text="@string/write" android:textSize="40sp" /> <Button android:id="@+id/readButton" android:layout_width="170dp" android:layout_height="70dp" android:layout_below="@id/result" android:layout_alignParentRight="true" android:text="@string/read" android:textSize="40sp" /> <CheckBox android:id="@+id/appendBox" android:layout_width="80dp" android:layout_height="70dp" android:layout_below="@id/dot" android:layout_toRightOf="@id/writeButton" android:layout_alignParentBottom="true" android:text="@string/appendBox" /> </RelativeLayout> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView" /> <EditText android:id="@+id/displayView" android:layout_width="match_parent" android:layout_height="44dp" /> <EditText android:id="@+id/resultText" android:layout_width="fill_parent" android:layout_height="45dp" android:layout_gravity="center" android:editable="false" android:gravity="left" android:text="@string/resultText" /> </LinearLayout> </ScrollView>https://download.csdn.net/download/qi2456/11203862