Android開(kāi)發(fā)之計(jì)算器GridLayout布局實(shí)現(xiàn)方法示例
本文實(shí)例講述了Android開(kāi)發(fā)之計(jì)算器GridLayout布局實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
運(yùn)行效果:
Demo 下載地址:https://github.com/LonglyWolf/Calculator
或者點(diǎn)擊此處本站下載。
按鈕布局實(shí)現(xiàn):
一個(gè)Linearlayout 嵌套三個(gè)TextView 最下方的顯示當(dāng)前計(jì)算式。上面為先前的計(jì)算式。
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>
算法實(shí)現(xiàn):
在這里 我先將輸入的 中綴表達(dá)式,轉(zhuǎn)為后綴表達(dá)式,再用后綴表達(dá)式進(jìn)行了計(jì)算。
具體實(shí)現(xiàn)參照前面一篇:http://www.dbjr.com.cn/article/158331.htm
這里給大家提供另一種更簡(jiǎn)單的思路:
如果不要求算法,Java中已經(jīng)自定義了:ScriptEngineManager類,我們可以直接調(diào)用它的方法,求得TextView上計(jì)算式的值
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,"請(qǐng)正確輸入",Toast.LENGTH_SHORT).show(); e.printStackTrace(); }
關(guān)于括號(hào)自動(dòng)匹配:
設(shè)一個(gè)Flag,判斷前一個(gè)字符是什么,空或者運(yùn)算符就輸出“(”,然后falg++
否則輸出“)” falg-- 最后輸入完成,計(jì)算前直接檢查一下falg是否為0即可:
最后講下原式的取回:
很多人計(jì)算的時(shí)候,會(huì)輸入錯(cuò)誤,這是需要取回計(jì)算式
實(shí)現(xiàn)很簡(jiǎn)單,一個(gè)點(diǎn)擊事件的事
比如說(shuō)點(diǎn)完最頂上的TextView ,就把你當(dāng)前的TextView.setText()就搞定了
具體算法實(shí)現(xiàn)可以參考我開(kāi)頭給出的 Demo
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android開(kāi)發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
這篇文章主要介紹了Android開(kāi)發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils,涉及Android基于pinyin4j-2.5.0.jar包文件實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11android中關(guān)于call撥號(hào)功能的實(shí)現(xiàn)方法
這篇文章主要介紹了android中關(guān)于call撥號(hào)功能實(shí)現(xiàn)的記錄,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05android studio 3.0 gradle 打包腳本配置詳解
這篇文章主要介紹了android studio 3.0 gradle 打包腳本配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10淺談Android中關(guān)于靜態(tài)變量(static)的使用問(wèn)題
本文主要介紹了Android中關(guān)于靜態(tài)變量(static)的使用問(wèn)題,具有一定的參考作用,下面跟著小編一起來(lái)看下吧2017-01-01Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android編程心得分享——JSON學(xué)習(xí)過(guò)程
在我們初步學(xué)習(xí)JSON時(shí)我們都知道JSON作為現(xiàn)在比較流行的數(shù)據(jù)交換格式,有著它的許多優(yōu)點(diǎn),這里將我學(xué)習(xí)JSON的過(guò)程記錄如下2013-06-06Android開(kāi)發(fā)中總結(jié)的Adapter工具類【附完整源碼下載】
這篇文章主要介紹了Android開(kāi)發(fā)中總結(jié)的Adapter工具類,簡(jiǎn)單說(shuō)明了Adapter的功能,并結(jié)合實(shí)例形式分析了Adapter工具類的相關(guān)使用方法,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11Android優(yōu)雅的方式解決軟鍵盤遮擋按鈕問(wèn)題
這篇文章主要介紹了Android優(yōu)雅的方式解決軟鍵盤遮擋按鈕問(wèn)題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01