Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器APP
一、簡(jiǎn)介:用Android Studio 實(shí)現(xiàn)一個(gè)簡(jiǎn)單的計(jì)算器APP,并在藍(lán)疊模擬器中運(yùn)行。
該計(jì)算器只能實(shí)現(xiàn)兩位數(shù)字的四則運(yùn)算。
二、代碼
activity_main.xml ---界面設(shè)計(jì)
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/textView" android:layout_alignParentStart="true" android:rowCount="6" android:columnCount="4" > <!--設(shè)置網(wǎng)格為6行4列--> <!--顯示文本組件,占1行4列--> <TextView android:id="@+id/text" android:layout_width="350dp" android:layout_height="wrap_content" android:layout_columnSpan="4" android:layout_marginLeft="4px" android:gravity="left" android:textSize="50dp" /> <!--清除按鈕,占1行4列--> <Button android:id="@+id/btnClear" android:layout_width="353dp" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除" android:textSize="26sp" /> <!--以下按鈕為數(shù)字按鈕和函數(shù)按鈕,每個(gè)占1行1列--> <Button android:id="@+id/btn1" android:text="1" android:textSize="26sp" /> <Button android:id="@+id/btn2" android:text="2" android:textSize="26sp" /> <Button android:id="@+id/btn3" android:text="3" android:textSize="26sp" /> <Button android:id="@+id/btnPlus" android:text="+" android:textSize="26sp" /> <Button android:id="@+id/btn4" android:text="4" android:textSize="26sp" /> <Button android:id="@+id/btn5" android:text="5" android:textSize="26sp" /> <Button android:id="@+id/btn6" android:text="6" android:textSize="26sp" /> <Button android:id="@+id/btnSubtract" android:text="-" android:textSize="26sp" /> <Button android:id="@+id/btn7" android:text="7" android:textSize="26sp" /> <Button android:id="@+id/btn8" android:text="8" android:textSize="26sp" /> <Button android:id="@+id/btn9" android:text="9" android:textSize="26sp" /> <Button android:id="@+id/btnMultiply" android:text="*" android:textSize="26sp" /> <Button android:id="@+id/btnPoint" android:text="." android:textSize="26sp" /> <Button android:id="@+id/btn0" android:text="0" android:textSize="26sp" /> <Button android:id="@+id/btnSum" android:text="=" android:textSize="26sp" /> <Button android:id="@+id/btnDivide" android:text="/" android:textSize="26sp" /> </GridLayout>
界面:

MainActivity.java---功能實(shí)現(xiàn)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
btnClear,btnPlus,btnSubtract,btnMultiply,btnDivide,btnSum,btnPoint;
TextView text;
String str = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnClear = (Button) findViewById(R.id.btnClear);
btnPlus = (Button) findViewById(R.id.btnPlus);
btnSubtract = (Button) findViewById(R.id.btnSubtract);
btnMultiply = (Button) findViewById(R.id.btnMultiply);
btnDivide = (Button) findViewById(R.id.btnDivide);
btnPoint = (Button) findViewById(R.id.btnPoint);
btnSum = (Button) findViewById(R.id.btnSum);
text = (TextView) findViewById(R.id.text) ;
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnPlus.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnPoint.setOnClickListener(this);
btnSum.setOnClickListener(new click());
//給所有按鈕注冊(cè)點(diǎn)擊事件
}
@Override
public void onClick(View v) {
String input=text.getText().toString();
switch (v.getId()){
case R.id.btn0:
case R.id.btn1:
case R.id.btn2:
case R.id.btn3:
case R.id.btn4:
case R.id.btn5:
case R.id.btn6:
case R.id.btn7:
case R.id.btn8:
case R.id.btn9:
case R.id.btnPoint:
text.setText(input+((Button)v).getText());
break;
case R.id.btnPlus:
case R.id.btnSubtract:
case R.id.btnMultiply:
case R.id.btnDivide:
text.setText(input + " " + ((Button)v).getText() + " "); //給運(yùn)算符前后加空格,好判斷
break;
case R.id.btnClear:
text.setText("");
break;
}
}
class click implements View.OnClickListener {
public void onClick (View v) {
getResult();
}
}
private void getResult () {
String str1 = text.getText().toString();
if(str1 == null || str1.equals("")){
return;
}
if(!str1.contains(" ")){
return ;
}
double result = 0;
// 第一個(gè)數(shù)的字符串
String s1 = str1.substring(0,str1.indexOf(" "));
// 運(yùn)算符
String op = str1.substring(str1.indexOf(" ")+1,str1.indexOf(" ")+2);
// 第二個(gè)數(shù)的字符串
String s2 = str1.substring(str1.indexOf(" ")+3);
double d1 = Double.parseDouble(s1);//將數(shù)字字符串轉(zhuǎn)為double類(lèi)型
double d2 = Double.parseDouble(s2);
if (op.equals("+")) { //加法運(yùn)算
result = d1 + d2;
} else if (op.equals("-")) { //減法運(yùn)算
result = d1 - d2;
} else if (op.equals("*")) { //乘法運(yùn)算
result = d1 * d2;
} else if (op.equals("/")) { //除法運(yùn)算
if (d2 == 0) { //如果被除數(shù)是0
result = 0; //則結(jié)果是0
}
else {
result = d1 / d2;
}
}
text.setText(str1 + " = " + result); //顯示計(jì)算結(jié)果
if (!s1.contains(".") && !s2.contains(".") && !op.equals("/")) {//如果兩個(gè)整數(shù)且不是出發(fā)運(yùn)算
int r = (int) result; //則結(jié)果轉(zhuǎn)為整數(shù)
text.setText(str1 + " = " + r );
}
}
}
三、運(yùn)行測(cè)試




