四则运算简易计算器
最近在学习安卓,老师教我们导入Mozilla Rhino和使用JS engine计算,省去了繁琐的步骤。
来这里记录一下。
首先是导入Mozilla Rhino,现在更新到1.7.12版本
图片:
要点击右上角出现的sync now,成功后会出现以上You can use the project…
现在post代码
MainActivity.java
package com.finalproject.basiccalculator;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import org.mozilla.javascript.Context;import org.mozilla.javascript.Scriptable;public class MainActivity extends AppCompatActivity {//how to evaluate expression//Mozilla Rhino - can help using is in android application//method to clearpublic void clear(){TextView res=findViewById(R.id.result);if(res.getText().equals(\"0\")){res.setText(\"\");}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final TextView result=findViewById(R.id.result);findViewById(R.id.num0).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"0\");}});findViewById(R.id.num1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"1\");}});findViewById(R.id.num2).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"2\");}});findViewById(R.id.num3).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"3\");}});findViewById(R.id.num4).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"4\");}});findViewById(R.id.num5).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"5\");}});findViewById(R.id.num6).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"6\");}});findViewById(R.id.num7).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"7\");}});findViewById(R.id.num8).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"8\");}});findViewById(R.id.num9).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"9\");}});findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"+\");}});findViewById(R.id.minus).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"-\");}});findViewById(R.id.multiply).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"*\");}});findViewById(R.id.divide).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0clear();result.setText(result.getText()+\"/\");}});findViewById(R.id.clear).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//clear the result if there is just 0result.setText(\"0\");}});//do the evaluation when the equal button is clickedfindViewById(R.id.equal).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String expression=result.getText().toString();//evaluate using JS engineContext con= Context.enter();//当前线程上下文创建con.setOptimizationLevel(-1);// Turn off optimization to make Rhino Android compatibleScriptable scope=con.initSafeStandardObjects();// 初始化标准对象function object 等等Object res=con.evaluateString(scope,expression,\"<cmd>\", 1,null);result.setText(res.toString());}});}}
接着是页面布局 activity_main.xml
用了GridLayout去设置按钮,布局更整洁方便。
<?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\"android:layout_weight=\"10\"android:orientation=\"vertical\"tools:context=\".MainActivity\"><RelativeLayoutandroid:layout_width=\"match_parent\"android:layout_height=\"0dp\"android:layout_weight=\"2\"><TextViewandroid:id=\"@+id/result\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:text=\"@string/result_value\"android:textAlignment=\"textEnd\"android:textSize=\"35sp\" /></RelativeLayout><androidx.gridlayout.widget.GridLayoutandroid:layout_width=\"match_parent\"android:layout_height=\"0dp\"android:layout_gravity=\"center|center_vertical\"android:layout_weight=\"8\"android:padding=\"15dp\"app:columnCount=\"4\"app:rowCount=\"4\"><Buttonandroid:id=\"@+id/num1\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num1\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num2\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num2\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num3\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num3\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/add\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/opAdd\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num4\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num4\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num5\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num5\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num6\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num6\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/minus\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/opMinus\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num7\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num7\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num8\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num8\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num9\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num9\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/multiply\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/opMultiply\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/clear\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/opClear\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/num0\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/num0\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/equal\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/opEquals\"android:textSize=\"25dp\" /><Buttonandroid:id=\"@+id/divide\"style=\"@style/Widget.AppCompat.Button.Colored\"android:layout_width=\"85dp\"android:layout_height=\"85dp\"android:layout_margin=\"4dp\"android:text=\"@string/opDivide\"android:textSize=\"25dp\" /></androidx.gridlayout.widget.GridLayout></LinearLayout>
现在上图
第一次记录学习,希望大家不要嫌弃。