欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)之計算器GridLayout布局實現(xiàn)方法示例

 更新時間:2019年03月23日 11:24:30   作者:水中魚之1999  
這篇文章主要介紹了Android開發(fā)之計算器GridLayout布局實現(xiàn)方法,結(jié)合實例形式分析了Android計算器界面布局及表達式計算相關(guā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)文章

最新評論