Android計算器編寫代碼
其實這個安卓計算機,所有的后臺思想與《C#計算器編寫代碼》是一模一樣的。Win窗體程序移植到安卓,從C#到Java其實很簡單的,因為兩者的基本語法都很相像,唯一的難點是安卓的xml布局部分,不像C#窗體能夠直接拖。
還是如下圖一個能夠完成基本四則運算的計算器:

先在res\values\strings.xml設(shè)置按鈕相應(yīng)的字體,以免布局文件警告滿天飛:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">計算器</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)于百分比布局多個LinearLayout嵌套時出現(xiàn)的問題與解決方案》(點擊打開鏈接)的思想,具體如下圖,一個TextView、一個EditText,皆直接用match_parent占據(jù)整行的寬度,之后利用LinearLayout與TableLayout作橫向比例的劃分。

因此,res\layout\activity_main.xml具體代碼如下,之后的操作要操作的組件加上Id,這里加上《【Android】內(nèi)存卡圖片讀取器,圖庫app》(點擊打開鏈接)的ScrollView是防止某些手機屏幕過少,加上垂直滾動條:
<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沒什么好說的,基本與直接Win窗體的《C#計算器編寫代碼》,將C#改成java是一個很簡單的事情。唯一注意的是,這里的按鈕比較多,因此不建議像《【Android】利用Java代碼布局,按鈕添加點擊事件》(點擊打開鏈接)一樣,使用內(nèi)部匿名類實現(xiàn)按鈕的點擊事件,應(yīng)該讓MainActivity實現(xiàn)OnClickListener接口,之后在繼承下來的onClick方法,根據(jù)傳遞過來的View v中的id,利用switch-case結(jié)構(gòu)來搞,這樣清晰明了。
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>();// 存用戶輸入的運算符,定義+為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)// 防止用戶多次輸入一個符號鍵,符號鍵只允許輸入一次
{
result_flag = false;
value_list.add(Double.parseDouble(editText1.getText()
.toString()));// 將當前已輸入的數(shù)字放入value_list
operator_list.add(0);
textView1.setText(textView1.getText() + "+");
add_flag = true;
can_operate_flag = false;// 剛剛輸入完符號,不能構(gòu)成一條正常的表達式,如111+,設(shè)置為不可運行狀態(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)輸入的東西加個括號。(運算符棧問題是一個很復(fù)雜的數(shù)據(jù)結(jié)構(gòu)問題,這里不做,: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) {// 需要防止用戶沒輸入數(shù)字,或者只輸入了一個數(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#的運算符重載的關(guān)鍵字,前面加個_來區(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ù)字與運算數(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這類情況這里允許,因為java可以講000001223自己轉(zhuǎn)化為1223
private void num_down(String num) {
if (add_flag || minus_flag || multi_flag || div_flag || result_flag) {
if (result_flag)// 按下等號,剛剛算完一個運算的狀態(tài)
{
textView1.setText("");
}
editText1.setText("");// 如果用戶剛剛輸入完一個運算符
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ù)點.,則要判斷當前已輸入的數(shù)字中是否含有小數(shù)點.才允許輸入
editText1.setText(editText1.getText() + num);
textView1.setText(textView1.getText() + num);
can_operate_flag = true;
}
}
}
關(guān)于計算器的精彩文章請查看《計算器專題》 ,更多精彩等你來發(fā)現(xiàn)!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android應(yīng)用開發(fā)的版本更新檢測升級功能實現(xiàn)示例
本文對Android版本更新的知識做全面的總結(jié),主要包括開發(fā)中版本的設(shè)置,如何檢測本程序的版本,版本的更新判斷和顯示,新版本程序的安裝2022-04-04
Android AutoCompleteTextView連接數(shù)據(jù)庫自動提示的方法(附demo源碼下載)
這篇文章主要介紹了Android AutoCompleteTextView連接數(shù)據(jù)庫自動提示的方法,結(jié)合實例形式分析了AutoCompleteTextView操作數(shù)據(jù)庫的原理與具體技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02
Android編程之canvas繪制各種圖形(點,直線,弧,圓,橢圓,文字,矩形,多邊形,曲線,圓角矩形)
這篇文章主要介紹了Android編程之canvas繪制各種圖形的方法,涉及Android使用Canvas類中常用繪圖方法的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12
android自動生成dimens適配文件的圖文教程詳解(無需Java工具類)
這篇文章主要介紹了android自動生成dimens適配文件,無需Java工具類,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
android利用ContentResolver訪問者獲取手機聯(lián)系人信息
這篇文章主要介紹了android利用ContentResolver訪問者獲取手機聯(lián)系人信息,非常具有實用價值,需要的朋友可以參考下。2017-02-02

