计算器
Posted beens
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了计算器相关的知识,希望对你有一定的参考价值。
layout代码:
<?xml version="1.0" encoding="utf-8"?> <GridLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.wang.jisuanji.MainActivity" android:rowCount="6" android:columnCount="4"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ccc" android:layout_columnSpan="4" android:layout_rowWeight="1" android:textSize="40sp" android:gravity="right|center_vertical" android:id="@+id/input"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ccc" android:layout_columnSpan="4" android:layout_rowWeight="1" android:textSize="40sp" android:gravity="right|center_vertical" android:id="@+id/output"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="C" android:textSize="25sp" android:id="@+id/bt_C" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="DEL" android:textSize="25sp" android:id="@+id/bt_DEL" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="÷" android:textSize="25sp" android:id="@+id/bt_chu" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="×" android:textSize="25sp" android:id="@+id/bt_cheng" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="7" android:textSize="25sp" android:id="@+id/bt_7" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="8" android:textSize="25sp" android:id="@+id/bt_8" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="9" android:textSize="25sp" android:id="@+id/bt_9" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="-" android:textSize="25sp" android:id="@+id/bt_jian" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="4" android:textSize="25sp" android:id="@+id/bt_4" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="5" android:textSize="25sp" android:id="@+id/bt_5" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="6" android:textSize="25sp" android:id="@+id/bt_6" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="+" android:onClick="jia_OnClick" android:textSize="25sp" android:id="@+id/bt_jia" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="1" android:textSize="25sp" android:id="@+id/bt_1" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="2" android:textSize="25sp" android:id="@+id/bt_2" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="3" android:textSize="25sp" android:id="@+id/bt_3" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="=" android:textSize="25sp" android:id="@+id/bt_deng" android:onClick="deng_OnClick" android:layout_rowSpan="2" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="0" android:textSize="25sp" android:layout_columnSpan="2" android:id="@+id/bt_0" android:layout_columnWeight="1" android:layout_rowWeight="1"/> <Button android:layout_width="0dp" android:layout_height="0dp" android:text="." android:textSize="40sp" android:id="@+id/bt_dian" android:layout_columnWeight="1" android:layout_rowWeight="1"/> </GridLayout>
Activity代码:
package com.example.my.myapplication; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Testjisuanqei extends Activity { //单目运算可以实现连续进行,实现不了运算符之间的运算 String str = ""; String str1=""; String str2=""; String string=""; String stringq=""; String stringh=""; String[] strings=new String[200]; String[] strings1=new String[200]; TextView input; TextView output; Button bt_0; Button bt_1; Button bt_2; Button bt_3; Button bt_4; Button bt_5; Button bt_6; Button bt_7; Button bt_8; Button bt_9; Button bt_jia; Button bt_jian; Button bt_cheng; Button bt_chu; Button bt_DEL; Button bt_C; Button bt_dian; Button bt_deng; int i, j; int g =0; int k=0; double a = 0; double b = 0; double c=0 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_testjisuanqei); input = (TextView) findViewById(R.id.input); output = (TextView) findViewById(R.id.output); bt_0 = (Button) findViewById(R.id.bt_0); bt_1 = (Button) findViewById(R.id.bt_1); bt_2 = (Button) findViewById(R.id.bt_2); bt_3 = (Button) findViewById(R.id.bt_3); bt_4 = (Button) findViewById(R.id.bt_4); bt_5 = (Button) findViewById(R.id.bt_5); bt_6 = (Button) findViewById(R.id.bt_6); bt_7 = (Button) findViewById(R.id.bt_7); bt_8 = (Button) findViewById(R.id.bt_8); bt_9 = (Button) findViewById(R.id.bt_9); bt_jia = (Button) findViewById(R.id.bt_jia); bt_jian = (Button) findViewById(R.id.bt_jian); bt_cheng = (Button) findViewById(R.id.bt_cheng); bt_chu = (Button) findViewById(R.id.bt_chu); bt_dian = (Button) findViewById(R.id.bt_dian); bt_DEL = (Button) findViewById(R.id.bt_DEL); bt_C = (Button) findViewById(R.id.bt_C); bt_deng = (Button) findViewById(R.id.bt_deng); Bt_OnClickListener bt_onClickListener = new Bt_OnClickListener(); bt_0.setOnClickListener(bt_onClickListener); bt_1.setOnClickListener(bt_onClickListener); bt_2.setOnClickListener(bt_onClickListener); bt_3.setOnClickListener(bt_onClickListener); bt_4.setOnClickListener(bt_onClickListener); bt_5.setOnClickListener(bt_onClickListener); bt_6.setOnClickListener(bt_onClickListener); bt_7.setOnClickListener(bt_onClickListener); bt_8.setOnClickListener(bt_onClickListener); bt_9.setOnClickListener(bt_onClickListener); bt_dian.setOnClickListener(bt_onClickListener); //bt_jia.setOnClickListener(bt_onClickListener); bt_C.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { str = "0"; input.setText(str); str = ""; output.setText(""); string=""; a=0; k=g=0; } }); bt_jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button bt = (Button) v; str1 = str;//str1放前一个数据 // string = str;//存放加号以前的数据 str = str + bt.getText().toString(); strings[g]=str; Log.e("TAG","strings["+g+"]="+strings[g]); g++; input.setText(str); stringq=string; if (i==1) { // a = a-Double.parseDouble(stringq); if (a==0) { a =Double.parseDouble(stringq)-a; } else { Log.e("TAG","jianss_a="+a); a =a-Double.parseDouble(stringq); } } else if (i==2) { if (a==0) { a=a+1; } a = Double.parseDouble(stringq)*a; } else if(i==3) { if (a==0) { a=a+1; a = Double.parseDouble(stringq)/a; } else { a =a/Double.parseDouble(stringq); } } else if (i==3) { if (a==0) { a=a+1; a = Double.parseDouble(stringq)/a; } else { a =a/Double.parseDouble(stringq); } } else { a = Double.parseDouble(stringq)+a; } string=""; i = jia();//返回一个0 } }); bt_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button bt = (Button) v; str1 = str;//str1放前一个数据 // string = str;//存放加号以前的数据 str = str + bt.getText().toString(); strings[g]=str; Log.e("TAG","strings["+g+"]="+strings[g]); g++; input.setText(str); stringq=string; if (a==0) { a =Double.parseDouble(stringq)-a; } else if (i==0) { Log.e("TAG","jian1_a="+a); a =a+Double.parseDouble(stringq); } else if (i==2) { if (a==0) { a=a+1; } a = Double.parseDouble(stringq)*a; } else if(i==3) { if (a==0) { a=a+1; Log.e("TAG","a==="+a); a = Double.parseDouble(stringq)/a; Log.e("TAG","a===1"+a); } else { Log.e("TAG","a=="+a); a =a/Double.parseDouble(stringq); Log.e("TAG","a==1"+a); } } else if (i==3) { if (a==0) { a=a+1; a = Double.parseDouble(stringq)/a; } else { a =a/Double.parseDouble(stringq); } } else { Log.e("TAG","jian_a="+a); a =a-Double.parseDouble(stringq); } Log.e("TAG", "jian_a1=" + a); i = jian();//返回一个0 string=""; } }); bt_cheng.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button bt = (Button) v; str1 = str;//str1放前一个数据 // string = str;//存放加号以前的数据 str = str + bt.getText().toString(); strings[g]=str; Log.e("TAG","stringscheng["+g+"]="+strings[g]); g++; input.setText(str); stringq=string; Log.e("TAG","stringscheng1["+g+"]="+string); if (a==0) { a=a+1; a=a*Double.parseDouble(stringq); } else if (i==0) { //Log.e("TAG","jianqwe_a="+a); a =a+Double.parseDouble(stringq); //Log.e("TAG","jianqweqq_a="+a); } else if (i==1) { if (a==0) { a =Double.parseDouble(stringq)-a; } else { Log.e("TAG","jian_a="+a); a =a-Double.parseDouble(stringq); } } else if(i==3) { if (a==0) { a=a+1; a = Double.parseDouble(stringq)/a; } else { a =a/Double.parseDouble(stringq); //Log.e("TAG","stringDEL2="+string); } } else { a = Double.parseDouble(stringq)*a; } string=""; i = cheng(); } }); bt_chu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button bt = (Button) v; str1 = str;//str1放前一个数据 // string = str;//存放加号以前的数据 str = str + bt.getText().toString(); strings[g]=str; Log.e("TAG","strings["+g+"]="+strings[g]); g++; input.setText(str); stringq=string; if (a==0) { a=a+1; a = Double.parseDouble(stringq)/a; } else if (i==0) { Log.e("TAG","jian1_a="+a); a =a+Double.parseDouble(stringq); } else if (i==2) { if (a==0) { a=a+1; } a = Double.parseDouble(stringq)*a; } else if (i==1) { if (a==0) { a =Double.parseDouble(stringq)-a; } else { Log.e("TAG","jian_a="+a); a =a-Double.parseDouble(stringq); } } else { a =a/Double.parseDouble(stringq); } // string=""; // string1 = "";//存放加号以后的数据 // //23+23+45= string=""; i = chu();//返回一个0 } }); bt_deng.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Button bt = (Button) v; str1 = str;//str1放前一个数据 str = str + bt.getText().toString(); strings[g]=str; Log.e("TAG","strings["+g+"]="+strings[g]); g++; input.setText(str); deng(i); str = "" + c;//使下一次输入时显示页面为c } }); bt_DEL.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("TAG", "stringsDEL[" + g + "]=" + strings[g - 2]); input.setText(strings[g - 2]); str=strings[g-2]; g--; //string=str2; string=strings1[k-1]; Log.e("TAG","stringDEL="+string); k--; } }); } class Bt_OnClickListener implements View.OnClickListener { @Override public void onClick(View v) { Button bt = (Button) v; str1 = str;//str1放前一个数据 str = str + bt.getText().toString(); strings[g]=str; Log.e("TAG","strings["+g+"]="+strings[g]); g++; input.setText(str); // if (DEL_OnClick(v)==8) // { // string=str2; // } str2=string; strings1[k]=str2; Log.e("TAG", "strings1111[" + k + "]=" + strings1[k]); k++; string = string + bt.getText().toString(); //str2=bt.getText().toString(); output.setText(""); } } public int jia() { return 0; } public int jian() { return 1; } public int cheng() { return 2; } public int chu() { return 3; } //结果 public void deng(int i) { if (i == 0) {//执行加法 stringh=string; b = Double.parseDouble(stringh); c = a+b; } if (i == 1) { stringh=string; b = Double.parseDouble(stringh); c = a-b; } if (i == 2) { stringh=string; b = Double.parseDouble(stringh); c = a*b; } if (i == 3) { stringh=string; b = Double.parseDouble(stringh); c = (double)a/b; } output.setText("" + c); } }
以上是关于计算器的主要内容,如果未能解决你的问题,请参考以下文章
Vue3官网-高级指南(十七)响应式计算`computed`和侦听`watchEffect`(onTrackonTriggeronInvalidate副作用的刷新时机`watch` pre)(代码片段