簡單實現(xiàn)Android計算器功能
自己寫的安卓的計算器:
注:這個是在mac中開發(fā)的,如果要在windows的eclipse中運行可能會出現(xiàn)路徑問題,解決辦法從windows中已有的安卓工程根目錄下復(fù)制一下classpath文件,然后復(fù)制粘貼覆蓋掉這個工程根目錄里面的路徑文件,再導(dǎo)入工程應(yīng)該就可以打開了。
安卓計算器Android <wbr>calculator
工程下載鏈接:https://github.com/jiangxh1992/Android-Calculator
代碼:
package com.example.calculator; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ //按鈕 Button btn_1,btn_2,btn_3, btn_4,btn_5,btn_6, btn_7,btn_8,btn_9, btn_10,btn_11,btn_12, btn_13,btn_14,btn_15, btn_16,btn_17,btn_18, btn_19,btn_20,btn_21,btn_22; //用于判斷是否需要重新輸入 boolean restart; //屏幕上數(shù)字的正負(fù)狀態(tài) int front; //操作數(shù)與操作符 float operator1,operator2; int symbol;//jre1.6不支持switch(字符串) //"+、—、*、/、x^y、/x、sinx、cosx"-->1、2、3、4、5、6、7、8 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化操作數(shù)(默認(rèn)為0)操作符(默認(rèn)為+) operator1=0; operator2=0; restart=true; front=0;//默認(rèn)為正數(shù) TextView txt=(TextView) findViewById(R.id.textView1); txt.setText("00"); //獲取button對象 btn_1= (Button)findViewById(R.id.button1); btn_2= (Button)findViewById(R.id.button2); btn_3= (Button)findViewById(R.id.button3); btn_4= (Button)findViewById(R.id.button4); btn_5= (Button)findViewById(R.id.button5); btn_6= (Button)findViewById(R.id.button6); btn_7= (Button)findViewById(R.id.button7); btn_8= (Button)findViewById(R.id.button8); btn_9= (Button)findViewById(R.id.button9); btn_10= (Button)findViewById(R.id.button10); btn_11= (Button)findViewById(R.id.button11); btn_12= (Button)findViewById(R.id.button12); btn_13= (Button)findViewById(R.id.button13); btn_14= (Button)findViewById(R.id.button14); btn_15= (Button)findViewById(R.id.button15); btn_16= (Button)findViewById(R.id.button16); btn_17= (Button)findViewById(R.id.button17); btn_18= (Button)findViewById(R.id.button18); btn_19= (Button)findViewById(R.id.button19); btn_20= (Button)findViewById(R.id.button20); btn_21= (Button)findViewById(R.id.button21); btn_22= (Button)findViewById(R.id.button22); //注冊點擊事件 btn_1.setOnClickListener(this); btn_2.setOnClickListener(this); btn_3.setOnClickListener(this); btn_4.setOnClickListener(this); btn_5.setOnClickListener(this); btn_6.setOnClickListener(this); btn_7.setOnClickListener(this); btn_8.setOnClickListener(this); btn_9.setOnClickListener(this); btn_10.setOnClickListener(this); btn_11.setOnClickListener(this); btn_12.setOnClickListener(this); btn_13.setOnClickListener(this); btn_14.setOnClickListener(this); btn_15.setOnClickListener(this); btn_16.setOnClickListener(this); btn_17.setOnClickListener(this); btn_18.setOnClickListener(this); btn_19.setOnClickListener(this); btn_20.setOnClickListener(this); btn_21.setOnClickListener(this); btn_22.setOnClickListener(this); } @Override //點擊事件處理 public void onClick(View btn) { //獲取文本框 TextView txt=(TextView) findViewById(R.id.textView1); //文本框內(nèi)容 String text=(String) txt.getText(); float value=Float.parseFloat(text); System.out.println(value); switch(btn.getId()){ //數(shù)字 case R.id.button1: if(restart){ txt.setText("1"); restart=false;} else txt.setText(text+"1"); break; case R.id.button2: if(restart){ txt.setText("2"); restart=false;} else txt.setText(text+"2"); break; case R.id.button3: if(restart){ txt.setText("3"); restart=false;} else txt.setText(text+"3"); break; case R.id.button4: if(restart){ txt.setText("4"); restart=false;} else txt.setText(text+"4"); break; case R.id.button5: if(restart){ txt.setText("5"); restart=false;} else txt.setText(text+"5"); break; case R.id.button6: if(restart){ txt.setText("6"); restart=false;} else txt.setText(text+"6"); break; case R.id.button7: if(restart){ txt.setText("7"); restart=false;} else txt.setText(text+"7"); break; case R.id.button8: if(restart){ txt.setText("8"); restart=false;} else txt.setText(text+"8"); break; case R.id.button9: if(restart){ txt.setText("9"); restart=false;} else txt.setText(text+"9"); break; case R.id.button10: if(restart) txt.setText("00"); else txt.setText(text+"0"); break; //+ case R.id.button11: symbol=1; operator1=value; restart=true; break; //- case R.id.button12: symbol=2; operator1=value; restart=true; break; //-/+ case R.id.button13: if(restart){ txt.setText("-"); front=1;} else if(front==0){ txt.setText("-"+text); front=1;} else if(front==1){ txt.setText(text.substring(1)); front=0; } break; //* case R.id.button14: symbol=3; operator1=value; restart=true; break; // / case R.id.button15: symbol=4; operator1=value; restart=true; break; //C case R.id.button16: txt.setText("00"); restart=true; break; // . case R.id.button17: if(restart) txt.setText("."); else txt.setText(text+"."); restart=false; break; //= case R.id.button18: restart=true; switch(symbol){ case 1: txt.setText(String.valueOf(operator1+value)); break; case 2: txt.setText(String.valueOf(operator1-value)); break; case 3: txt.setText(String.valueOf(operator1*value)); break; case 4: txt.setText(String.valueOf(operator1/value)); break; case 5: txt.setText(String.valueOf(Math.pow(operator1, value))); break; } break; //x^y case R.id.button19: symbol=5; operator1=value; restart=true; break; // /x case R.id.button20: symbol=6; txt.setText(String.valueOf(Math.sqrt(value))); restart=true; break; // sinx case R.id.button21: symbol=7; txt.setText(String.valueOf(Math.sin(value))); restart=true; break; // cosx case R.id.button22: symbol=8; txt.setText(String.valueOf(Math.cos(value))); restart=true; break; default: break; } } }
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中LinearLayout布局的常用屬性總結(jié)
這篇文章主要介紹了Android中LinearLayout布局的常用屬性總結(jié),包括居中、重心、比例等線性布局中的基本設(shè)置,需要的朋友可以參考下2016-04-04Android fragment實現(xiàn)按鈕點擊事件的示例講解
下面小編就為大家分享一篇Android fragment實現(xiàn)按鈕點擊事件的示例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android實現(xiàn)文件或文件夾壓縮成.zip格式壓縮包
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)文件或文件夾壓縮成.zip格式壓縮包,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07React-Native之Android(6.0及以上)權(quán)限申請詳解
這篇文章主要介紹了React-Native之Android(6.0及以上)權(quán)限申請詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Android開發(fā)中PopupWindow用法實例分析
這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下2016-02-02android開發(fā)教程之使用線程實現(xiàn)視圖平滑滾動示例
這篇文章主要介紹了android使用線程實現(xiàn)視圖平滑滾動示例,需要的朋友可以參考下2014-03-03