赞
踩
计算器,但是还有一点bug,我不知道怎么改了
文件列表
MainActivity.java
- package com.example.calculator;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.view.View;
- import android.widget.TextView;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
- private TextView tv_result;
- //第一个操作数
- private String firstNum="";
- //操作符
- private String operator="";
- //第二个操作数
- private String secoundNum="";
- //当前计算结果
- private String result="";
- //显示的文本内容
- private String showText="";
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- //从布局文件中获取名叫tv_result的文本视图
- tv_result=findViewById(R.id.tv_result);
- //下面给每个按钮控件都注册了点击监听器
- findViewById(R.id.btn_cancel).setOnClickListener(this);//C
- findViewById(R.id.btn_clear).setOnClickListener(this);
- findViewById(R.id.btn_divide).setOnClickListener(this);
- findViewById(R.id.btn_dot).setOnClickListener(this);
- findViewById(R.id.btn_eight).setOnClickListener(this);
- findViewById(R.id.btn_equal).setOnClickListener(this);
- findViewById(R.id.btn_five).setOnClickListener(this);
- findViewById(R.id.btn_four).setOnClickListener(this);
- findViewById(R.id.btn_minus).setOnClickListener(this);
- findViewById(R.id.btn_multiply).setOnClickListener(this);
- findViewById(R.id.btn_nine).setOnClickListener(this);
- findViewById(R.id.btn_one).setOnClickListener(this);
- findViewById(R.id.btn_plus).setOnClickListener(this);
- findViewById(R.id.btn_reciprocal).setOnClickListener(this);
- findViewById(R.id.btn_seven).setOnClickListener(this);
- findViewById(R.id.btn_six).setOnClickListener(this);
- findViewById(R.id.btn_sqrt).setOnClickListener(this);
- findViewById(R.id.btn_three).setOnClickListener(this);
- findViewById(R.id.btn_two).setOnClickListener(this);
- findViewById(R.id.btn_zero).setOnClickListener(this);
-
-
- }
-
-
- @Override
- public void onClick(View v) {
- String inputText;
- //如果是开根号的
- if(v.getId()==R.id.btn_sqrt){
- inputText="√";
- }
- else{
- inputText=((TextView) v).getText().toString();
- }
- switch (v.getId()){
- case R.id.btn_clear:
- clear();
- break;
- case R.id.btn_cancel:
- if(showText=="")
- break;
- else{
- showText=showText.substring(0,showText.length()-1);
- refreshText(showText);}
- break;
- case R.id.btn_plus:
- case R.id.btn_minus:
- case R.id.btn_multiply:
- case R.id.btn_divide:
- operator=inputText;
- refreshText(showText+operator);
- break;
- case R.id.btn_equal:
- if(operator==""&&firstNum!=""||secoundNum==""){//6=6,6+=6
- showText=firstNum;
- refreshText(showText);
- }else{
- double calculate_result=calculateFour();
- refreshOperate(String.valueOf(calculate_result));
- refreshText(showText+"="+result);}
- break;
- case R.id.btn_sqrt:
- double sqrt_result=Math.sqrt(Double.parseDouble(firstNum));
- refreshOperate(String.valueOf(sqrt_result));
- refreshText(showText+"√="+result);
- break;
- case R.id.btn_reciprocal:
- double reciprocal_result=1.0/Double.parseDouble(firstNum);
- refreshOperate(String.valueOf(reciprocal_result));
- refreshText("1.0/"+showText+"="+result);
- break;
- default:
- //上次结果已经出来了,在输入数字时,之前的应该清空
- if(result.length()>0&&operator.equals("")){
- clear();
- }
-
- if(operator.equals("")){//没有运算符,先输入第一个操作数
- firstNum=firstNum+inputText;
- }else//如果有运算符,就输入第二个操作数
- secoundNum=secoundNum+inputText;
-
- if(showText.equals("0")&& !inputText.equals(".")) {
- refreshText(inputText);//如果前面是一个0,后面不是小数点,而是一个数,就不需要接上前面的0,直接显示输入的数
- }else{
- refreshText(showText + inputText);//这个零的后面如果是小数点,那么前面的0就拼接上
- }
- break;
- }
- }
-
-
- //刷新文本显示
- private void refreshText(String text){
- showText=text;
- tv_result.setText(showText);
- }
-
- //清空并初始化
- private void clear(){
- refreshOperate("");
- refreshText("");
- }
-
- //刷新运算结果,第一次运算4+5=9,第二次运算时,这个9就变成了firstNum,secoundNum和operator都为空再次重新赋值
- private void refreshOperate(String new_result){
- result=new_result;
- firstNum=result;
- secoundNum="";
- operator="";
- }
-
- //四则运算
- private double calculateFour(){
- switch (operator){
- case"+":
- return Double .parseDouble(firstNum)+Double.parseDouble(secoundNum);
- case"":
- return Double .parseDouble(firstNum)-Double.parseDouble(secoundNum);
- case"×":
- return Double .parseDouble(firstNum)*Double.parseDouble(secoundNum);
- default:
- return Double .parseDouble(firstNum)/Double.parseDouble(secoundNum);
- }
- }
-
-
- }
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity"
- android:orientation="vertical"
- android:background="@color/purple_200"
- android:padding="5dp">
-
-
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/simple_calculator"
- android:lines="3"
- android:gravity="center"
- android:textSize="25sp"
- android:textColor="@color/black"/>
-
- <TextView
- android:id="@+id/tv_result"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/white"
- android:text="0"
- android:textColor="@color/black"
- android:textSize="25sp"
- android:lines="4"
- android:gravity="right|bottom"/>
-
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="4"
- android:rowCount="5">
-
- <Button
- android:id="@+id/btn_cancel"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/cancel"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- "
- <Button
- android:id="@+id/btn_divide"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/divide"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_multiply"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/multiplt"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_clear"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/clear"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- </GridLayout>
-
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="4"
- android:rowCount="5">
-
- <Button
- android:id="@+id/btn_seven"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/seven"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- "
- <Button
- android:id="@+id/btn_eight"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/eight"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_nine"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/nine"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_plus"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/plus"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- </GridLayout>
-
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="4"
- android:rowCount="5">
-
- <Button
- android:id="@+id/btn_four"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/four"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- "
- <Button
- android:id="@+id/btn_five"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/five"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_six"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/six"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_minus"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/minus"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- </GridLayout>
-
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="4"
- android:rowCount="5">
-
- <Button
- android:id="@+id/btn_one"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/one"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- "
- <Button
- android:id="@+id/btn_two"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/two"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_three"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/three"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_sqrt"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/sqrt"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- </GridLayout>
-
- <GridLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:columnCount="4"
- android:rowCount="5">
-
- <Button
- android:id="@+id/btn_reciprocal"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/reciprocal"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- "
- <Button
- android:id="@+id/btn_zero"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/zero"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_dot"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/dot"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
-
- <Button
- android:id="@+id/btn_equal"
- android:layout_width="0dp"
- android:layout_height="@dimen/button_height"
- android:layout_columnWeight="1"
- android:gravity="center"
- android:text="@string/equal"
- android:textColor="@color/black"
- android:textSize="@dimen/button_font_size"/>
- </GridLayout>
-
- </LinearLayout>
-
- </ScrollView>
-
-
- </LinearLayout>
dimens.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <dimen name="button_font_size">30sp</dimen>
- <dimen name="button_height">75dp</dimen>
-
- </resources>
strings.xml
- <resources>
- <string name="app_name">Calculator</string>
- <string name="simple_calculator">简单计算器</string>
- <string name="cancel">CE</string>
- <string name="divide">÷</string>
- <string name="multiplt">×</string>
- <string name="clear">C</string>
- <string name="seven">7</string>
- <string name="eight">8</string>
- <string name="nine">9</string>
- <string name="plus">+</string>
- <string name="four">4</string>
- <string name="five">5</string>
- <string name="six">6</string>
- <string name="minus">-</string>
- <string name="one">1</string>
- <string name="two">2</string>
- <string name="three">3</string>
- <string name="reciprocal">1/x</string>
- <string name="zero">0</string>
- <string name="dot">.</string>
- <string name="equal">=</string>
- <string name="sqrt">√ ̄</string>
- </resources>
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。