Android開發(fā)之計算器GridLayout布局實現(xiàn)方法示例
本文實例講述了Android開發(fā)之計算器GridLayout布局實現(xiàn)方法。分享給大家供大家參考,具體如下:
運行效果:

Demo 下載地址:https://github.com/LonglyWolf/Calculator
或者點擊此處本站下載。
按鈕布局實現(xiàn):
一個Linearlayout 嵌套三個TextView 最下方的顯示當(dāng)前計算式。上面為先前的計算式。
Gridview 網(wǎng)格布局排布按鈕
<?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>
算法實現(xiàn):
在這里 我先將輸入的 中綴表達(dá)式,轉(zhuǎn)為后綴表達(dá)式,再用后綴表達(dá)式進(jìn)行了計算。
具體實現(xiàn)參照前面一篇:http://www.dhdzp.com/article/158331.htm
這里給大家提供另一種更簡單的思路:
如果不要求算法,Java中已經(jīng)自定義了:ScriptEngineManager類,我們可以直接調(diào)用它的方法,求得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,"請正確輸入",Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
關(guān)于括號自動匹配:
設(shè)一個Flag,判斷前一個字符是什么,空或者運算符就輸出“(”,然后falg++
否則輸出“)” falg-- 最后輸入完成,計算前直接檢查一下falg是否為0即可:

最后講下原式的取回:
很多人計算的時候,會輸入錯誤,這是需要取回計算式
實現(xiàn)很簡單,一個點擊事件的事
比如說點完最頂上的TextView ,就把你當(dāng)前的TextView.setText()就搞定了

具體算法實現(xiàn)可以參考我開頭給出的 Demo
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android 側(cè)邊滑動關(guān)閉Activity的示例代碼
這篇文章主要介紹了Android 側(cè)邊滑動關(guān)閉Activity的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
flutter InheritedWidget使用方法總結(jié)
這篇文章主要為大家介紹了flutter InheritedWidget使用方法總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android EditText實現(xiàn)關(guān)鍵詞批量搜索示例
本篇文章主要介紹了Android EditText實現(xiàn)關(guān)鍵詞批量搜索示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android recycleView的應(yīng)用和點擊事件實例詳解
這篇文章主要介紹了Android recycleView的應(yīng)用和點擊事件實例詳解的相關(guān)資料,需要的朋友可以參考下2016-12-12
Android 中ScrollView與ListView沖突問題的解決辦法
這篇文章主要介紹了Android 中ScrollView與ListView沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握解決問題的辦法,需要的朋友可以參考下2017-10-10

