计算器源码Java程序代码:package ymq.demo03;import android.app.Activity;import android.os.Bundle;import android.view.*;import android.widget.*;public class demo03 extends Activity {/** Called when the activity is first created. */String str="";EditText et;int c=0,flag=0;double b=0.0,g=0.0,f=0.0;View vi;public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stubmenu.add(0, 1, 1, "退出");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stubif(item.getItemId()==1){finish();}return super.onOptionsItemSelected(item);}//计算方法public double calculater(){switch(c){case 0:f=g;break;case 1:f=b+g;break;case 2:f=b-g;break;case 3:f=b*g;break;case 4:f=b/g;break;}b=f;return f;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);//获得按键final Button number[]=new Button[10];final Button fuhao[]=new Button[11];fuhao[0]=(Button)findViewById(R.id.button01);fuhao[1]=(Button)findViewById(R.id.button02);fuhao[2]=(Button)findViewById(R.id.button03);fuhao[3]=(Button)findViewById(R.id.button04);fuhao[4]=(Button)findViewById(R.id.button05);fuhao[5]=(Button)findViewById(R.id.button06);fuhao[6]=(Button)findViewById(R.id.buttonce);fuhao[7]=(Button)findViewById(R.id.buttonc);fuhao[8]=(Button)findViewById(R.id.zheng);fuhao[9]=(Button)findViewById(R.id.kaifang);fuhao[10]=(Button)findViewById(R.id.pingfang);number[0]=(Button)findViewById(R.id.button0);number[1]=(Button)findViewById(R.id.button1);number[2]=(Button)findViewById(R.id.button2);number[3]=(Button)findViewById(R.id.button3);number[4]=(Button)findViewById(R.id.button4);number[5]=(Button)findViewById(R.id.button5);number[6]=(Button)findViewById(R.id.button6);number[7]=(Button)findViewById(R.id.button7);number[8]=(Button)findViewById(R.id.button8);number[9]=(Button)findViewById(R.id.button9);et=(EditText) findViewById(R.id.textView1);et.setText(str);fuhao[6].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubstr="";et.setText(str);vi=v;}});fuhao[7].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubb=0.0;c=0;g=0.0;str="";et.setText(str);}});fuhao[8].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(vi!=fuhao[5]&&str!=""){char ch=str.charAt(0);if(ch=='-')str=str.replace("-","");elsestr="-"+str;et.setText(str);}}});fuhao[9].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""){double a=Double.parseDouble(str);str=Math.sqrt(a)+"";et.setText(str);});fuhao[10].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""){double a=Double.parseDouble(str);str=""+a*a;et.setText(str);}}});//设定数字按键number[0].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=0;et.setText(str);flag=0;}else{char ch1[];ch1=str.toCharArray();if(!(ch1.length==1&&ch1[0]=='0')){str+=0;et.setText(str);}}vi=v;}});number[1].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=1;et.setText(str);flag=0;}else{str+=1;et.setText(str);}vi=v;}});number[2].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=2;et.setText(str);flag=0;}else{str+=2;et.setText(str);}vi=v;}});number[3].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=3;et.setText(str);flag=0;}else{str+=3;et.setText(str);}vi=v;}});number[4].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=4;et.setText(str);flag=0;}else{str+=4;et.setText(str);}vi=v;}});number[5].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=5;et.setText(str);flag=0;}else{str+=5;et.setText(str);}vi=v;}});number[6].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=6;et.setText(str);flag=0;}else{str+=6;et.setText(str);}vi=v;}});number[7].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=7;et.setText(str);flag=0;}else{str+=7;et.setText(str);}vi=v;}});number[8].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=8;et.setText(str);flag=0;}else{str+=8;et.setText(str);}vi=v;}});number[9].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(flag==1){str="";str+=9;et.setText(str);flag=0;}else{str+=9;et.setText(str);}vi=v;}});//设定符号键//加fuhao[0].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""){if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){c=1;}else{g=Double.parseDouble(str);calculater();str=""+f;et.setText(str);c=1;flag=1;}}}});//减fuhao[1].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""){if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){c=2;}else{g=Double.parseDouble(str);calculater();str=""+f;et.setText(str);c=2;flag=1;vi=v;}}}});//乘fuhao[2].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""){if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){c=3;}else{g=Double.parseDouble(str);calculater();str=""+f;et.setText(str);flag=1;vi=v;}}}});//除fuhao[3].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""){if(vi==fuhao[0]||vi==fuhao[1]||vi==fuhao[2]||vi==fuhao[3]){c=4;}else{g=Double.parseDouble(str);calculater();str=""+f;et.setText(str);c=4;flag=1;vi=v;}}}});//等号fuhao[4].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str!=""&&vi!=fuhao[0]&&vi!=fuhao[1]&&vi!=fuhao[2]&&vi!=fuhao[3 ]){g=Double.parseDouble(str);calculater();str=""+f;et.setText(str);flag=1;vi=v;}}});//小数点fuhao[5].setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubif(str==""){str+=".";et.setText(str);}else{char ch1[];int x=0;ch1=str.toCharArray();for(int i=0;i<ch1.length;i++)if(ch1[i]=='.')x++;if(x==0){str+=".";et.setText(str);}}}});}}布局文件:main.xml<?xml version="1.0"encoding="utf-8"?><LinearLayoutandroid:background="#ff808080"xmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"android:layout_gravity="center_vertical"android:id="@+id/zhengfu"><EditTextandroid:text="TextView"android:id="@+id/textView1"android:textSize="25sp"android:textColor="#000000"android:layout_gravity="center"android:gravity="left"android:cursorVisible="false"android:editable = "false"android:layout_height="wrap_content"android:layout_width="fill_parent"android:layout_marginLeft="4dip"android:layout_marginRight="4dip"android:layout_marginTop="4dip"android:layout_weight="1.5"></EditText><TableLayoutandroid:id="@+id/tablelayout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="4dip"android:layout_weight="10"><LinearLayoutandroid:id="@+id/linearlayout02"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"><Buttonandroid:layout_width="50dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:id="@+id/buttonc"android:text="@string/shan"></Button><Buttonandroid:layout_width="50dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:id="@+id/buttonce" android:text="@string/shanchu"></Button></LinearLayout><TableRowandroid:id="@+id/row0"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/one" android:id="@+id/button1"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/two" android:id="@+id/button2"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/three" android:id="@+id/button3"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/jia" android:id="@+id/button01"></Button></TableRow><TableRowandroid:id="@+id/row1"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/fore" android:id="@+id/button4"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/five" android:id="@+id/button5"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/six" android:id="@+id/button6"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/jian" android:id="@+id/button02"></Button></TableRow><TableRowandroid:id="@+id/row2"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/seven" android:id="@+id/button7"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/eight" android:id="@+id/button8"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/nine" android:id="@+id/button9"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/cheng" android:id="@+id/button03"></Button></TableRow><TableRowandroid:id="@+id/row3"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/zero" android:id="@+id/button0"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:id="@+id/button06" android:text="@string/dihao"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/dengyu" android:id="@+id/button05"></Button><Buttonandroid:layout_width="40dip"android:layout_height="fill_parent"android:layout_weight="1"android:textSize="25sp"android:text="@string/chu" android:id="@+id/button04"></Button></TableRow><LinearLayoutandroid:id="@+id/tableRow1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_weight="1"><Buttonandroid:textSize="25sp"android:layout_height="fill_parent"android:layout_width="50dp"android:layout_weight="1" android:text="@string/zhengfu"android:id="@+id/zheng"></Button><Buttonandroid:id="@+id/pingfang"android:textSize="25sp"android:layout_height="fill_parent"android:layout_width="50dp"android:layout_weight="1" android:text="@string/ping"></Button><Buttonandroid:id="@+id/kaifang"android:textSize="25sp"android:layout_height="fill_parent"android:layout_width="50dp"android:layout_weight="1" android:text="@string/kai"></Button></LinearLayout></TableLayout></LinearLayout>主配置文件:<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="ymq.demo03"android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="7"/><application android:icon="@drawable/icon"android:label="@string/app_name"><activity android:name=".demo03"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><categoryandroid:name="UNCHER"/></intent-filter></activity></application></manifest>。