android簡(jiǎn)易計(jì)算器的制作
之前有好好完成老師留過的C++大作業(yè),使用MFC制作通訊錄。所以用AS寫一個(gè)安卓的計(jì)算器并不是很難,但還是想上手操作一下,寫一個(gè)只有簡(jiǎn)單加減乘除運(yùn)算的小計(jì)算器,后面可能會(huì)考慮加一些其他的稍微復(fù)雜的計(jì)算功能。下面是步驟。
1.首先創(chuàng)建一個(gè)empty activity,取名為MyStudyCalculator。
2.打開activity_main.xml文件,創(chuàng)建兩個(gè)編輯框(EditText)、四個(gè)按鈕(Button)、一個(gè)文本框(TextView),并設(shè)置相應(yīng)的id。其中編輯框作用是讓用戶填入兩個(gè)數(shù)字,四個(gè)按鈕分別對(duì)應(yīng)四種不同的運(yùn)算(需要對(duì)按鈕分別添加響應(yīng)事件),文本框用于顯示運(yùn)算結(jié)果。我另外添加了兩個(gè)文本框,一個(gè)用于顯示標(biāo)題,一個(gè)顯示作者,對(duì)于該計(jì)算器來說沒有任何作用。下面給出代碼:
//第一個(gè)數(shù)字 <EditText android:id="@+id/first" android:layout_width="85dp" android:layout_height="wrap_content" android:layout_marginEnd="56dp" android:layout_marginStart="8dp" android:layout_marginTop="168dp" android:hint="@string/num1" app:layout_constraintEnd_toStartOf="@+id/second" app:layout_constraintHorizontal_bias="0.621" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> //第二個(gè)數(shù)字 <EditText android:id="@+id/second" android:layout_width="85dp" android:layout_height="wrap_content" android:layout_marginEnd="64dp" android:layout_marginTop="168dp" android:hint="@string/num2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> //第二個(gè)數(shù)字 <TextView android:id="@+id/res" android:layout_width="96dp" android:layout_height="33dp" android:layout_marginBottom="84dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:gravity="center" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.481" app:layout_constraintStart_toStartOf="parent" /> //加法按鈕 <Button android:id="@+id/button1" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="260dp" android:onClick="SUM" android:text="+" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.391" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> //減法按鈕 <Button android:id="@+id/button2" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginBottom="30dp" android:layout_marginEnd="148dp" android:layout_marginTop="8dp" android:onClick="SUB" android:text="-" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.595" /> //乘法按鈕 <Button android:id="@+id/button3" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginBottom="152dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:onClick="MUL" android:text="*" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.393" app:layout_constraintStart_toStartOf="parent" /> //除法按鈕 <Button android:id="@+id/button4" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginBottom="8dp" android:layout_marginEnd="148dp" android:layout_marginTop="8dp" android:onClick="DIV" android:text="/" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.678" /> //標(biāo)題 <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="36dp" android:text="丑陋的而且只能算加減乘除的計(jì)算機(jī)" android:textSize="20dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> //作者 <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="11dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="496dp" android:text="TimberWolf" android:textSize="10dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.99" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
3.打開MainActivity.java寫四個(gè)按鈕對(duì)應(yīng)的方法,代碼如下:
//加法 public void SUM(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 + num2; res.setText(String.valueOf(ans)); } //減法 public void SUB(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 - num2; res.setText(String.valueOf(ans)); } //乘法 public void MUL(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 * num2; res.setText(String.valueOf(ans)); } //除法 public void DIV(View view) { EditText first = (EditText)findViewById(R.id.first); EditText second = (EditText)findViewById(R.id.second); TextView res = (TextView)findViewById(R.id.res); double num1 = 0; double num2 = 0; double ans = 0; String numfirst = first.getText().toString(); String numsecond = second.getText().toString(); num1 = Double.parseDouble(numfirst); num2 = Double.parseDouble(numsecond); ans = num1 / num2; res.setText(String.valueOf(ans)); }
4.看似代碼很長(zhǎng),其實(shí)都是一樣的,很簡(jiǎn)單就完成了,其中MainActivity.java中的代碼我沒有給出注釋,就是幾種類型的轉(zhuǎn)換。歡迎大佬指點(diǎn),初學(xué)者不懂的可以給我留言。下面給出仿真機(jī)運(yùn)行效果。
更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)
- Android入門計(jì)算器編寫代碼
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
- Android開發(fā)學(xué)習(xí)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- 從零開始學(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ì)算器編寫代碼
- android計(jì)時(shí)器,時(shí)間計(jì)算器的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)計(jì)算器(計(jì)算表達(dá)式/計(jì)算小數(shù)點(diǎn)以及括號(hào))
相關(guān)文章
Android 編程下字庫(kù)的使用及注意事項(xiàng)
在安卓操作系統(tǒng)下對(duì)于 TextView 字體的支持非常有限,默認(rèn)情況下TextView的typeface屬性支持三種字體;接下來本文將會(huì)介紹Android 編程下字庫(kù)的使用及注意事項(xiàng),感興趣的朋友可以了解下,希望對(duì)你有所幫助2013-01-01Android判斷后臺(tái)服務(wù)是否開啟的兩種方法實(shí)例詳解
這篇文章主要介紹了Android判斷后臺(tái)服務(wù)是否開啟的方法的相關(guān)資料,這里提供了兩種方法及實(shí)例,需要的朋友可以參考下2017-07-07Android開發(fā)DataBinding基礎(chǔ)使用
這篇文章主要為大家介紹了Android開發(fā)DataBinding基礎(chǔ)使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android Studio 3.5版本JNI生成SO文件詳解
這篇文章主要介紹了Android Studio 3.5版本JNI生成SO文件詳解,想了解JNI的同學(xué),可以參考下2021-04-04Android自定義實(shí)現(xiàn)可回彈的ScollView
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)可回彈的ScollView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android自定義ImageView實(shí)現(xiàn)在圖片上添加圖層效果
這篇文章給大家主要介紹了利用Android自定義ImageView如何實(shí)現(xiàn)在圖片上添加圖層的效果,實(shí)現(xiàn)的效果類似在圖片增加秒殺、搶光等標(biāo)簽圖片,對(duì)大家開發(fā)的時(shí)候具有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11