您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > android简单计算器代码
packagehua.xing.demo;importandroid.app.Activity;importandroid.os.Bundle;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;publicclassCalculatordemoActivityextendsActivityimplementsOnClickListener{/**Calledwhentheactivityisfirstcreated.*/privateButtonbut0=null;privateButtonbut1=null;privateButtonbut2=null;privateButtonbut3=null;privateButtonbut4=null;privateButtonbut5=null;privateButtonbut6=null;privateButtonbut7=null;privateButtonbut8=null;privateButtonbut9=null;privateButtondim=null;privateButtonadd=null;privateButtonsub=null;privateButtonmult=null;privateButtondiv=null;privateButtonequ=null;privateEditTextetext=null;privatedoublevalue=0;privateStringstr1=null;privateStringtem=;privateStringsign=a;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);super.setContentView(R.layout.main);add=(Button)findViewById(R.id.add);sub=(Button)findViewById(R.id.sub);mult=(Button)findViewById(R.id.mult);div=(Button)findViewById(R.id.divid);equ=(Button)findViewById(R.id.caculate);but1=(Button)findViewById(R.id.but1);but3=(Button)findViewById(R.id.but3);but2=(Button)findViewById(R.id.but2);but4=(Button)findViewById(R.id.but4);but5=(Button)findViewById(R.id.but5);but6=(Button)findViewById(R.id.but6);but7=(Button)findViewById(R.id.but7);but8=(Button)findViewById(R.id.but8);but9=(Button)findViewById(R.id.but9);but0=(Button)findViewById(R.id.but0);dim=(Button)findViewById(R.id.dim);etext=(EditText)findViewById(R.id.etext);add.setOnClickListener(newAListener());sub.setOnClickListener(newAListener());mult.setOnClickListener(newAListener());div.setOnClickListener(newAListener());equ.setOnClickListener(newListener());but1.setOnClickListener(this);but3.setOnClickListener(this);but2.setOnClickListener(this);but4.setOnClickListener(this);but5.setOnClickListener(this);but6.setOnClickListener(this);but7.setOnClickListener(this);but8.setOnClickListener(this);but9.setOnClickListener(this);but0.setOnClickListener(this);dim.setOnClickListener(this);}@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubif(v.equals(but1)){tem=tem+1;etext.setText(tem);}if(v.equals(but2)){tem=tem+2;etext.setText(tem);}if(v.equals(but3)){tem=tem+3;etext.setText(tem);}if(v.equals(but4)){tem=tem+4;etext.setText(tem);}if(v.equals(but5)){tem=tem+5;etext.setText(tem);}if(v.equals(but6)){tem=tem+6;etext.setText(tem);}if(v.equals(but7)){tem=tem+7;etext.setText(tem);}if(v.equals(but8)){tem=tem+8;etext.setText(tem);}if(v.equals(but9)){tem=tem+9;etext.setText(tem);}if(v.equals(but0)){tem=tem+0;etext.setText(tem);}}classAListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewarg){//TODOAuto-generatedmethodstubstr1=etext.getText().toString();if(str1.equals()){}else{doubled=Double.parseDouble(str1);if(sign==a){value=d;}if(sign==*){value=value*d;}if(sign==-){value=value-d;}if(sign==/){value=value/d;}if(sign==+){value=d+value;}}etext.setText();if(arg.equals(add)){sign=+;}if(arg.equals(div)){sign=/;}if(arg.equals(mult)){sign=*;}if(arg.equals(sub)){sign=-;}tem=;}}classListenerimplementsOnClickListener{@OverridepublicvoidonClick(Viewarg){//TODOAuto-generatedmethodstubstr1=etext.getText().toString();if(str1.equals()){}else{doubled=Double.parseDouble(str1);if(sign==a){value=d;}if(sign==*){value=value*d;}if(sign==-){value=value-d;}if(sign==/){value=value/d;}if(sign==+){value=d+value;}}etext.setText(value+);sign=a;value=0;tem=;}}}?xmlversion=1.0encoding=utf-8?RelativeLayoutxmlns:android=:layout_width=fill_parentandroid:layout_height=fill_parentandroid:orientation=verticalEditTextandroid:id=@+id/etextandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:layout_margin=5px/EditTextButtonandroid:id=@+id/but7android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/etextandroid:text=7android:textSize=30px/Buttonandroid:id=@+id/but8android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/etextandroid:layout_toRightOf=@id/but7android:text=8android:textSize=30px/Buttonandroid:id=@+id/but9android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/etextandroid:layout_toRightOf=@id/but8android:text=9android:textSize=30px/Buttonandroid:id=@+id/but4android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/but7android:text=4android:textSize=30px/Buttonandroid:id=@+id/but5android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/but8android:layout_toRightOf=@id/but4android:text=5android:textSize=30px/Buttonandroid:id=@+id/but6android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/but9android:layout_toRightOf=@id/but5android:text=6android:textSize=30px/Buttonandroid:id=@+id/but1android:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:layout_below=@id/but4android
本文标题:android简单计算器代码
链接地址:https://www.777doc.com/doc-2057937 .html