The last article talked about the six basic layouts of android: https://blog.csdn.net/qq_40205116/article/details/88418781
This article uses layout to make an android small project (calculator). The effect is as follows (those who wanted to insert video, but found that they couldn't insert video, could only insert pictures. Here% means taking the rest):
Analysis: the main subgrade thinking is the priority of the four operations of addition, subtraction, multiplication and division. It's not difficult to solve this problem. You can convert the equation into a character array first, and then convert it into a character set (number and operator), and then calculate it. When calculating, the subscript of the first number string is 0, and only to judge whether the second string operator is multiplication, division or redundancy.
1. If this is the case, then put the result in the set with subscript 0 and delete the next two elements
2. If not, judge whether the operator with subscript 3 is multiplication, division or redundancy. If yes, you can operate. If no, you can operate on the previous group.
To push, if you don't understand it, then look at the code (expression ability is not very good O(∩) O haha ~)!
Code part:
In activity main.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/TableLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="*" > <!-- Displayed text --> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="150dp" android:background="@android:color/holo_blue_bright" android:textSize="20dp" android:gravity="center|right" android:text="" /> <!-- android:onClick="fun"Set click event android:tag="%"Click to get the text value --> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="C" /> <Button android:id="@+id/btn02" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="←" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="%" android:text="%" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="÷" android:text="÷" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="7" android:text="7" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="8" android:text="8" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="9" android:text="9" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="x" android:text="x" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="4" android:text="4" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="5" android:text="5" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="6" android:text="6" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="-" android:text="-" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="1" android:text="1" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="2" android:text="2" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="3" android:text="3" /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="+" android:text="+" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="." android:text="." /> <Button android:layout_width="wrap_content" android:layout_height="match_parent" android:onClick="fun" android:tag="0" android:text="0" /> <Button android:id="@+id/btn19" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_span="2" android:text="=" /> </TableRow> </TableLayout>
MainActivity.java class:
package com.example.tablelayoutjsq; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView text; private Button btn01; private Button btn02; private Button btn19; String str; //Text value String str1 = ""; //Addition, subtraction, multiplication and division equation private OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { String s = ""; switch (v.getId()) { case R.id.btn01: //empty text.setText(""); str1 = ""; break; case R.id.btn02: //Regression if(text.getText().toString().length() > 0){ s = text.getText().toString().substring(0, text.getText().toString().length()-1); text.setText(s); } break; case R.id.btn19: //Click = sign calculation result str = text.getText().toString(); if(str.equals("")){ return ; } char[] array = str.toCharArray(); List<String> list = new ArrayList<String>(); for(int i = str1.length(); i < array.length; i++){ if(array[i] == '%' || array[i] == '÷' || array[i] == 'x' || array[i] == '-' || array[i] == '+'){ if(!s.equals("")){ list.add(s); s = ""; } String a = array[i]+""; list.add(a); }else{ s += array[i]; if(i == array.length-1){ list.add(s); s = ""; } } } while(true){ String b = list.get(list.size()-1); if(b.equals("%") || b.equals("÷") || b.equals("x") || b.equals("+") || b.equals("-")) list.remove(list.size()-1); if(list.size() == 1){ str1 = text.getText().toString()+"\n"; text.setText(str1+list.get(0)); break; } if(list.size() == 3){ fun(0, list); }else{ if("%".equals(list.get(3)) || "÷".equals(list.get(3)) || "x".equals(list.get(3))){ fun(2, list); }else{ fun(0, list); } } } default: break; } } //Addition, subtraction, multiplication and division private void fun(int i, List<String> list) { double x = Double.parseDouble(list.get(i)); String y = list.get(i+1); double z = Double.parseDouble(list.get(i+2)); String sum; if(y.equals("%")){ sum = x % z+""; }else if(y.equals("÷")){ sum = x / z+""; }else if(y.equals("+")){ sum = x + z+""; }else if(y.equals("-")){ sum = x - z+""; }else{ sum = x * z+""; } list.set(i, sum); list.remove(i+2); list.remove(i+1); } }; //Click the button to display the button value on the text public void fun(View v){ str = text.getText().toString(); if(str.length() > 0){ char c = str.charAt(str.length()-1); String ss = (String) v.getTag(); if((c == '%' || c == '÷' || c == 'x' || c == '+' || c == '-') && (ss.equals("%") || ss.equals("÷") || ss.equals("x") || ss.equals("+") || ss.equals("-"))){ str = str.substring(0, str.length()-1)+ss; text.setText(str); }else{ text.append((String)v.getTag()); } }else{ text.append((String)v.getTag()); //append concatenates a string } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); btn01 = (Button) findViewById(R.id.btn01); btn02 = (Button) findViewById(R.id.btn02); btn19 = (Button) findViewById(R.id.btn19); btn01.setOnClickListener(listener); btn02.setOnClickListener(listener); btn19.setOnClickListener(listener); } }