Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器源碼
本文實(shí)例為大家分享了Android Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
源碼:
布局樣式:
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="horizontal" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:baselineAligned="false" ? ? android:background="@color/grey" ? ? android:columnCount="4" ? ? android:rowCount="6"> ? ? <TextView ? ? ? ? android:id="@+id/textview_01" ? ? ? ? android:layout_columnSpan="4" ? ? ? ? android:layout_gravity="fill" ? ? ? ? android:gravity="right" ? ? ? ? android:layout_marginLeft="5dp" ? ? ? ? android:layout_marginRight="5dp" ? ? ? ? android:background="@color/grey" ? ? ? ? android:paddingRight="20dp" ? ? ? ? android:paddingTop="40dp" ? ? ? ? android:text="0" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:textSize="88sp"> ? ? </TextView> ? ? <Button ? ? ? ? android:id="@+id/button_return" ? ? ? ? android:layout_columnSpan="2" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="2dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="30dp" ? ? ? ? android:textStyle="bold" ? ? ? ? android:text="回退" /> ? ? <Button ? ? ? ? android:id="@+id/button_clear" ? ? ? ? android:layout_columnSpan="2" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="2dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="30dp" ? ? ? ? android:textStyle="bold" ? ? ? ? android:text="清空"/> ? ? <Button ? ? ? ? android:id="@+id/button_add" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="+"/> ? ? <Button ? ? ? ? android:id="@+id/button_01" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="1"/> ? ? <Button ? ? ? ? android:id="@+id/button_02" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="2"/> ? ? <Button ? ? ? ? android:id="@+id/button_03" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="3"/> ? ? <Button ? ? ? ? android:id="@+id/button_reduce" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="-"/> ? ? <Button ? ? ? ? android:id="@+id/button_04" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="4"/> ? ? <Button ? ? ? ? android:id="@+id/button_05" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="5"/> ? ? <Button ? ? ? ? android:id="@+id/button_06" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="6"/> ? ? <Button ? ? ? ? android:id="@+id/button_ride" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="*"/> ? ? <Button ? ? ? ? android:id="@+id/button_07" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="7"/> ? ? <Button ? ? ? ? android:id="@+id/button_08" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="8"/> ? ? <Button ? ? ? ? android:id="@+id/button_09" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="9"/> ? ? <Button ? ? ? ? android:id="@+id/button_except" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="/"/> ? ? <Button ? ? ? ? android:id="@+id/button_spot" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="."/> ? ? <Button ? ? ? ? android:id="@+id/button_00" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="0"/> ? ? <Button ? ? ? ? android:id="@+id/button_equal" ? ? ? ? android:layout_columnSpan="1" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_columnWeight="1" ? ? ? ? android:layout_rowWeight="1" ? ? ? ? android:layout_margin="1dp" ? ? ? ? android:background="@color/white" ? ? ? ? android:textSize="50dp" ? ? ? ? android:text="="/> </GridLayout>
Java部分源碼:
package com.example.pyfcalculator; ? import androidx.appcompat.app.AppCompatActivity; ? import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.TextView; ? public class MainActivity extends AppCompatActivity implements View.OnClickListener { ? ? ? TextView textView; ? ? Button button_return,button_00,button_01,button_02,button_03, ? ? ? ? ? ? button_04,button_05,button_06,button_07,button_08,button_09,button_add, ? ? ? ? ? ? button_clear,button_equal,button_except,button_reduce,button_ride,button_spot; ? ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? initViewAndListener(); ? ? } ? ? ? ? //注冊(cè)監(jiān)聽(tīng)方法 ? ? protected void initViewAndListener(){ ? ? ? ? button_return=findViewById(R.id.button_return); ? ? ? ? button_00=findViewById(R.id.button_00); ? ? ? ? button_01=findViewById(R.id.button_01); ? ? ? ? button_02=findViewById(R.id.button_02); ? ? ? ? button_03=findViewById(R.id.button_03); ? ? ? ? button_04=findViewById(R.id.button_04); ? ? ? ? button_05=findViewById(R.id.button_05); ? ? ? ? button_06=findViewById(R.id.button_06); ? ? ? ? button_07=findViewById(R.id.button_07); ? ? ? ? button_08=findViewById(R.id.button_08); ? ? ? ? button_09=findViewById(R.id.button_09); ? ? ? ? button_add=findViewById(R.id.button_add); ? ? ? ? button_clear=findViewById(R.id.button_clear); ? ? ? ? button_equal=findViewById(R.id.button_equal); ? ? ? ? button_except=findViewById(R.id.button_except); ? ? ? ? button_reduce=findViewById(R.id.button_reduce); ? ? ? ? button_ride=findViewById(R.id.button_ride); ? ? ? ? button_spot=findViewById(R.id.button_spot); ? ? ? ? ? button_return.setOnClickListener(this); ? ? ? ? button_00.setOnClickListener(this); ? ? ? ? button_01.setOnClickListener(this); ? ? ? ? button_02.setOnClickListener(this); ? ? ? ? button_03.setOnClickListener(this); ? ? ? ? button_04.setOnClickListener(this); ? ? ? ? button_05.setOnClickListener(this); ? ? ? ? button_06.setOnClickListener(this); ? ? ? ? button_07.setOnClickListener(this); ? ? ? ? button_08.setOnClickListener(this); ? ? ? ? button_09.setOnClickListener(this); ? ? ? ? button_add.setOnClickListener(this); ? ? ? ? button_clear.setOnClickListener(this); ? ? ? ? button_equal.setOnClickListener(this); ? ? ? ? button_except.setOnClickListener(this); ? ? ? ? button_reduce.setOnClickListener(this); ? ? ? ? button_ride.setOnClickListener(this); ? ? ? ? button_spot.setOnClickListener(this); ? ? ? ? ? textView=findViewById(R.id.textview_01); ? ? } ? ? ? private String num_in_now;//記錄當(dāng)前結(jié)果 ? ? private boolean flag=false;//記錄計(jì)算器狀態(tài) ? ? int now_num=0; ? ? ? @Override ? ? public void onClick(View view) { ? ? ? ? ? //獲取文本框內(nèi)容 ? ? ? ? String currentText=textView.getText().toString(); ? ? ? ? ? switch (view.getId()){ ? ? ? ? ? ? case R.id.button_00: ? ? ? ? ? ? ? ? if ((textView.getText().toString().equals("0"))) ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? isflag("0"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_01: ? ? ? ? ? ? ? ? isflag("1"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_02: ? ? ? ? ? ? ? ? isflag("2"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_03: ? ? ? ? ? ? ? ? isflag("3"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_04: ? ? ? ? ? ? ? ? isflag("4"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_05: ? ? ? ? ? ? ? ? isflag("5"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_06: ? ? ? ? ? ? ? ? isflag("6"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_07: ? ? ? ? ? ? ? ? isflag("7"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_08: ? ? ? ? ? ? ? ? isflag("8"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_09: ? ? ? ? ? ? ? ? isflag("9"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_add: // ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"+"); ? ? ? ? ? ? ? ? now_num++; ? ? ? ? ? ? ? ? if(now_num>1){ ? ? ? ? ? ? ? ? ? ? equal(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? isflag("+"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_except: // ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"/"); ? ? ? ? ? ? ? ? now_num++; ? ? ? ? ? ? ? ? if(now_num>1){ ? ? ? ? ? ? ? ? ? ? equal(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? isflag("/"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_spot: // ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"."); ? ? ? ? ? ? ? ? isflag("."); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_reduce: // ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"-"); ? ? ? ? ? ? ? ? now_num++; ? ? ? ? ? ? ? ? if(now_num>1){ ? ? ? ? ? ? ? ? ? ? equal(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? isflag("-"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_ride: // ? ? ? ? ? ? ? ?textView.setText(textView.getText()+"*"); ? ? ? ? ? ? ? ? now_num++; ? ? ? ? ? ? ? ? if(now_num>1){ ? ? ? ? ? ? ? ? ? ? equal(); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? isflag("*"); ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_clear: ? ? ? ? ? ? ? ? textView.setText("0"); ? ? ? ? ? ? ? ? flag=false; ? ? ? ? ? ? ? ? now_num=0; ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_return: ? ? ? ? ? ? ? ? if(TextUtils.isEmpty(textView.getText())){ ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (currentText.substring(0,currentText.length()-1).length()>0){ ? ? ? ? ? ? ? ? ? ? textView.setText(currentText.substring(0,currentText.length()-1)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else{ ? ? ? ? ? ? ? ? ? ? textView.setText("0"); ? ? ? ? ? ? ? ? ? ? flag=false; ? ? ? ? ? ? ? ? ? ? now_num=0; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case R.id.button_equal: ? ? ? ? ? ? ? ? if(TextUtils.isEmpty(textView.getText())){ ? ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? equal(); ? ? ? ? ? ? ? ? flag=false; ? ? ? ? ? ? ? ? now_num=0; ? ? ? ? ? ? ? ? break; ? ? ? ? } ? ? ? } ? ? ? private void equal(){ ? ? ? ? textView.setText(operation(textView.getText().toString())); ? ? } ? ? ? //運(yùn)算方法 ? ? private String operation(String string){ ? ? ? ? int index; ? ? ? ? double num_1,num_2; ? ? ? ? if (string.contains("+")){ ? ? ? ? ? ? index=string.indexOf("+"); ? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index)); ? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length())); ? ? ? ? ? ? return num_1+num_2+""; ? ? ? ? } ? ? ? ? if (string.contains("-")){ ? ? ? ? ? ? index=string.indexOf("-"); ? ? ? ? ? ? if(index==0){ ? ? ? ? ? ? ? ? String string_2=string.substring(1,string.length()); ? ? ? ? ? ? ? ? int index_2=string_2.indexOf("-"); ? ? ? ? ? ? ? ? num_1= Double.parseDouble(string_2.substring(0,index_2)); ? ? ? ? ? ? ? ? num_2= Double.parseDouble(string_2.substring(index_2+1,string_2.length())); ? ? ? ? ? ? ? ? return (-(num_1+num_2))+""; ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index)); ? ? ? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length())); ? ? ? ? ? ? ? ? return num_1-num_2+""; ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? if (string.contains("*")){ ? ? ? ? ? ? index=string.indexOf("*"); ? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index)); ? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length())); ? ? ? ? ? ? return num_1*num_2+""; ? ? ? ? } ? ? ? ? if (string.contains("/")){ ? ? ? ? ? ? index=string.indexOf("/"); ? ? ? ? ? ? num_1= Double.parseDouble(string.substring(0,index)); ? ? ? ? ? ? num_2= Double.parseDouble(string.substring(index+1,string.length())); ? ? ? ? ? ? return num_1/num_2+""; ? ? ? ? } ? ? ? ? return "error!!!"; ? ? } ? ? ? //判斷0要不要被替換 看flag的狀態(tài) 輸入數(shù)字 ? ? private void isflag(String info){ ? ? ? ? if (flag){ ? ? ? ? ? ? textView.setText(textView.getText()+info); ? ? ? ? }else{ ? ? ? ? ? ? if(info.equals(".")||info.equals("+")||info.equals("-")||info.equals("*")||info.equals("/")){ ? ? ? ? ? ? ? ? textView.setText(textView.getText()+info); ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? textView.setText(info); ? ? ? ? ? ? } ? ? ? ? ? ? flag=true; ? ? ? ? } ? ? } ? }
其余資源沒(méi)有太大難度,就不一展現(xiàn)了!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序—微信跳一跳,Android游戲助手(外掛)使用教程詳解
這篇文章主要介紹了微信小程序—微信跳一跳,Android游戲助手(外掛)使用教程詳解,需要的朋友可以參考下2018-01-01android開(kāi)發(fā)教程之framework增加字符串資源和圖片等resource資源
這篇文章主要介紹了android開(kāi)發(fā)中framework增加字符串資源和圖片等resource資源方法,需要的朋友可以參考下2014-02-02Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能
這篇文章主要介紹了Flutter 實(shí)現(xiàn)虎牙/斗魚 彈幕功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android:“萬(wàn)能”Activity重構(gòu)篇
本文主要介紹了mvp以及每一層,以及使用mvp來(lái)重構(gòu)“萬(wàn)能”Activity,其實(shí)每一層需要注意的東西還有很多,比如model層是最難寫的一層。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01Android屏蔽軟鍵盤并且顯示光標(biāo)的實(shí)例詳解
這篇文章主要介紹了Android屏蔽軟鍵盤并且顯示光標(biāo)的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10Android實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫
這篇文章主要為大家詳細(xì)介紹了Android短信驗(yàn)證碼自動(dòng)填寫功能的實(shí)現(xiàn)過(guò)程,感興趣的小伙伴們可以參考一下2016-08-08Android實(shí)現(xiàn)環(huán)形進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)環(huán)形進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析
下面小編就為大家?guī)?lái)一篇Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
這篇文章主要為大家詳細(xì)介紹了Android折疊式Toolbar的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02android下拉刷新ListView的介紹和實(shí)現(xiàn)代碼
在當(dāng)下,列表組件不帶下拉刷新的都不好意思叫列表。第一次完成列表的下拉刷新功能的時(shí)候,直接在Activity中實(shí)現(xiàn),雖然功能上是實(shí)現(xiàn)了,總體上感覺(jué)很亂。所以第二次用到的時(shí)候,就想著封裝成一個(gè)組件,實(shí)現(xiàn)和Activity的解耦。2013-04-04