Android計(jì)算器編寫代碼
其實(shí)這個(gè)安卓計(jì)算機(jī),所有的后臺(tái)思想與《C#計(jì)算器編寫代碼》是一模一樣的。Win窗體程序移植到安卓,從C#到Java其實(shí)很簡(jiǎn)單的,因?yàn)閮烧叩幕菊Z(yǔ)法都很相像,唯一的難點(diǎn)是安卓的xml布局部分,不像C#窗體能夠直接拖。
還是如下圖一個(gè)能夠完成基本四則運(yùn)算的計(jì)算器:
先在res\values\strings.xml設(shè)置按鈕相應(yīng)的字體,以免布局文件警告滿天飛:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">計(jì)算器</string> <string name="bt_1">1</string> <string name="bt_2">2</string> <string name="bt_3">3</string> <string name="bt_4">4</string> <string name="bt_5">5</string> <string name="bt_6">6</string> <string name="bt_7">7</string> <string name="bt_8">8</string> <string name="bt_9">9</string> <string name="bt_0">0</string> <string name="bt_point">.</string> <string name="bt_ce">CE</string> <string name="bt_plus">+</string> <string name="bt_minus">-</string> <string name="bt_multi">×</string> <string name="bt_div">÷</string> <string name="bt_result">=</string> </resources>
之后,布局部分采用了《【Android】關(guān)于百分比布局多個(gè)LinearLayout嵌套時(shí)出現(xiàn)的問(wèn)題與解決方案》(點(diǎn)擊打開鏈接)的思想,具體如下圖,一個(gè)TextView、一個(gè)EditText,皆直接用match_parent占據(jù)整行的寬度,之后利用LinearLayout與TableLayout作橫向比例的劃分。
因此,res\layout\activity_main.xml具體代碼如下,之后的操作要操作的組件加上Id,這里加上《【Android】?jī)?nèi)存卡圖片讀取器,圖庫(kù)app》(點(diǎn)擊打開鏈接)的ScrollView是防止某些手機(jī)屏幕過(guò)少,加上垂直滾動(dòng)條:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:enabled="false" android:inputType="none" android:textSize="18sp" /> <LinearLayout android:baselineAligned="false" android:layout_width="match_parent" android:layout_height="match_parent" > <TableLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_7" /> <Button android:id="@+id/bt_8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_8" /> <Button android:id="@+id/bt_9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_9" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_4" /> <Button android:id="@+id/bt_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_5" /> <Button android:id="@+id/bt_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_1" /> <Button android:id="@+id/bt_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_2" /> <Button android:id="@+id/bt_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_0" /> <Button android:id="@+id/bt_point" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_point" /> </LinearLayout> </TableLayout> <TableLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <Button android:id="@+id/bt_ce" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/bt_ce" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_plus" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_plus" /> <Button android:id="@+id/bt_minus" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_minus" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/bt_multi" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_multi" /> <Button android:id="@+id/bt_div" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/bt_div" /> </LinearLayout> <Button android:id="@+id/bt_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/bt_result" /> </TableLayout> </LinearLayout> </LinearLayout> </ScrollView>
之后是MainActivity.java沒(méi)什么好說(shuō)的,基本與直接Win窗體的《C#計(jì)算器編寫代碼》,將C#改成java是一個(gè)很簡(jiǎn)單的事情。唯一注意的是,這里的按鈕比較多,因此不建議像《【Android】利用Java代碼布局,按鈕添加點(diǎn)擊事件》(點(diǎn)擊打開鏈接)一樣,使用內(nèi)部匿名類實(shí)現(xiàn)按鈕的點(diǎn)擊事件,應(yīng)該讓MainActivity實(shí)現(xiàn)OnClickListener接口,之后在繼承下來(lái)的onClick方法,根據(jù)傳遞過(guò)來(lái)的View v中的id,利用switch-case結(jié)構(gòu)來(lái)搞,這樣清晰明了。
package com.calculator; import java.util.*; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.TextView; import android.app.Activity; public class MainActivity extends Activity implements OnClickListener { private List<Double> value_list = new ArrayList<Double>();// 存用戶輸入的數(shù)字 private List<Integer> operator_list = new ArrayList<Integer>();// 存用戶輸入的運(yùn)算符,定義+為0,-為1,×為2,÷為3 // 狀態(tài)記錄 private boolean add_flag = false;// +按下 private boolean minus_flag = false;// -按下 private boolean multi_flag = false;// ×按下 private boolean div_flag = false;// ÷按下 private boolean result_flag = false;// =按下 private boolean can_operate_flag = false;// 按下=是否響應(yīng) private TextView textView1; private EditText editText1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt_0).setOnClickListener(this); findViewById(R.id.bt_1).setOnClickListener(this); findViewById(R.id.bt_2).setOnClickListener(this); findViewById(R.id.bt_3).setOnClickListener(this); findViewById(R.id.bt_4).setOnClickListener(this); findViewById(R.id.bt_5).setOnClickListener(this); findViewById(R.id.bt_6).setOnClickListener(this); findViewById(R.id.bt_7).setOnClickListener(this); findViewById(R.id.bt_8).setOnClickListener(this); findViewById(R.id.bt_9).setOnClickListener(this); findViewById(R.id.bt_point).setOnClickListener(this); findViewById(R.id.bt_ce).setOnClickListener(this); findViewById(R.id.bt_plus).setOnClickListener(this); findViewById(R.id.bt_minus).setOnClickListener(this); findViewById(R.id.bt_multi).setOnClickListener(this); findViewById(R.id.bt_div).setOnClickListener(this); findViewById(R.id.bt_result).setOnClickListener(this); textView1 = (TextView) findViewById(R.id.textView1); editText1 = (EditText) findViewById(R.id.editText1); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_0: num_down("0"); break; case R.id.bt_1: num_down("1"); break; case R.id.bt_2: num_down("2"); break; case R.id.bt_3: num_down("3"); break; case R.id.bt_4: num_down("4"); break; case R.id.bt_5: num_down("5"); break; case R.id.bt_6: num_down("6"); break; case R.id.bt_7: num_down("7"); break; case R.id.bt_8: num_down("8"); break; case R.id.bt_9: num_down("9"); break; case R.id.bt_point: num_down("."); break; case R.id.bt_plus: if (!add_flag)// 防止用戶多次輸入一個(gè)符號(hào)鍵,符號(hào)鍵只允許輸入一次 { result_flag = false; value_list.add(Double.parseDouble(editText1.getText() .toString()));// 將當(dāng)前已輸入的數(shù)字放入value_list operator_list.add(0); textView1.setText(textView1.getText() + "+"); add_flag = true; can_operate_flag = false;// 剛剛輸入完符號(hào),不能構(gòu)成一條正常的表達(dá)式,如111+,設(shè)置為不可運(yùn)行狀態(tài) } break; case R.id.bt_minus: if (!minus_flag) { result_flag = false; value_list.add(Double.parseDouble(editText1.getText() .toString())); operator_list.add(1); textView1.setText(textView1.getText() + "-"); minus_flag = true; can_operate_flag = false; } break; case R.id.bt_multi: if (!multi_flag) { result_flag = false; value_list.add(Double.parseDouble(editText1.getText() .toString())); operator_list.add(2); textView1.setText("(" + textView1.getText() + ")×");// 給前面的已經(jīng)輸入的東西加個(gè)括號(hào)。(運(yùn)算符棧問(wèn)題是一個(gè)很復(fù)雜的數(shù)據(jù)結(jié)構(gòu)問(wèn)題,這里不做,:P) multi_flag = true; can_operate_flag = false; } break; case R.id.bt_div: if (!div_flag) { result_flag = false; value_list.add(Double.parseDouble(editText1.getText() .toString())); operator_list.add(3); textView1.setText("(" + textView1.getText() + ")÷"); div_flag = true; can_operate_flag = false; } break; case R.id.bt_result: if (value_list.size() > 0 && operator_list.size() > 0 && can_operate_flag) {// 需要防止用戶沒(méi)輸入數(shù)字,或者只輸入了一個(gè)數(shù),就按=。 value_list.add(Double.parseDouble(editText1.getText() .toString())); double total = value_list.get(0); for (int i = 0; i < operator_list.size(); i++) { int _operator = operator_list.get(i);// operator是C#的運(yùn)算符重載的關(guān)鍵字,前面加個(gè)_來(lái)區(qū)別 switch (_operator) { case 0: total += value_list.get(i + 1); break; case 1: total -= value_list.get(i + 1); break; case 2: total *= value_list.get(i + 1); break; case 3: total /= value_list.get(i + 1); break; } } editText1.setText(total + ""); textView1.setText(total + ""); operator_list.clear();// 算完,就清空累積數(shù)字與運(yùn)算數(shù)組 value_list.clear(); result_flag = true;// 表示=按下 } break; case R.id.bt_ce: operator_list.clear(); value_list.clear(); add_flag = false; minus_flag = false; multi_flag = false; div_flag = false; result_flag = false; can_operate_flag = false; editText1.setText(""); textView1.setText(""); break; } } // 數(shù)字鍵按下,含0與.,類似000001223這類情況這里允許,因?yàn)閖ava可以講000001223自己轉(zhuǎn)化為1223 private void num_down(String num) { if (add_flag || minus_flag || multi_flag || div_flag || result_flag) { if (result_flag)// 按下等號(hào),剛剛算完一個(gè)運(yùn)算的狀態(tài) { textView1.setText(""); } editText1.setText("");// 如果用戶剛剛輸入完一個(gè)運(yùn)算符 add_flag = false; minus_flag = false; multi_flag = false; div_flag = false; result_flag = false; } if ((num.equals(".") && editText1.getText().toString().indexOf(".") < 0) || !num.equals(".")) { // 如果用戶輸入的是小數(shù)點(diǎn).,則要判斷當(dāng)前已輸入的數(shù)字中是否含有小數(shù)點(diǎn).才允許輸入 editText1.setText(editText1.getText() + num); textView1.setText(textView1.getText() + num); can_operate_flag = true; } } }
關(guān)于計(jì)算器的精彩文章請(qǐng)查看《計(jì)算器專題》 ,更多精彩等你來(lái)發(fā)現(xiàn)!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 從零開始學(xué)android實(shí)現(xiàn)計(jì)算器功能示例分享(計(jì)算器源碼)
- Android開發(fā)實(shí)現(xiàn)的簡(jiǎn)單計(jì)算器功能【附完整demo源碼下載】
- android計(jì)算器簡(jiǎn)單實(shí)現(xiàn)代碼
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小程序
- android studio實(shí)現(xiàn)計(jì)算器
- Android中使用GridLayout網(wǎng)格布局來(lái)制作簡(jiǎn)單的計(jì)算器App
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能
相關(guān)文章
Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法
這篇文章主要介紹了Android中ImageView使用網(wǎng)絡(luò)圖片資源的方法,較為詳細(xì)的分析了ImageView調(diào)用網(wǎng)絡(luò)圖片的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android ViewPager實(shí)現(xiàn)無(wú)限循環(huán)輪播廣告位Banner效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)無(wú)限循環(huán)輪播廣告位Banner效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android自定義StickinessView粘性滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義StickinessView粘性滑動(dòng)效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android自定義view系列之99.99%實(shí)現(xiàn)QQ側(cè)滑刪除效果實(shí)例代碼詳解
這篇文章給大家介紹android自定義view系列之99.99%實(shí)現(xiàn)QQ側(cè)滑刪除效果,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-09-09淺談Android textview文字對(duì)齊換行的問(wèn)題
下面小編就為大家分享一篇淺談Android textview文字對(duì)齊換行的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01