Example of GridLayout implementation method for Android calculator

Time:2021-6-4

This paper describes the implementation method of GridLayout for Android calculator. The details are as follows:

Operation effect:

Demo download address: https://github.com/LonglyWolf/Calculator

Or click hereDownload from our website

Button layout implementation:

A LinearLayout nests three textviews, and the bottom part displays the current formula. Above is the previous formula.

GridView grid layout button


<?xml version="1.0" encoding="utf-8"?>
<GridLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:rowCount="7"
  android:columnCount="4"
  android:id="@+id/root"
  android:background="@color/buttonBackgroundBlack"
  android:padding="5dp">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_columnSpan="4">
    <TextView
      android:id="@+id/textview_up"
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:layout_columnSpan="4"
      android:layout_gravity="right"
      android:background="#ff525252"
      android:padding="3pt"
      android:singleLine="true"
      android:gravity="right"
      android:textColor="#66ffffff"
      android:textSize="25sp" />
    <TextView
      android:id="@+id/textview_down"
      android:layout_width="match_parent"
      android:layout_height="60dp"
      android:layout_columnSpan="4"
      android:layout_gravity="right"
      android:background="#ff525252"
      android:padding="3pt"
      android:singleLine="true"
      android:gravity="right"
      android:textColor="#66ffffff"
      android:textSize="25sp" />
    <TextView
      android:id="@+id/textview"
      android:layout_width="match_parent"
      android:layout_height="75dp"
      android:layout_columnSpan="4"
      android:layout_gravity="right"
      android:background="#ff525252"
      android:padding="3pt"
      android:gravity="right"
      android:singleLine="true"
      android:textColor="#eee"
      android:textSize="40sp"
      android:maxLines="10"/>
  </LinearLayout>
</GridLayout>

Algorithm implementation:

Here I first input infix expression, into a suffix expression, and then the suffix expression for calculation.

Refer to the previous article for specific implementation https://www.jb51.net/article/158331.htm

Here’s another simpler way of thinking:

If no algorithm is required, the scriptenginemanager class has been customized in Java. We can directly call its method to get the value of the formula on textview

ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
String expression = tv.getText().toString;
try {
  String result = String.valueOf(scriptEngine.eval(expression));
  System.out.println(result);
} catch (ScriptException e) {
  Toast. Maketext (mainactivity. This, "please input correctly", toast. Length_ SHORT).show();
  e.printStackTrace();
}

About bracket auto matching:

Set a flag to judge what the previous character is. If it is empty or the operator is used, it will output “(“, and then false++

Otherwise, output “)” falg — the final input is completed. Before calculation, check whether falg is 0 directly

Finally, we will talk about the retrieval of the original formula

When many people calculate, they will input errors, which is the need to retrieve the formula

Implementation is very simple, a click event thing

For example, after clicking the top textview, your current textview. Settext () will be finished

The specific algorithm implementation can refer to the demo I gave at the beginning

For more information about Android, readers interested in this site can see the following topics: summary of Android control usage, introduction and advanced course of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of operation skills of Android database and summary of operation skills of Android resources

I hope this article is helpful for Android programming.