測(cè)試結(jié)果:
1.可以計(jì)算簡(jiǎn)單兩位數(shù)的四則運(yùn)算,但是如果計(jì)算超過(guò)2位數(shù)的運(yùn)算,則會(huì)出現(xiàn)異常使程序退出。
2.四則運(yùn)算中,結(jié)果可以為負(fù)數(shù),但是運(yùn)算數(shù)若為負(fù)數(shù),則會(huì)出現(xiàn)異常,原因是該程序公式為【數(shù)字1 + 運(yùn)算符 +數(shù)字二】,若輸入負(fù)數(shù),即多出一位運(yùn)算符,則會(huì)拋出異常。
四、總結(jié)
總的來(lái)說(shuō),這個(gè)計(jì)算器確實(shí)十分簡(jiǎn)單,功能也不完善,還有很多小bug,但是對(duì)于剛?cè)腴T(mén)的菜鳥(niǎo)來(lái)說(shuō),也用了不少的時(shí)間。希望自己能更加努力地堅(jiān)持學(xué)習(xí)下去!
更多計(jì)算器功能實(shí)現(xiàn),請(qǐng)點(diǎn)擊專(zhuān)題: 計(jì)算器功能匯總 進(jìn)行學(xué)習(xí)
關(guān)于Android計(jì)算器功能的實(shí)現(xiàn),查看專(zhuān)題:Android計(jì)算器 進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- android studio實(shí)現(xiàn)計(jì)算器
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器(表格布局TableLayout)
- Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
- Android Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)
- Android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器
- android?studio實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
相關(guān)文章
Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條功能
這篇文章主要介紹了Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
詳解Android Webview加載網(wǎng)頁(yè)時(shí)發(fā)送HTTP頭信息
這篇文章主要介紹了詳解Android Webview加載網(wǎng)頁(yè)時(shí)發(fā)送HTTP頭信息的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android開(kāi)發(fā)之TabActivity用法實(shí)例詳解
這篇文章主要介紹了Android開(kāi)發(fā)之TabActivity用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android擴(kuò)展Activity實(shí)現(xiàn)標(biāo)簽頁(yè)效果的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android利用DownloadManager實(shí)現(xiàn)文件下載
這篇文章主要為大家詳細(xì)介紹了Android利用DownloadManager實(shí)現(xiàn)文件下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Android Studio多工程引用同一個(gè)library項(xiàng)目配置的解決方法
大家在使用android studio的時(shí)候,會(huì)遇到多個(gè)項(xiàng)目引用相同的library這篇文章主要介紹了Android Studio多工程引用同一個(gè)library項(xiàng)目配置方法,需要的朋友可以參考下2018-03-03
Android通過(guò)RemoteViews實(shí)現(xiàn)跨進(jìn)程更新UI示例
本篇文章主要介紹了Android通過(guò)RemoteViews實(shí)現(xiàn)跨進(jìn)程更新UI示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
mui,h5+中實(shí)現(xiàn)控制頁(yè)面load顯示
這篇文章主要介紹了mui,h5+中實(shí)現(xiàn)控制頁(yè)面load顯示的相關(guān)代碼寫(xiě)法和運(yùn)用技巧,需要的朋友參考一下。2017-11-11
Ubantu16.04進(jìn)行Android 8.0源碼編譯的流程
這篇文章主要介紹了Ubantu16.04進(jìn)行Android 8.0源碼編譯的相關(guān)資料,需要的朋友可以參考下2018-02-02

