Android開發(fā)之計算器GridLayout布局實現(xiàn)方法示例
本文實例講述了Android開發(fā)之計算器GridLayout布局實現(xiàn)方法。分享給大家供大家參考,具體如下:
運行效果:
Demo 下載地址:https://github.com/LonglyWolf/Calculator
或者點擊此處本站下載。
按鈕布局實現(xiàn):
一個Linearlayout 嵌套三個TextView 最下方的顯示當前計算式。上面為先前的計算式。
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):
在這里 我先將輸入的 中綴表達式,轉(zhuǎn)為后綴表達式,再用后綴表達式進行了計算。
具體實現(xiàn)參照前面一篇:http://www.dbjr.com.cn/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 ,就把你當前的TextView.setText()就搞定了
具體算法實現(xiàn)可以參考我開頭給出的 Demo
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
這篇文章主要介紹了Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils,涉及Android基于pinyin4j-2.5.0.jar包文件實現(xiàn)漢字轉(zhuǎn)拼音功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11android中關(guān)于call撥號功能的實現(xiàn)方法
這篇文章主要介紹了android中關(guān)于call撥號功能實現(xiàn)的記錄,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05android studio 3.0 gradle 打包腳本配置詳解
這篇文章主要介紹了android studio 3.0 gradle 打包腳本配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10淺談Android中關(guān)于靜態(tài)變量(static)的使用問題
本文主要介紹了Android中關(guān)于靜態(tài)變量(static)的使用問題,具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01Android自定義ViewGroup實現(xiàn)標簽浮動效果
這篇文章主要為大家詳細介紹了Android自定義ViewGroup實現(xiàn)標簽浮動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06Android開發(fā)中總結(jié)的Adapter工具類【附完整源碼下載】
這篇文章主要介紹了Android開發(fā)中總結(jié)的Adapter工具類,簡單說明了Adapter的功能,并結(jié)合實例形式分析了Adapter工具類的相關(guān)使用方法,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11