安卓计算机源代码
android简单计算器代码
android简单计算器代码————————————————————————————————作者:————————————————————————————————日期:2package hua.xing.demo;import android。
app。
Activity;import android。
os。
Bundle;import android.view。
View;import android.view.View.OnClickListener;import android。
widget.Button;import android.widget。
EditText;public class CalculatordemoActivity extends Activity implements OnClickListener { /** Called when the activity is first created。
*/private Button but0 = null;private Button but1 = null;private Button but2 = null;private Button but3 = null;private Button but4 = null;private Button but5 = null;private Button but6 = null;private Button but7 = null;private Button but8 = null;private Button but9 = null;private Button dim = null;private Button add = null;private Button sub = null;private Button mult = null;private Button div = null;private Button equ = null;private EditText etext = null;private double value = 0;private String str1 = null;private String tem = ””;private String sign = ”a";@Overridepublic void onCreate(Bundle savedInstanceState){ super。
简单的计算器源代码
ห้องสมุดไป่ตู้
public void actionPerformed(ActionEvent e){ //数字事件 单单单 if(e.getSource()==s1) 单单单单 num(1); 单单单 else if(e.getSource()==s2) 单单单 num(2); 单单单 else if(e.getSource()==s3) 单单单 num(3); 单单单 else if(e.getSource()==s4) 单单单 num(4); 单单单 else if(e.getSource()==s5) 单单单 num(5); 单单单 else if(e.getSource()==s6) 单单单 num(6); 单单单 else if(e.getSource()==s7) 单单单 num(7); 单单单 else if(e.getSource()==s8) 单单单 num(8); 单单单 else if(e.getSource()==s9) 单单单 num(9); 单单单 else if(e.getSource()==s0) 单单单 num(0); 单单单 单单单 //符号事件单 单单单 else if(e.getSource()==b1) 单单单 sign(1); 单单单 else if(e.getSource()==b2) 单单单 sign(2); 单单单 else if(e.getSource()==b3) 单单单 sign(3); 单单单 else if(e.getSource()==b4) 单单单 sign(4); 单单单 //等号 单单单 else if(e.getSource()==f1){ 单单单 str=resultField.getText(); 单单单 if(str.indexOf(".")<=1){ 单单单 单str+="."; 单单单 单resultField.setText(str); 单单单 单} 单单单 } 单单单 else if(e.getSource()==f2){ 单单单 num2=Double.parseDouble(resultField.getText());
最新Android计算器代码编写汇总
A n d r o i d计算器代码编写<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android" android:padding="10px"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:id="@+id/label"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"/><EditTextandroid:id="@+id/factor1"android:layout_height="wrap_content"android:layout_width="fill_parent"android:layout_below="@id/label"android:layout_margin="5px"></EditText><Buttonandroid:id="@+id/dig1"android:layout_width="50px"android:layout_height="wrap_content"android:layout_below="@id/factor1"android:text="1"android:textSize="30px"/><Buttonandroid:id="@+id/dig2"android:layout_width="50px"android:layout_height="wrap_content"android:layout_below="@id/factor1"android:layout_toRightOf="@id/dig1"android:text="2"android:textSize="30px"/><Buttonandroid:id="@+id/dig3"android:layout_width="50px"android:layout_height="wrap_content"android:layout_below="@id/factor1"android:layout_toRightOf="@id/dig2" android:text="3"android:textSize="30px"/><Buttonandroid:id="@+id/dig4"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig1" android:text="4"android:textSize="30px"/><Buttonandroid:id="@+id/dig5"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig1" android:layout_toRightOf="@id/dig4" android:text="5"android:textSize="30px"/><Buttonandroid:id="@+id/dig6"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig1" android:layout_toRightOf="@id/dig5" android:text="6"android:textSize="30px"/><Buttonandroid:id="@+id/dig7"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig4" android:text="7"android:textSize="30px"/><Buttonandroid:id="@+id/dig8"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig4" android:layout_toRightOf="@id/dig7"android:text="8"android:textSize="30px"/><Buttonandroid:id="@+id/dig9"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig4" android:layout_toRightOf="@id/dig8" android:text="9"android:textSize="30px"/><Buttonandroid:id="@+id/dig0"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/dig4" android:layout_toRightOf="@id/dig9" android:text="0"android:textSize="30px"/><Buttonandroid:id="@+id/add"android:layout_width="50px"android:layout_height="wrap_content" android:layout_below="@id/factor1" android:layout_toRightOf="@id/dig3" android:textSize="30px"android:text="@string/add"android:gravity="center"/><Buttonandroid:id="@+id/sub"android:layout_width="50px"android:layout_height="wrap_content" android:layout_toRightOf="@id/add" android:layout_alignTop="@id/add" android:textSize="30px"android:gravity="center"android:text="@string/sub"/><Buttonandroid:id="@+id/mult"android:layout_width="50px"android:layout_height="wrap_content" android:layout_toRightOf="@id/dig6" android:layout_alignTop="@id/dig6" android:textSize="30px"android:gravity="center"android:text="@string/mult"/><Buttonandroid:id="@+id/divid"android:layout_width="50px"android:layout_height="wrap_content" android:layout_toRightOf="@id/mult" android:layout_alignTop="@id/mult" android:textSize="30px"android:gravity="center"android:text="@string/divid"/><Buttonandroid:id="@+id/clear"android:layout_width="150px" android:layout_height="wrap_content" android:layout_toRightOf="@id/sub" android:layout_alignTop="@id/sub" android:gravity="center"android:textSize="30px"android:text="C"/><Buttonandroid:id="@+id/dim"android:layout_width="50px"android:layout_height="wrap_content" android:layout_toRightOf="@id/dig0" android:layout_alignTop="@id/dig0" android:gravity="center"android:textSize="30px"android:text="."/><Buttonandroid:id="@+id/sqr"android:layout_width="150px" android:layout_height="wrap_content" android:layout_toRightOf="@id/divid" android:layout_alignTop="@id/divid"android:gravity="center"android:textSize="30px"android:text="sqr"/><Buttonandroid:id="@+id/caculate"android:layout_height="wrap_content"android:layout_width="150px"android:layout_toRightOf="@id/dim"android:layout_alignTop="@id/dim"android:gravity="center"android:textSize="30px"android:text="="/></RelativeLayout>import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class Activity01 extends Activity implements OnClickListener {/** Called when the activity is first created. */private Button add,sub,mult,divid,caculate,clear;private Button dig0,dig1,dig2,dig3,dig4,dig5,dig6,dig7,dig8,dig9,dim,sqr; private EditText factor1;private String factoronestr,factortwostr;double value=0;String symbol="a";String tem="";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);clear=(Button)findViewById(R.id.clear);add=(Button)findViewById(R.id.add);sub=(Button)findViewById(R.id.sub);mult=(Button)findViewById(R.id.mult);divid=(Button)findViewById(R.id.divid); caculate=(Button)findViewById(R.id.caculate); sqr=(Button)findViewById(R.id.sqr);dig1=(Button)findViewById(R.id.dig1);dig3=(Button)findViewById(R.id.dig3);dig2=(Button)findViewById(R.id.dig2);dig4=(Button)findViewById(R.id.dig4);dig5=(Button)findViewById(R.id.dig5);dig6=(Button)findViewById(R.id.dig6);dig7=(Button)findViewById(R.id.dig7);dig8=(Button)findViewById(R.id.dig8);dig9=(Button)findViewById(R.id.dig9);dig0=(Button)findViewById(R.id.dig0);dim=(Button)findViewById(R.id.dim);factor1=(EditText)findViewById(R.id.factor1); add.setOnClickListener(new addlistener()); sub.setOnClickListener(new sublistener()); mult.setOnClickListener(new multlistener()); divid.setOnClickListener(new dividlistener()); caculate.setOnClickListener(new listener()); clear.setOnClickListener(new clearlistener()); sqr.setOnClickListener(new sqrlistener());dig1.setOnClickListener(this);dig3.setOnClickListener(this);dig2.setOnClickListener(this);dig4.setOnClickListener(this);dig5.setOnClickListener(this);dig6.setOnClickListener(this);dig7.setOnClickListener(this);dig8.setOnClickListener(this);dig9.setOnClickListener(this);dig0.setOnClickListener(this);dim.setOnClickListener(this);}@Overridepublic void onClick(View v){if(v.equals(dig3)){tem=tem+"3";factor1.setText(tem);}if(v.equals(dig1)){tem=tem+"1";factor1.setText(tem);}if(v.equals(dig2)){tem=tem+"2";factor1.setText(tem);}if(v.equals(dig4)){tem=tem+"4";factor1.setText(tem);}if(v.equals(dig5)){tem=tem+"5";factor1.setText(tem);}if(v.equals(dig6)){tem=tem+"6";factor1.setText(tem);}if(v.equals(dig7)){tem=tem+"7";factor1.setText(tem);}if(v.equals(dig8)){tem=tem+"8";factor1.setText(tem);}if(v.equals(dig9)){tem=tem+"9";factor1.setText(tem);}if(v.equals(dig0)){tem=tem+"0";factor1.setText(tem);}if(v.equals(dim)){tem=tem+".";factor1.setText(tem);}}class addlistener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfactoronestr=factor1.getText().toString();if(factoronestr.equals("")){}elseif(symbol=="a"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=factoronedouble;factor1.setText("");}elseif(symbol=="*"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value*factoronedouble;factor1.setText("");}elseif(symbol=="-"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value-factoronedouble;factor1.setText("");}elseif(symbol=="/"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value/factoronedouble;factor1.setText("");}else{factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=factoronedouble+value;factor1.setText("");}symbol="+";tem="";}}class sublistener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfactoronestr=factor1.getText().toString();if(factoronestr.equals("")){}elseif(symbol=="a"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=factoronedouble;factor1.setText("");}elseif(symbol=="*"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value*factoronedouble;factor1.setText("");}elseif(symbol=="+"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value+factoronedouble;factor1.setText("");}elseif(symbol=="/"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value/factoronedouble;factor1.setText("");}else{factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value-factoronedouble;factor1.setText("");}symbol="-";tem="";}}class multlistener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfactoronestr=factor1.getText().toString();if(factoronestr.equals("")){}elseif(symbol=="a"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=factoronedouble;factor1.setText("");}elseif(symbol=="+"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value+factoronedouble;factor1.setText("");}elseif(symbol=="-"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value-factoronedouble;factor1.setText("");}elseif(symbol=="/"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value/factoronedouble;factor1.setText("");}else{factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=factoronedouble*value;factor1.setText("");}symbol="*";tem="";}}class dividlistener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfactoronestr=factor1.getText().toString();if(factoronestr.equals("")){}elseif(symbol=="a"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=factoronedouble;factor1.setText("");}elseif(symbol=="*"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value*factoronedouble;factor1.setText("");}elseif(symbol=="-"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value-factoronedouble;factor1.setText("");}elseif(symbol=="+"){factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value+factoronedouble;factor1.setText("");}else{factoronestr=factor1.getText().toString();double factoronedouble=Double.parseDouble(factoronestr); value=value/factoronedouble;factor1.setText("");}symbol="/";tem="";}}class clearlistener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfactor1.setText(null);symbol="a";tem="";}}class listener implements OnClickListener{@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfactortwostr=factor1.getText().toString();double factortwodouble=Double.parseDouble(factortwostr); if(symbol=="+"){value=value+factortwodouble;factor1.setText(value+"");value=0;symbol="a";}if(symbol=="-"){value=value-factortwodouble;factor1.setText(value+"");value=0;symbol="a";}if(symbol=="*"){value=value*factortwodouble;factor1.setText(value+"");value=0;symbol="a";}if(symbol=="/"){value=value/factortwodouble;if(value%1==0){factor1.setText((int)value+""); }else{factor1.setText(value+"");}value=0;symbol="a";}tem="";}}class sqrlistener implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfactoronestr=factor1.getText().toString();factoronestr=factor1.getText().toString();if(factoronestr.equals("")){}else{double factoronedouble=Double.parseDouble(factoronestr); value=StrictMath.pow(factoronedouble, 0.5);factor1.setText(""+value);value=0;symbol="a";}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// TODO Auto-generated method stubmenu.add(0, 1, 1, "exit");menu.add(0,2,2,"about");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// TODO Auto-generated method stubif(item.getItemId()==1){finish();}if(item.getItemId()==2){Intent intent=new Intent();intent.setClass(Activity01.this, AboutActivity.class);Activity01.this.startActivity(intent);}return super.onOptionsItemSelected(item); }}。
分享45个android实例源码
分享45个android实例源码分享45个android实例源码,很好很强大/android-20978-1-1.htmlandriod闹钟源代码/android-20974-1-1.htmlandroid源码分享之指南针程序/android-20973-1-1.html重力感应的测试程序andriod源代码/android-20972-1-1.htmlandroid源码分享之时光日志个人日程管理/android-20969-1-1.htmlOpenGL的一个简单的例子/android-20968-1-1.html文件管理器-android源代码/android-20960-1-1.htmlAndroid Txt文本阅读器源码/android-20959-1-1.htmlAndroid远程登录含有loading登录效果~~完整代码和超级详细注释/android-20958-1-1.htmlAndroid 手电筒源码/android-20957-1-1.htmlAndroid操作数据库实例/android-20954-1-1.htmlandroid 画图程序/android-20953-1-1.htmlAndroid 天气预报加widget源码/android-20950-1-1.htmlAndroid 指南针程序/android-20949-1-1.htmlAndroid 个人记账程序源码/android-20940-1-1.htmlAndroid游戏的心跳效果/android-20939-1-1.htmlAndroid PDF 阅读器源码/android-20858-1-1.htmlAndroid SqliteManager 源码/android-20857-1-1.htmlandroid 多点触控实例源码/android-20856-1-1.htmlAndroid 条码扫描程序源码/android-20855-1-1.htmlEditText插入QQ表情源码/android-20854-1-1.htmlAsyncTask进度条加载网站数据到ListView /android-20834-1-1.htmlandroid连接SQLite数据库-----增加改查+分页/android-20833-1-1.htmlAndroid 一个批量删除联系人的Demo/android-20832-1-1.htmlTXT 文本阅读器源码(android源码分享)/android-20827-1-1.htmlandroid 查询工具源代码/android-20824-1-1.htmlandroid进度条对话框Demo/android-20823-1-1.htmlAndroid实现渐显按钮的左右滑动效果/android-20752-1-1.htmlandroid天气预报源码/android-20977-1-1.htmlAndroid 文件浏览器源码/android-20976-1-1.htmlandroid源码分享之私密通讯录源码/android-20975-1-1.htmlAndroid自定义泡泡效果源码/android-20956-1-1.htmlandroid 获取Gps信息的程序源码/android-20955-1-1.htmlandroid 超炫的图片浏览器/android-20952-1-1.htmlandroid 加载时闪烁点样式的启动画面/android-20951-1-1.html实现基于Android的英文电子词典/android-20948-1-1.html基于Android 的英文电子词典/android-20947-1-1.htmlandroid 源码之英语单词记忆程序源码/android-20936-1-1.htmlandorid 源码北京公交线路查询(离线)/android-20938-1-1.htmlAndroid 计算器源码/android-20935-1-1.html带文字的ProgressBar Demo源码/android-20831-1-1.htmlandroid自定义时钟(三种方法实现,秒针效果,详细注解)/android-20830-1-1.htmlAndroid 秒表源码分享/android-20829-1-1.htmlAndroid源代码定时情景模式切换/android-20828-1-1.htmlandroid 公交查询/android-20826-1-1.htmlandroid源码分享之带手势划动功能的日历源码/android-20825-1-1.html。
深入理解Android5源代码
深⼊理解Android5源代码深⼊理解Android 5 源代码1 Android系统介绍1.1 Android系统成功的秘诀1.1.1 获取了业界的⼴泛⽀持1.1.2 研发阵容强⼤1.1.3 为开发⼈员“精⼼定制”1.1.4 开源1.2 剖析Android系统架构1.2.1 底层操作系统层(OS)1.2.2 各种库(Libraries)和Android运⾏环境(RunTime)1.2.3 ApplicationFramework(应⽤程序框架)1.2.4 顶层应⽤程序(Application)1.3 五⼤组件1.3.1 Activity界⾯1.3.2 Intent和IntentFilters切换1.3.3 Service(服务)1.3.4 BroadcastReceiver发送⼴播1.3.5 ⽤ContentProvider存储数据1.4 进程和线程1.4.1 什么是进程1.4.2 什么是线程2 获取并编译Android源代码2.1 获取Android源代码2.1.1 在Linux系统中获取Android源代码2.1.2 在Windows平台获取Android源代码2.2 分析Android源代码结构2.2.1 总体结构2.2.2 应⽤程序部分2.2.3 应⽤程序框架部分2.2.4 系统服务部分2.2.5 系统程序库部分2.2.6 硬件抽象层部分2.3 Android源代码提供的接⼝2.3.1 暴露接⼝和隐藏接⼝2.3.2 调⽤隐藏接⼝2.4 编译源代码2.4.1 搭建编译环境2.4.2 在模拟器中运⾏2.5 编译源代码⽣成SDK3 分析Java Native Interface系统3.1 JNI基础3.1.1 JNI的功能结构3.1.2 JNI的调⽤层次3.1.3 分析JNI的本质3.2 分析MediaScanner3.2.1 分析Java层3.2.2 分析JNI层3.2.3 分析Native(本地)层3.3 分析Camera系统的JNI3.3.1 Java层预览接⼝3.3.2 注册预览的JNI函数3.3.3 C/C++层的预览函数4 分析HAL系统4.1 HAL基础4.1.1 推出HAL的背景4.1.2 HAL的基本结构4.2 分析HAL module架构4.2.1 hw_module_t4.2.2 结构hw_module_methods_t的定义4.2.3 hw_device_t结构4.3 分析⽂件hardware.c4.3.1 寻找动态链接库的地址4.3.2 数组variant_keys4.3.3 载⼊相应的库4.3.4 获得hw_module_t结构体4.4 分析硬件抽象层的加载过程4.5 分析硬件访问服务4.5.1 定义硬件访问服务接⼝4.5.2 具体实现4.6 分析Android官⽅实例4.6.1 获取实例⼯程源代码4.6.2 直接调⽤Service⽅法的实现代码4.6.3 通过Manager调⽤Service的实现代码4.7 HAL和系统移植4.7.1 移植各个Android部件的⽅式4.7.2 设置设备权限4.7.3 init.rc初始化4.7.4 ⽂件系统的属性5 分析IPC通信机制5.1 Binder机制概述5.2 分析Binder驱动程序5.2.1 分析数据结构5.2.2 分析设备初始化5.2.3 打开Binder设备⽂件5.2.4 内存映射5.2.5 释放物理页⾯5.2.6 分配内核缓冲区5.2.7 释放内核缓冲区5.2.8 查询内核缓冲区5.3 Binder封装库5.3.1 类BBinder5.3.2 类BpRefBase5.3.3 类IPCThreadState5.4 初始化Java层Binder框架5.5 分析MediaServer的通信机制5.5.1 MediaServer的⼊⼝函数5.5.2 ProcessState5.5.3 defaultServiceManager5.5.4 注册MediaPlayerService5.5.5 分析StartThread Pool和join Thread Pool6 分析Binder对象和Java接⼝6.1 分析实体对象(binder_node)6.2 分析本地对象(BBinder)6.3 分析引⽤对象(binder_ref)6.4 分析代理对象(BpBinder)6.5 分析Java接⼝6.5.1 获取Service Manager6.5.2 分析ActivityManagerService的Java层7 分析ServiceManager和MessageQueue 7.1 分析ServiceManager7.1.1 分析主⼊⼝函数7.1.2 打开Binder设备⽂件7.1.3 注册处理7.1.4 创建Binder实体对象7.1.5 尽职的循环7.1.6 将信息注册到ServiceManager7.1.7 分析MediaPlayerService和Client7.2 获得Service Manager接⼝7.3 分析MessageQueue7.3.1 创建MessageQueue7.3.2 提取消息7.3.3 分析函数nativePollOnce8 init进程和Zygote进程8.1 分析init进程8.1.1 分析⼊⼝函数8.1.2 分析配置⽂件8.1.3 分析Service8.1.4 解析on字段的内容8.1.5 init控制Service8.1.6 控制属性服务8.2 分析Zygote(孕育)进程8.2.1 Zygote基础8.2.2 分析Zygote的启动过程9 System进程和应⽤程序进程9.1 分析System进程9.1.1 启动System进程前的准备⼯作9.1.2 分析SystemServer9.1.3 分析EntropyService9.1.4 分析DropBoxManagerService9.1.5 分析DiskStatsService9.1.6 分析DeviceStorageManagerService(监测系统内存存储空间的状态)9.1.7 分析SamplingProfilerService9.2 分析应⽤程序进程9.2.1 创建应⽤程序9.2.2 启动线程池9.2.3 创建信息循环10 分析Activity组件10.1 Activity基础10.1.1 Activity状态10.1.2 剖析Activity中的主要函数10.2 分析Activity的启动源代码10.2.1 Launcher启动应⽤程序10.2.2 返回ActivityManagerService的远程接⼝10.2.3 解析intent的内容10.2.4 分析检查机制10.2.5 执⾏Activity组件的操作10.2.6 将Launcher推⼊Paused状态10.2.7 处理消息10.2.8 暂停完毕10.2.9 建⽴双向连接10.2.10 启动新的Activity10.2.11 通知机制10.2.12 发送消息11 应⽤程序管理服务——PackageManagerService分析11.1 PackageManagerService概述11.2 系统进程启动11.3 开始运⾏11.4 扫描APK⽂件11.5 解析并安装⽂件11.6 启动系统默认Home应⽤程序Launcher11.6.1 设置系统进程11.6.2 启动Home应⽤程序11.6.3 启动uncher11.6.4 加载应⽤程序11.6.5 获得Activity12 Content Provider存储机制12.1 Content Provider基础12.1.1 ContentProvider在应⽤程序中的架构12.1.2 ContentProvider的常⽤接⼝12.2 启动Content Provider12.2.1 获得对象接⼝12.2.2 存在校验12.2.3 启动Android应⽤程序12.2.4 根据进程启动Content Provider 12.2.5 处理消息12.2.6 具体启动12.3 Content Provider数据共享12.3.1 获取接⼝12.3.2 创建CursorWindow对象12.3.3 数据传递12.3.4 处理进程通信的请求12.3.5 数据操作13 分析⼴播机制源代码13.1 Broadcast基础13.2 发送⼴播信息13.2.1 intent描述指⽰13.2.2 传递⼴播信息13.2.3 封装传递13.2.4 处理发送请求13.2.5 查找⼴播接收者13.2.6 处理⼴播信息13.2.7 检查权限13.2.8 处理的进程通信请求13.3 分析BroadCastReceiver13.3.1 MainActivity的调⽤13.3.2 注册⼴播接收者13.3.3 获取接⼝对象13.3.4 处理进程间的通信请求14 分析电源管理系统14.1 Power Management架构基础14.2 分析Framework层14.2.1 ⽂件PowerManager.java14.2.2 提供PowerManager功能14.3 JNI层架构分析14.3.1 定义了两层之间的接⼝函数14.3.2 与Linux Kernel层进⾏交互14.4 Kernel(内核)层架构分析14.4.1 ⽂件power.c14.4.2 ⽂件earlysuspend.c14.4.3 ⽂件wakelock.c14.4.4 ⽂件resume.c14.4.5 ⽂件suspend.c14.4.6 ⽂件main.c14.4.7 proc⽂件14.5 wakelock和early_suspend14.5.1 wakelock的原理14.5.2 early_suspend的原理14.5.3 Android休眠14.5.4 Android唤醒14.6 Battery电池系统架构和管理14.6.1 实现驱动程序14.6.2 实现JNI本地代码14.6.3 Java层代码14.6.4 实现Uevent部分14.7 JobScheduler节能调度机制14.7.1 JobScheduler机制的推出背景14.7.2 JobScheduler的实现14.7.3 实现操作调度14.7.4 封装调度任务15 分析WindowManagerService系统15.1 WindowManagerService基础15.2 计算Activity窗⼝的⼤⼩15.2.1 实现View遍历15.2.2 函数relayoutWindow15.2.3 函数relayoutWindow15.2.4 拦截消息的处理类15.2.5 判断是否计算过16 分析电话系统16.1 Android电话系统详解16.1.1 电话系统简介16.1.2 电话系统结构16.1.3 驱动程序介绍16.1.4 RIL接⼝16.1.5 分析电话系统的实现流程16.2 电话系统中的⾳频模块16.2.1 ⾳频系统结构16.2.2 分析⾳频系统的层次16.3 分析拨号流程16.3.1 拨号界⾯16.3.2 实现Phone应⽤16.3.3 Call通话控制16.3.4 静态⽅法调⽤16.3.5 通话管理16.3.6 dial拨号16.3.7 状态跟踪16.3.8 RIL消息“出/⼊”⼝16.3.9 显⽰通话主界⾯17 分析短信系统17.1 短信系统的主界⾯17.2 发送普通短信17.3 发送彩信17.4 接收短信17.4.1 Java应⽤层的接收流程17.4.2 Framework层的处理过程18 Sensor传感器系统详解18.1 Android传感器系统概述18.2 Java层详解18.3 Frameworks层详解18.3.1 监听传感器的变化18.3.2 注册监听18.4 JNI层详解18.4.1 实现Native(本地)函数18.4.2 处理客户端数据18.4.3 处理服务端数据18.4.4 封装HAL层的代码18.4.5 处理消息队列18.5 HAL层详解19 分析SEAndroid系统19.1 SEAndroid概述19.1.1 内核空间19.1.2 ⽤户空间19.2 ⽂件安全上下⽂19.2.1 设置打包在ROM⾥⾯的⽂件的安全上下⽂19.2.2 设置虚拟⽂件系统的安全上下⽂19.2.3 设置应⽤程序数据⽂件的安全上下⽂19.3 进程安全上下⽂19.3.1 为独⽴进程静态地设置安全上下⽂19.3.2 为应⽤程序进程设置安全上下⽂20 分析ART系统20.1 对⽐Dalvik VM和ART20.2 启动ART20.2.1 运⾏app_process进程20.2.2 准备启动20.2.3 创建运⾏实例20.2.4 注册本地JNI函数20.2.5 启动守护进程20.2.6 解析参数20.2.7 初始化类、⽅法和域20.3 分析主函数main20.4 查找⽬标类20.4.1 函数LookupClass()20.4.2 函数DefineClass()20.4.3 函数InsertClass()20.4.4 函数LinkClass()20.5 类操作20.6 实现托管操作20.7 加载OAT⽂件20.7.1 产⽣OAT20.7.2 创建ART虚拟机20.7.3 解析启动参数并创建堆20.7.4 ⽣成指定⽬录⽂件20.7.5 加载OAT⽂件20.7.6 解析字段思维导图防⽌博客图床图⽚失效,防⽌图⽚源站外链:思维导图在线编辑链接:。
简易计算器Android版(源码)
简易计算器Android版(源码)说明:每完成一次计算后,需先clear 清除记忆,当然也可以连算XML布局<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TableLayoutandroid:id="@+id/tableLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:collapseColumns="4"><TableRowandroid:id="@+id/tableRow_et"android:layout_width="fill_parent"android:layout_height="fill_parent"><EditTextandroid:id="@+id/et"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_span="4"android:focusable="false"android:singleLine="true"android:inputType="text"android:gravity="right"></EditText></TableRow><TableRowandroid:id="@+id/tableRow1"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_7"android:layout_width="40px"android:layout_height="40px"android:text="7"/><Buttonandroid:id="@+id/bt_8"android:layout_width="40px"android:layout_height="40px"android:text="8"/><Buttonandroid:id="@+id/bt_9"android:layout_width="40px"android:layout_height="40px"android:text="9"/><Buttonandroid:id="@+id/bt_back"android:layout_width="40px"android:layout_height="40px"android:text="back"/></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_4"android:layout_width="40px"android:layout_height="40px"android:text="4"/><Buttonandroid:id="@+id/bt_5"android:layout_width="40px"android:layout_height="40px"android:text="5"/><Buttonandroid:id="@+id/bt_6"android:layout_width="40px"android:layout_height="40px"android:text="6"/><Buttonandroid:id="@+id/bt_divide"android:layout_width="40px"android:layout_height="40px"android:text="/"/></TableRow><TableRowandroid:id="@+id/tableRow3"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_1"android:layout_width="40px"android:layout_height="40px"android:text="1"/><Buttonandroid:id="@+id/bt_2"android:layout_width="40px"android:layout_height="40px"android:text="2"/><Buttonandroid:id="@+id/bt_3"android:layout_width="40px"android:layout_height="40px"android:text="3"/><Buttonandroid:id="@+id/bt_multiply"android:layout_width="40px"android:layout_height="40px"android:text="*"/></TableRow><TableRowandroid:id="@+id/tableRow4"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_0"android:layout_width="50px"android:layout_height="40px"android:text="0"/><Buttonandroid:id="@+id/bt_point"android:layout_width="50px"android:layout_height="40px"android:text="."/><Buttonandroid:id="@+id/bt_add"android:layout_width="50px"android:layout_height="40px"android:text="+"/><Buttonandroid:id="@+id/bt_sub"android:layout_width="50px"android:layout_height="40px"android:text="-"/></TableRow><TableRowandroid:id="@+id/tableRow4"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_equal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_span="3"android:text="="/><Buttonandroid:id="@+id/bt_clear"android:layout_width="50px"android:layout_height="40px"android:text="clear"/></TableRow></TableLayout></LinearLayout>源代码:package android.sdk;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class Android_calculatorActivity extends Activity {private Button bt_1;private Button bt_2;private Button bt_3;private Button bt_4;private Button bt_5;private Button bt_6;private Button bt_7;private Button bt_8;private Button bt_9;private Button bt_0;private Button bt_add;private Button bt_sub; // 减private Button bt_multiply; // 乘private Button bt_divide; // 除private Button bt_back;private Button bt_equal; // 等于private Button bt_point; // 点private Button bt_clear; // 清除private EditText et_play; // 显示private String str_oper = "+"; // 运算符private StringBuffer str_display = new StringBuffer();; // 显示private String str_result; // 结果显示private double num1;private double num2;private boolean flag = true; // 小数点个数开关控制;private boolean b_sub, b_mul, b_div; // 运算符开关控制@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(yout.main);bt_0 = (Button) findViewById(R.id.bt_0);bt_1 = (Button) findViewById(R.id.bt_1);bt_2 = (Button) findViewById(R.id.bt_2);bt_3 = (Button) findViewById(R.id.bt_3);bt_4 = (Button) findViewById(R.id.bt_4);bt_5 = (Button) findViewById(R.id.bt_5);bt_6 = (Button) findViewById(R.id.bt_6);bt_7 = (Button) findViewById(R.id.bt_7);bt_8 = (Button) findViewById(R.id.bt_8);bt_9 = (Button) findViewById(R.id.bt_9);bt_add = (Button) findViewById(R.id.bt_add);bt_sub = (Button) findViewById(R.id.bt_sub);bt_multiply = (Button) findViewById(R.id.bt_multiply);bt_divide = (Button) findViewById(R.id.bt_divide);bt_back = (Button) findViewById(R.id.bt_back);bt_equal = (Button) findViewById(R.id.bt_equal); bt_point = (Button) findViewById(R.id.bt_point); bt_clear = (Button) findViewById(R.id.bt_clear); et_play = (EditText) findViewById(R.id.et);et_play.setText("0.0");bt_0.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("0");et_play.setText(str_display.toString());}});bt_1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("1");et_play.setText(str_display.toString());}});bt_2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("2");et_play.setText(str_display.toString());}});bt_3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("3");et_play.setText(str_display.toString());}});bt_4.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("4");et_play.setText(str_display.toString());}});bt_5.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("5");et_play.setText(str_display.toString());}});bt_6.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("6");et_play.setText(str_display.toString());}});bt_7.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("7");et_play.setText(str_display.toString());}});bt_8.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("8");et_play.setText(str_display.toString());}});bt_9.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("9");et_play.setText(str_display.toString());}});bt_point.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (flag){str_display.append(".");flag = false;}}});bt_back.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (str_display.length() != 0){str_display.deleteCharAt(str_display.length() - 1);et_play.setText(str_display.toString());}}});bt_add.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "+";if (!(str_display.toString() == "")){num1 += Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));flag = true;}});bt_sub.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "-";if (!b_sub && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_sub = true;} else{if (!(str_display.toString() == "")){num1 -=Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_multiply.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "*";if (!b_mul && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_mul = true;} else{if (!(str_display.toString() == "")){num1 *=Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_divide.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "/";if (!b_div && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_div = true;} else{if (!(str_display.toString() == "")){if(Double.parseDouble(str_display.toString()) == 0){Toast.makeText(Android_calculatorActivity.this,"除数不能为0!",Toast.LENGTH_LONG).show();} else{num1 /=Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_clear.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "+";str_display = new StringBuffer("");str_result = null;num1 = 0;num2 = 0;flag = true;b_sub = false;b_mul = false;b_div = false;et_play.setText("0.0");}});bt_equal.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (str_oper.equals("+")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 + num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("-")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 - num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("*")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 * num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("/")){num2 = Double.parseDouble(str_display.toString());if (!(num2 == 0)){str_result = String.valueOf((num1 / num2));et_play.setText(str_result);} else{Toast.makeText(Android_calculatorActivity.this,"除数不能为0!", Toast.LENGTH_LONG).show();}str_display = new StringBuffer("");}}});}。
编译Android内核源码
编译Android内核源代码下载好Android内核源代码这里使用的是模拟器所以下载的是android-goldfish-2.6.29版本的打开Android内核源代码目录下的Makefile文件修改体系结构和交叉编译工具即ARCH 和CROSS_COMPILE为Android模拟器编译内核分为三个步骤第一步是将交叉编译工具所在的目录添加到环境变量PA TH中第二步是修改硬件配置文件goldfish_defconfig第三步时执行make命令编译好的Android内核镜像文件zImage位于Android内核源代码目录下arch/arm/boot子目录中运行Android模拟器命令emulator 带参数-kernel 以指定Android模拟器所使用的内核镜像文件使用adb命令或者打开模拟器手机的设置关于手机查看内核版本是否为自己编译的开发一个Android应用程序来验证系统的行为这里假设读者已经熟悉如何使用Android SDK来开发Android应用程序在linux下的Android应用程序工程一般包含有一个源代码目录src 一个资源目录res 一个配置文件AndroidManifes.xml和一个编译脚本文件Android.mk这里吐槽一下AndroidManifest英文不要拼错我写错成了AndroidMainfest找半天才找到原因单独编译和打包Android应用程序模块在使用mmm命令之前要在Android源代码目录下执行以下source ./build/envsetup.sh脚本文件mmm命令才可以被使用命令mmm的用法可以通过-help来查看单独编译Android应用程序模块USER@MACHINE:~/Android$ mmm ./packages/experimental/HelloAndroid重新打包Android系统镜像文件USER@MACHINE:~Android$ make snod执行emulator命令启动Android模拟器在应用程序启动器中可以看到刚刚应用程序的快捷图标点击软件正常运行OK修改架构和交叉编译链查看交叉编译工具所在目录make goldfish_defconfig内核编译成功emulator带参数启动 &表示在后台启动Android模拟器目的为了可以在同一终端下使用adb命令命令emulator 带参数-kernel 以指定Android模拟器所使用的内核镜像文件验证是否为自己编译的内核Linux version 2.6.29 (devwang@devwang-desktop) (gcc version 4.4.3 (GCC) ) #2 Sun Jan 25 16:53:55 HKT 2015果然是的很高兴有木有在linux下的Android应用程序工程一般包含有一个源代码目录src 一个资源目录res 一个配置文件AndroidManifes.xml和一个编译脚本文件Android.mk遇到的问题AndroidManifest百度参考解决方法:/vblittleboy/article/details/21012991如果Makefile出现:make: *** No rule to make target ` ‘, needed by xxx. Stop.的错误,那么基本上都是属于找不到所依赖的文件所导致的,所以应该去:检测确保,所依赖的文件,是否真实存在。
简单计算器设计源代码
Button btn0,btn1,btn2,btn3,btn4,btn5;
Button btn6,btn7,btn8,btn9;
EditText et=null;
Button btnC,btnadd,btnsub,btnmul,btndiv,btnequal,btnpoint;
<Button
android:id="@+id/btn_seven_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="/"/>
</LinearLayout>
</LinearLayout>
源代码:
package com.example.num;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
android:text="-"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layoutid:orientation="horizontal" >
计算器安卓源代码
Java源代码package com.example.jisuanqi; import java.util.Date;import android.app.Activity;import android.os.Bundle;import android.view.View;importandroid.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText;public class MainActivity extends Activity {private Button one;private Button two;private Button four; private Button five; private Button six; private Button seven; private Button eight; private Button nine; private Button zero; private Button dot; private Button add; private Button sub; private Button mult; private Button division; private Button amount; private Button clear;private Button sqrt;private Button pow;private EditText led;String num1 = "";String num2 = "";double num3 = 0;String rezult = "";String sign = null;int mark = 0;boolean flag = true;boolean dotmark = true;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState) ;setContentView(yout.activity_m ain);zero = (Button) findViewById(R.id.zero);one = (Button) findViewById(R.id.one);two = (Button) findViewById(R.id.two);three = (Button) findViewById(R.id.three);four = (Button) findViewById(R.id.four);five = (Button) findViewById(R.id.five);six = (Button) findViewById(R.id.six);seven = (Button) findViewById(R.id.seven);eight = (Button) findViewById(R.id.eight);nine = (Button) findViewById(R.id.nine);dot = (Button) findViewById(R.id.dot);add = (Button)findViewById(R.id.add);sub = (Button) findViewById(R.id.sub);mult = (Button) findViewById(R.id.mult);division = (Button) findViewById(R.id.division);clear = (Button) findViewById(R.id.clear);amount = (Button) findViewById(R.id.amount);time = (Button) findViewById(R.id.time);pow = (Button) findViewById(R.id.pow);sqrt = (Button) findViewById(R.id.sqrt);led = (EditText) findViewById(R.id.led);zero.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "0";led.setText(num1);} else if (mark == 1) {num2 = num2 + "0";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "0";led.setText(num1 + sign + num2);}}});one.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "1";led.setText(num1);} else if (mark == 1) {num2 = num2 + "1";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "1";led.setText(num1 + sign + num2);}}});two.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "2";led.setText(num1);} else if (mark == 1) {num2 = num2 + "2";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "2";led.setText(num1 + sign + num2);}}});three.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "3";led.setText(num1);} else if (mark == 1) {num2 = num2 + "3";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "3";led.setText(num1 + sign + num2);}}});four.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "4";led.setText(num1);} else if (mark == 1) {num2 = num2 + "4";led.setText(num1 + sign +num2);} else {num1 = String.valueOf(num3);num2 = num2 + "4";led.setText(num1 + sign + num2);}}});five.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "5";led.setText(num1);} else if (mark == 1) {num2 = num2 + "5";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "5";led.setText(num1 + sign + num2);}}});six.setOnClickListener(newOnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "6";led.setText(num1);} else if (mark == 1) {num2 = num2 + "6";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "6";led.setText(num1 + sign + num2);}}});seven.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "7";led.setText(num1);} else if (mark == 1) {num2 = num2 + "7";led.setText(num1 + sign + num2);} else {num1 =String.valueOf(num3);num2 = num2 + "7";led.setText(num1 + sign + num2);}}});eight.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "8";led.setText(num1);} else if (mark == 1) {num2 = num2 + "8";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "8";led.setText(num1 + sign + num2);}}});nine.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0) {num1 = num1 + "9";led.setText(num1);} else if (mark == 1) {num2 = num2 + "9";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "9";led.setText(num1 + sign + num2);}}});add.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (flag) {sign = "+";mark++;flag = false;dotmark = true;}}});sub.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (flag) {sign = "-";mark++;flag = false;dotmark = true;}}});mult.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (flag) {sign = "*";mark++;flag = false;dotmark = true;}}});division.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (flag) {sign = "÷";mark++;flag = false;dotmark = true;}}});pow.setOnClickListener(newOnClickListener() {public void onClick(View v) {if (flag) {sign = "^";mark++;flag = false;dotmark = true;}}});dot.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (dotmark) {if (mark == 0) {num1 = num1 + ".";led.setText(num1);} else if (mark == 1) {num2 = num2 + ".";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + ".";led.setText(num1 + sign + num2);}dotmark = false;}}});amount.setOnClickListener(new OnClickListener() { // 等于时发生的运算public void onClick(View v) {flag = true;dotmark = true;if (sign.equals("+")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x + y;rezult =String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("-")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x - y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("*")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x * y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("÷")) {double x = Double.parseDouble(num1);double y =Double.parseDouble(num2);num3 = x / y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("^")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = Math.pow(x, y);rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";}}});clear.setOnClickListener(new OnClickListener() {public void onClick(View v) {num1 = "";num2 = "";num3 = 0;mark = 0;sign = "";led.setText("0");flag = true;dotmark = true;}});sqrt.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0 && !num1.equals("") && num2.equals("")) {double parent = Double.parseDouble(num1);double root = Math.sqrt(parent);rezult = String.valueOf(root);led.setText(rezult);num1 = "";}}});time.setOnClickListener(new OnClickListener() {public void onClick(View v) {Date date = new Date();led.setText(date.toLocaleString());}});}}Layout(布局)<LinearLayoutxmlns:android="http://schemas /apk/res/android"xmlns:tools="http://schemas.a /tools"android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:orientation="vertical "tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="fill_pa rent"android:layout_height="wrap_c ontent"android:layout_weight="2" android:orientation="horizont al"><EditTextandroid:id="@+id/led" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_margin="3dip" android:background="#ccc" android:gravity="top|right"android:text="" android:textColor="#000" android:textSize="12pt"/> </LinearLayout><LinearLayoutandroid:layout_width="fill_pa rent"android:layout_height="wrap_c ontent"android:layout_weight="1"android:orientation="horizont al"><Buttonandroid:id="@+id/time" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1" android:text="time"android:textSize="10pt"/><Buttonandroid:id="@+id/pow"android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="pow" android:textSize="10pt"/><Buttonandroid:id="@+id/sqrt" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1" android:text="sqrt"android:textSize="10pt"/><Buttonandroid:id="@+id/clear" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1" android:text="clear" android:textSize="10pt"/> </LinearLayout><LinearLayoutandroid:layout_width="fill_pa rent"android:layout_height="wrap_c ontent"android:layout_weight="1" android:orientation="horizont al"><Buttonandroid:id="@+id/seven" android:layout_width="fill_pa rent"android:layout_height="fill_parent"android:layout_weight="1"android:text="7" android:textSize="12pt"/><Buttonandroid:id="@+id/eight" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="8" android:textSize="12pt"/><Buttonandroid:id="@+id/nine" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="9" android:textSize="12pt"/><Buttonandroid:id="@+id/division" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="÷" android:textSize="12pt"/> </LinearLayout><LinearLayoutandroid:layout_width="fill_pa rent"android:layout_height="wrap_c ontent"android:layout_weight="1" android:orientation="horizont al"><Buttonandroid:id="@+id/four" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="4" android:textSize="12pt"/><Buttonandroid:id="@+id/five" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="5" android:textSize="12pt"/><Buttonandroid:id="@+id/six" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="6" android:textSize="12pt"/><Buttonandroid:id="@+id/mult" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="*" android:textSize="12pt"/> </LinearLayout><LinearLayoutandroid:layout_width="fill_pa rent"android:layout_height="wrap_c ontent"android:layout_weight="1" android:orientation="horizont al"><Buttonandroid:id="@+id/one" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="1" android:textSize="12pt"/><Buttonandroid:id="@+id/two" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="2" android:textSize="12pt"/><Buttonandroid:id="@+id/three" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="3" android:textSize="12pt"/><Buttonandroid:id="@+id/sub" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="-"android:textSize="12pt"/> </LinearLayout><LinearLayoutandroid:layout_width="fill_pa rent"android:layout_height="wrap_c ontent"android:layout_weight="1" android:orientation="horizont al"><Buttonandroid:id="@+id/dot" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="." android:textSize="12pt"/><Buttonandroid:id="@+id/zero" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="0" android:textSize="12pt"/><Buttonandroid:id="@+id/amount" android:layout_width="fill_pa rent"android:layout_height="fill_p arent"android:layout_weight="1"android:text="=" android:textSize="12pt"/><Buttonandroid:id="@+id/add" android:layout_width="fill_pa rent"。
从零开始学android实现计算器功能示例分享(计算器源码)
从零开始学android实现计算器功能⽰例分享(计算器源码)下⾯是效果展⽰:复制代码代码如下:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><TextViewandroid:id="@+id/tvResult"android:layout_width="fill_parent"android:layout_height="wrap_content"android:height="50dp"android:text="@string/tvResult"/></LinearLayout><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btnBackspace"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="150dp"android:layout_marginLeft="10dp"android:text="@string/btnbackspace"/><Buttonandroid:id="@+id/btnCE"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="150dp"android:text="@string/btnCE"/></LinearLayout><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:width="75dp"android:text="@string/btn7"/><Buttonandroid:id="@+id/btn8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btn8"/><Buttonandroid:id="@+id/btnDiv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btnDiv"/></LinearLayout><LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:width="75dp"android:text="@string/btn4"/><Buttonandroid:id="@+id/btn5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btn5"/><Buttonandroid:id="@+id/btn6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btn6"/><Buttonandroid:id="@+id/btnMul"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btnMul"/></LinearLayout><LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:width="75dp"android:text="@string/btn1"/><Buttonandroid:id="@+id/btn2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btn2"/><Buttonandroid:id="@+id/btn3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btn3"/></LinearLayout><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/btn0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:width="75dp"android:text="@string/btn0"/><Buttonandroid:id="@+id/btnC"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btnC"/><Buttonandroid:id="@+id/btnEqu"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btnEqu"/><Buttonandroid:id="@+id/btnSub"android:layout_width="wrap_content"android:layout_height="wrap_content"android:width="75dp"android:text="@string/btnSub"/></LinearLayout></LinearLayout>复制代码代码如下:package com.example.week2;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.app.Activity;public class MainActivity extends Activity implements OnClickListener{ //声明⼀些控件Button btn0=null;Button btn1=null;Button btn2=null;Button btn3=null;Button btn4=null;Button btn5=null;Button btn6=null;Button btn7=null;Button btn8=null;Button btn9=null;Button btnBackspace=null;Button btnCE=null;Button btnMul=null;Button btnDiv=null;Button btnEqu=null;TextView tvResult=null;//声明两个参数。
android计算器源码
这是我本人做的Android 简单计算器源代码。
可以实现普通四则运算2,10,16进制转换。
常见异常处理,可以实现activity屏幕跳转。
算法用逆波兰实现。
本项目所有代码不涉及任何知识产权问题,完全由自己学习android时候写的,所以各位可以放心下载。
AndroidManifest.xml代码<?xml version="1.0"encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="my.calu"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/meinv"android:label="@string/app_name"><activity android:name=".Main"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN"/><categoryandroid:name="UNCHER"/></intent-filter></activity></application><uses-sdk android:minSdkVersion="4"/></manifest>//layout 代码<?xml version="1.0"encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/background"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="/apk/res/android"><Buttonandroid:id="@+id/binary_sign"android:layout_width="73px"android:layout_height="52px"android:text="10/2"android:layout_x="14dp"android:layout_y="148dp"/><Buttonandroid:id="@+id/sexadecimal_sign"android:layout_width="73px"android:layout_height="52px"android:text="10/16"android:layout_x="88dp"android:layout_y="148dp"/><Buttonandroid:id="@+id/add_sign"android:layout_width="73px"android:layout_height="52px"android:text="+"android:layout_x="240dp"android:layout_y="382dp"/><EditTextandroid:id="@+id/editview"android:layout_width="295px"android:layout_height="76px"android:text=""android:textSize="18sp"android:layout_x="8dp"android:layout_y="14dp"/><Buttonandroid:id="@+id/number0"android:layout_width="73px"android:layout_height="52px"android:text="0"android:layout_x="89dp"android:layout_y="382dp"/> <Buttonandroid:id="@+id/point_sign"android:layout_width="73px"android:layout_height="52px"android:text="."android:layout_x="10dp"android:layout_y="382dp"/> <Buttonandroid:id="@+id/sub_sign"android:layout_width="73px"android:layout_height="52px"android:text="-"android:layout_x="240dp"android:layout_y="322dp"/> <Buttonandroid:id="@+id/mult_sign"android:layout_width="73px"android:layout_height="52px"android:text="*"android:layout_x="240dp"android:layout_y="262dp"/> <Buttonandroid:id="@+id/dev_sign"android:layout_width="73px"android:layout_height="52px"android:text="÷"android:layout_x="240dp"android:layout_y="202dp"/> <Buttonandroid:id="@+id/number3"android:layout_width="73px"android:layout_height="52px"android:text="3"android:layout_x="170dp"android:layout_y="322dp"/> <Buttonandroid:id="@+id/number6"android:layout_width="73px"android:layout_height="52px"android:text="6"android:layout_x="170dp"android:layout_y="262dp"/> <Buttonandroid:id="@+id/equal_sign"android:layout_width="73px"android:layout_height="52px"android:text="="android:layout_x="167dp"android:layout_y="382dp"/> <Buttonandroid:id="@+id/number1"android:layout_width="73px"android:layout_height="52px"android:text="1"android:layout_x="10dp"android:layout_y="322dp"/> <Buttonandroid:id="@+id/number5"android:layout_width="73px"android:layout_height="52px"android:text="5"android:layout_x="90dp"android:layout_y="262dp"/> <Buttonandroid:id="@+id/number4"android:layout_width="73px"android:layout_height="52px"android:text="4"android:layout_x="10dp"android:layout_y="262dp"/> <Buttonandroid:id="@+id/number9"android:layout_width="73px"android:layout_height="52px"android:text="9"android:layout_x="170dp"android:layout_y="202dp"/><Buttonandroid:id="@+id/number8"android:layout_width="73px"android:layout_height="52px"android:text="8"android:layout_x="90dp"android:layout_y="202dp"/><Buttonandroid:id="@+id/number7"android:layout_width="73px"android:layout_height="52px"android:text="7"android:layout_x="10dp"android:layout_y="202dp"/><Buttonandroid:id="@+id/number2"android:layout_width="73px"android:layout_height="52px"android:text="2"android:layout_x="90dp"android:layout_y="322dp"/><Buttonandroid:id="@+id/clear_sign"android:layout_width="138px"android:layout_height="wrap_content"android:text="C"android:layout_x="174dp"android:layout_y="148dp"/></AbsoluteLayout>主代码package my.calu;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;//package expression;import java.util.ArrayList;import java.util.Stack;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main extends Activity implements OnClickListener {private static final char Str = 0;private Buttonnumber0,number1,number2,number3,number4,number5,number6,number7,numbe r8,number9;private Buttonbinary_sign,sexadecimal_sign,clear_sign,point_sign,add_sign,sub_sign, mult_sign,dev_sign,equal_sign;private EditText edittext;private String buffer="";private boolean first_is_number=false ,after_equal=false,flage_10zhijing=true,flage_16zhijing=true;//初始化事件public void init() {this.edittext=(EditText) findViewById(R.id.editview);this.edittext.setOnClickListener(this);this.number0=(Button) findViewById(R.id.number0);this.number0.setOnClickListener(this);this.number1=(Button) findViewById(R.id.number1);this.number1.setOnClickListener(this);this.number2=(Button) findViewById(R.id.number2);this.number2.setOnClickListener(this);this.number3=(Button) findViewById(R.id.number2);this.number0.setOnClickListener(this);this.number3=(Button) findViewById(R.id.number3);this.number3.setOnClickListener(this);this.number4=(Button) findViewById(R.id.number4);this.number4.setOnClickListener(this);this.number5=(Button) findViewById(R.id.number5);this.number5.setOnClickListener(this);this.number6=(Button) findViewById(R.id.number6);this.number6.setOnClickListener(this);this.number7=(Button) findViewById(R.id.number7);this.number7.setOnClickListener(this);this.number8=(Button) findViewById(R.id.number8);this.number8.setOnClickListener(this);this.number9=(Button) findViewById(R.id.number9);this.number9.setOnClickListener(this);this.point_sign=(Button) findViewById(R.id.point_sign);this.point_sign.setOnClickListener(this);this.equal_sign=(Button) findViewById(R.id.equal_sign);this.equal_sign.setOnClickListener(this);this.add_sign=(Button) findViewById(R.id.add_sign);this.add_sign.setOnClickListener(this);this.sub_sign=(Button) findViewById(R.id.sub_sign);this.sub_sign.setOnClickListener(this);this.mult_sign=(Button) findViewById(R.id.mult_sign);this.mult_sign.setOnClickListener(this);this.dev_sign=(Button) findViewById(R.id.dev_sign);this.dev_sign.setOnClickListener(this);this.clear_sign=(Button) findViewById(R.id.clear_sign);this.clear_sign.setOnClickListener(this);this.binary_sign=(Button) findViewById(R.id.binary_sign);this.binary_sign.setOnClickListener(this);this.sexadecimal_sign=(Button)findViewById(R.id.sexadecimal_sign);this.sexadecimal_sign.setOnClickListener(this);}//判断字符串是否为全为0-9的整形数public boolean is10Numeric(String str){Pattern pattern = pile("[0-9]*");Matcher isNum = pattern.matcher(str);if( !isNum.matches() ){return false;}return true;}//判断字符串是否为全为0-1的整形数public boolean is2Numeric(String str){Pattern pattern = pile("[0-1]*");Matcher isNum = pattern.matcher(str);if( !isNum.matches() ){return false;}return true;}//判断字符串是否为全为0-f的整形数public boolean is16Numeric(String str){Pattern pattern = pile("[0-f]*");Matcher isNum = pattern.matcher(str);if( !isNum.matches() ){return false;}return true;}//判断最后一位和第一位是否为数字public boolean isnumber ( String s) {int i=s.length();char c=s.charAt(i-1);char k=s.charAt(0);// System.out.println("---firstnumber--"+k);// System.out.println("---70c=charAt--"+c);if(s.equals(null)) {return false;}if((c >= '0' && c <= '9')&& (k >='0' && k <= '9') ){return true;}elsereturn false;}//判断是否有运算符public boolean have_signal(String s){after_equal=false;boolean have=s.contains("+");if(s.contains("+")||s.contains("-") || s.contains("*") || s.contains("/"))return true;elsereturn false;}//-------堆栈算法---------public void deal_equal (String s){// System.out.println("Hello World!");//中缀 => 后缀表达式SSSssss// String s = "8-3*5"; //中缀String S = ""; //后缀char[] Operators = new char[s.length()];int Top = -1;for (int i = 0; i < s.length(); i++){char C = s.charAt(i);// System.out.print(C);switch(C){case' ' :b reak;case'+' : //操作符case'-' :while (Top >= 0) //栈不为空时{char c = Operators[Top--]; //pop Operatorif (c == '('){Operators[++Top] = c; //push Operatorbreak;}else{S = S + c;}}O perators[++Top] = C; //push OperatorS += " ";break;case'*' : //操作符case'/' :w hile (Top >= 0) //栈不为空时{char c = Operators[Top--]; //pop Operatorif (c == '('){Operators[++Top] = c; //push Operatorbreak;}else{if (c == '+' || c == '-'){Operators[++Top] = c; //push Operatorbreak;}else{S = S + c;}}}O perators[++Top] = C; //push OperatorS += " ";b reak;case'(' : //操作符Operators[++Top] = C;S += " ";break;case')' : //操作符while (Top >= 0) //栈不为空时{char c = Operators[Top--]; //pop Operatorif (c == '('){break;}else{S = S + c;}}S += " ";break;default : //操作数S = S + C;break;}}while (Top >= 0){S = S + Operators[Top--]; //pop Operator}// System.out.print(Operators);// System.out.println(S); //后缀//后缀表达式计算double[] Operands = new double[S.length()];// System.out.println(S.length());double x, y, v;Top = - 1;String Operand = "";for (int i = 0; i < S.length(); i++){char c = S.charAt(i);if ((c >= '0' && c <= '9') || c == '.'){Operand += c;// System.out.print(Operand+" ");}if ((c == ' ' && Operand != "") || i == S.length()) {Operands[++Top] = ng.Double.parseDouble(Operand) ; //push OperandsOperand = "";}if (c == '+' || c == '-' || c == '*' || c == '/'){if ((Operand != "")){Operands[++Top] = ng.Double.parseDouble(Operand) ;//push Operands// System.out.println(Operators);Operand = "";}y = Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响x = Operands[Top--]; //pop 双目运算符的第一操作数switch (c){case'+' :v = x + y;break;case'-' :v = x - y;break;case'*' :v = x * y;break;case'/' :v = x / y; // 第一操作数 / 第二操作数注意操作数顺序对除法的影响break;default :v = 0;break;}Operands[++Top] = v; //push 中间结果再次入栈}}v = Operands[Top--]; //pop 最终结果// System.out.println("-------result-------"+v);String result=v+"";this.edittext.setText(result);}/** Called when the activity is first created. *///------------------函数入口--wangwei-------------//@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);this.init();//跳转// Intent intent=new Intent();//// Intent intent=TwoActivity.this.getlntent(); // Bundle bundle=intent.getExtras();// String name=bundle.getString(buffer);//}//触发事件后的动作// @Overridepublic void onClick(View v) {try {// TODO Auto-generated method stubif(v.getId()==this.number0.getId()) {buffer=buffer+0;this.edittext.setText(buffer);// first_is_number=true;}else if(v.getId()==this.number1.getId()) { buffer=buffer+1;this.edittext.setText(buffer);// first_is_number=true;}else if(v.getId()==this.number2.getId()) { buffer=buffer+2;this.edittext.setText(buffer);}else if(v.getId()==this.number3.getId()) { buffer=buffer+3;this.edittext.setText(buffer);// first_is_number=true;}else if(v.getId()==this.number4.getId()) { buffer=buffer+4;this.edittext.setText(buffer);// first_is_number=true;}else if(v.getId()==this.number5.getId()) { buffer=buffer+5;this.edittext.setText(buffer);}else if(v.getId()==this.number6.getId()) {buffer=buffer+6;this.edittext.setText(buffer);// first_is_number=true;}else if(v.getId()==this.number7.getId()) {buffer=buffer+7;this.edittext.setText(buffer);// first_is_number=true;}else if(v.getId()==this.number8.getId()) {buffer=buffer+8;this.edittext.setText(buffer);first_is_number=true;}else if(v.getId()==this.number9.getId()) {buffer=buffer+9;this.edittext.setText(buffer);// first_is_number=true;//---------------2进制的转换-----------}else if(v.getId()==this.binary_sign.getId()) { boolean is10=is10Numeric(buffer);boolean is2=is2Numeric(buffer);if(is10&&(flage_10zhijing==true)){int num=Integer.parseInt(buffer);flage_10zhijing=false;buffer=Integer.toBinaryString(num)+"" ;System.out.println("is10"+flage_10zhijing);}else if (is2&&(flage_10zhijing==false)){flage_10zhijing=true;buffer=Integer.valueOf(buffer,2).toString()+"" ;}else {//System.out.println(" 不是10进制也不是二进制");//buffer="0000000000";}this.edittext.setText(buffer);//----------------16进制的转换--------------}else if(v.getId()==this.sexadecimal_sign.getId()) { boolean is16=false,is10=false;is16=is16Numeric(buffer);is10=is10Numeric(buffer);System.out.println("is16 --- "+is16);System.out.println("flage_16zhijing"+flage_16zhijing);if(is10&&flage_16zhijing){int num=Integer.parseInt(buffer);flage_16zhijing=false;buffer=Integer.toHexString(num) +"" ;System.out.println("jinruis10 "+flage_16zhijing);}else if ((is16==true)&&(flage_16zhijing==false)){System.out.println("jinruis16 "+flage_16zhijing);flage_16zhijing=true;buffer=Integer.valueOf(buffer,16).toString() +"" ;//buffer="0000000000";}else {//System.out.println(" 不是16进制也不是1 0进制");//buffer="0000000000";}this.edittext.setText(buffer);//-------------四则运算-------------}else if(v.getId()==this.point_sign.getId()) {boolean isnumber;isnumber= isnumber ( buffer);if(isnumber)buffer=buffer+".";this.edittext.setText(buffer);}else if(v.getId()==this.sub_sign.getId()) {boolean isnumber;isnumber= isnumber ( buffer);if(isnumber)buffer=buffer+"-";this.edittext.setText(buffer);}else if(v.getId()==this.mult_sign.getId()) {boolean isnumber;isnumber= isnumber ( buffer);if(isnumber)buffer=buffer+"*";this.edittext.setText(buffer);}else if(v.getId()==this.dev_sign.getId()) {boolean isnumber;isnumber= isnumber ( buffer);if(isnumber)buffer=buffer+"/";this.edittext.setText(buffer);}else if(v.getId()==this.add_sign.getId()) {boolean isnumber;isnumber= isnumber ( buffer);if(isnumber){buffer=buffer+"+";this.edittext.setText(buffer);}else {//buffer="000000000000";this.edittext.setText(buffer);}//-------------------清除-------------------}else if(v.getId()==this.clear_sign.getId()) {// if(after_equal) {// int len=buffer.length();// if(len >=1) {// buffer= (String )buffer.subSequence(0, len-1); // }//// } else {// buffer="";// }buffer="";this.edittext.setText(buffer);//--------------计算结果------------------------}else if(v.getId()==this.equal_sign.getId()) {boolean isnumber;if(buffer.equals(null)) {buffer=" ";this.edittext.setText(buffer);}else {isnumber= isnumber ( buffer);boolean have_signal=have_signal(buffer);if(isnumber&&have_signal){after_equal=true;deal_equal(buffer);}}}} catch (Exception e) {// TODO: handle exception}// System.out.println("-----327------"+buffer);}}。
android计算器简单实现代码
android计算器简单实现代码本⽂实例为⼤家分享了android计算器的具体实现代码,供⼤家参考,具体内容如下java代码:package com.itheima74.simplecalculator4;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.iflytek.cloud.InitListener;import com.iflytek.cloud.SpeechConstant;import com.iflytek.cloud.SpeechError;import com.iflytek.cloud.SpeechSynthesizer;import com.iflytek.cloud.SpeechUtility;import com.iflytek.cloud.SynthesizerListener;import com.iflytek.cloud.util.ResourceUtil;import java.util.ArrayList;/*** 简易计算器(第三版)* 功能介绍:* 1.⽀持连续计算* 2.⽀持离线语⾳* 3.科⼤讯飞在线(离线)语⾳合成SDK使⽤步骤:* a.下载SDK,注册APPID* b.拷贝libs⽬录下Msc.jar⾄⼯程libs⽬录,右键:add as library* c.创建assets⽬录,拷贝tts⽂件夹下3个离线发⾳⼈资源* d.创建jniLibs⽬录,拷贝armeabi⽂件夹,x86⽂件(视具体⼿机机型⽽定) * e.初始化SDK*/public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView tv;// 显⽰控件private ArrayList<String> mList;// 存储⽤户输⼊集合private double mResult;// 计算结果,当除数为0时,⽆法计算结果,默认为初始值0private boolean mFlag = true;// 控制在线语⾳功能是否开启的变量,默认开启@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.activity_main);initSDK();initView();initList();}/*** 2.初始化离线语⾳** @param flag 离线语⾳开启或关闭开关* @param msg 要朗读的⽂字*/private void initLocalHeCheng(boolean flag, String msg) {if (flag) {//1.创建SpeechSynthesizer对象,第⼆个参数:本地合成传InitListenerSpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, new InitListener() {@Overridepublic void onInit(int code) {}});//2.合成参数设置//设置引擎类型为本地mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);//设置本地发⾳⼈mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");//加载本地合成资源,resPath为本地合成资源路径mTts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());// 设置语速mTts.setParameter(SpeechConstant.SPEED, "60");// 设置⾳调mTts.setParameter(SpeechConstant.PITCH, "50");// 设置⾳量mTts.setParameter(SpeechConstant.VOLUME, "100");// 设置播放器⾳频流类型mTts.setParameter(SpeechConstant.STREAM_TYPE, "3");//设置合成⾳频保存位置(可⾃定义保存位置),保存在“./sdcard/iflytek.pcm”//保存在SD卡需要在AndroidManifest.xml添加写SD卡权限//如果不需要保存合成⾳频,注释该⾏代码//mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");//3.开始合成mTts.startSpeaking(msg, mSynListener);}}// 获取发⾳⼈资源路径private String getResourcePath() {StringBuffer tempBuffer = new StringBuffer();// 合成通⽤资源tempBuffer.append(ResourceUtil.generateResourcePath(this, ResourceUtil.RESOURCE_TYPE.assets, "tts/common.jet")); tempBuffer.append(";");// 发⾳⼈资源tempBuffer.append(ResourceUtil.generateResourcePath(this, ResourceUtil.RESOURCE_TYPE.assets, "tts/xiaoyan.jet")); return tempBuffer.toString();}/*** 1.初始化SDK*/private void initSDK() {SpeechUtility.createUtility(this, SpeechConstant.APPID + "=58ba4628");}// 创建⼀个选项菜单@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}// 处理选项菜单的点击事件@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case R.id.exit:this.finish();break;case R.id.open_local_voice:mFlag = true;mFlag = false;break;}return super.onOptionsItemSelected(item);}/*** 2.在线语⾳合成,朗读** @param flag 控制⽅法的执⾏,true:执⾏,false:不执⾏* @param msg 需要朗读的⽂字*//*private void initYuYinHeCheng(boolean flag, String msg) {if (flag) {//1.创建SpeechSynthesizer对象, 第⼆个参数:本地合成时传InitListenerSpeechSynthesizer mTts = SpeechSynthesizer.createSynthesizer(this, null);//2.合成参数设置,详见《科⼤讯飞MSC API⼿册(Android)》SpeechSynthesizer 类mTts.setParameter(SpeechConstant.VOICE_NAME, mVoiceName);//设置发⾳⼈mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速mTts.setParameter(SpeechConstant.VOLUME, "80");//设置⾳量,范围0~100mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端//设置合成⾳频保存位置(可⾃定义保存位置),保存在“./sdcard/iflytek.pcm”//保存在SD卡需要在AndroidManifest.xml添加写SD卡权限//如果不需要保存合成⾳频,注释该⾏代码//mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm");//3.开始合成mTts.startSpeaking(msg, mSynListener);}}*/// 合成监听器private SynthesizerListener mSynListener = new SynthesizerListener() {//开始播放@Overridepublic void onSpeakBegin() {}//缓冲进度回调//percent为缓冲进度0~100,beginPos为缓冲⾳频在⽂本中开始位置,endPos表⽰缓冲⾳频在⽂本中结束位置, //info为附加信息。
安卓开发的自制计算器源代码
这是本人自己用安卓编写的计算器,代码的优化可能有所问题,不过大家都在学习阶段,欢迎大家批评指正,还是上源码吧。
先来运行图package com.example.nanchen.exam_1_1;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{private Button btn0;private Button btn1;private Button btn2;private Button btn3;private Button btn4;private Button btn5;private Button btn6;private Button btn7;private Button btn8;private Button btn9;private Button btnAdd;private Button btnSub;private Button btnMul;private Button btnDiv;private Button btnDec;private Button btnEqual;private Button btnClear;private Button btnBackSpace;private TextView textView_result;double num1 = 0,num2 = 0,result = 0;//定义存储结果和两个操作数boolean isCheckEqu = false;//检测是否点击了等于int op = 0;//定义运算符boolean hasDec = false;boolean hasOp = false;@Overrideprotected void onCreate Bundle savedInstanceStatesuper.onCreate savedInstanceState;setContentView yout.activity_main;textView_result = TextView findViewById R.id.result;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;btnAdd = Button findViewById R.id.btn10;btnSub = Button findViewById R.id.btn11;btnMul = Button findViewById R.id.btn12;btnDiv = Button findViewById R.id.btn13;btnDec = Button findViewById R.id.btn14;btnEqual = Button findViewById R.id.btn15;btnClear = Button findViewById R.id.clear;btnBackSpace = Button findViewById R.id.backSpace;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;btnAdd.setOnClickListener this;btnSub.setOnClickListener this;btnDec.setOnClickListener this;btnDiv.setOnClickListener this;btnEqual.setOnClickListener this;btnMul.setOnClickListener this;btnClear.setOnClickListener this;btnBackSpace.setOnClickListener this;@Overridepublic void onClick View vswitch v.getIdcase R.id.btn0:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString0 =textView_result.getText.toString;if myString0.equals"0"myString0 = "";myString0 += "0";textView_result.setText myString0;break;case R.id.btn1:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString1 =textView_result.getText.toString;if myString1.equals"0"myString1 = "";myString1 += "1";textView_result.setText myString1;break;case R.id.btn2:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString2 =textView_result.getText.toString;if myString2.equals"0"myString2 = "";myString2 += "2";textView_result.setText myString2;break;case R.id.btn3:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString3 =textView_result.getText.toString;if myString3.equals"0"myString3 = "";myString3 += "3";textView_result.setText myString3;break;case R.id.btn4:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString4 =textView_result.getText.toString;if myString4.equals"0"myString4 = "";myString4 += "4";textView_result.setText myString4;break;case R.id.btn5:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString5 =textView_result.getText.toString;if myString5.equals"0"myString5 = "";myString5 += "5";textView_result.setText myString5;break;case R.id.btn6:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString6 =textView_result.getText.toString;if myString6.equals"0"myString6 = "";myString6 += "6";textView_result.setText myString6;break;case R.id.btn7:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString7 =textView_result.getText.toString;if myString7.equals"0"myString7 = "";myString7 += "7";textView_result.setText myString7;break;case R.id.btn8:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString8 =textView_result.getText.toString;if myString8.equals"0"myString8 = "";myString8 += "8";textView_result.setText myString8;break;case R.id.btn9:if isCheckEqutextView_result.setText null;isCheckEqu=false;String myString9 =textView_result.getText.toString;if myString9.equals"0"myString9 = "";myString9 += "9";textView_result.setText myString9;break;case R.id.btn10:if hasOpdoEqual;//显示出来当前的答案String myStringAdd =textView_result.getText.toString;if myStringAdd.equals nullreturn;num1 = Double.valueOf myStringAdd;textView_result.setText null;// 这里被清除了op = 1;isCheckEqu = false;hasDec = false;hasOp = true;break;case R.id.btn11:if hasOpdoEqual;String myStringSub =textView_result.getText.toString;if myStringSub.equals nullreturn;num1 = Double.valueOf myStringSub;textView_result.setText null;op = 2;isCheckEqu = false;hasDec = false;hasOp = true;break;case R.id.btn12:if hasOpdoEqual;String myStringMul =textView_result.getText.toString;if myStringMul.equals nullreturn;num1 = Double.valueOf myStringMul;textView_result.setText null;op = 3;isCheckEqu = false;hasDec = false;hasOp = true;break;case R.id.btn13:if hasOpdoEqual;String myStringDiv =textView_result.getText.toString;if myStringDiv.equals nullreturn;num1 = Double.valueOf myStringDiv;textView_result.setText null;op = 4;hasDec = false;isCheckEqu = false;hasOp = true;break;case R.id.btn14:if isCheckEqutextView_result.setText null;isCheckEqu=false;if hasDec//如果已经有小数点了,则弹出一个对话框AlertDialog.Builder builder = new AlertDialog.Builder MainActivity.this;builder.setTitle"警告";builder.setMessage"输入错误,一个数字不能有两个小数点!";builder.setNegativeButton"取消", new DialogInterface.OnClickListener@Overridepublic void onClick DialogInterface dialog, int which;builder.show;return;hasDec = true;String myString14 =textView_result.getText.toString;myString14 += ".";textView_result.setText myString14;break;case R.id.btn15:doEqual;hasOp = false;break;case R.id.backSpace:String myString =textView_result.getText.toString;trytextView_result.setText myString.substring0,myString.length -1;catch Exception etextView_result.setText"0";break;case R.id.clear:textView_result.setText"0";break;default:break;/*** 按下等于*/private void doEqualStringmyStringEqu=textView_result.getText.toString;if myStringEqu.equals nullreturn;num2=Double.valueOf myStringEqu;textView_result.setText null;switch opcase 0:result=num2;break;case 1:result=num1+num2;break;case 2:result=num1-num2;break;case 3:result=num1*num2;break;case 4:result=num1/num2;break;default:result=0;break;textView_result.setText String.valueOf result;isCheckEqu=true;<?xml version="1.0" encoding="utf-8"?>LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.example.nanchen.exam_1_1.MainActivity"<!--显示结果-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/result"android:gravity="end"android:textSize="40sp"android:text="@string/zero"LinearLayoutLinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:orientation="horizontal"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="@string/clear"android:id="@+id/clear"android:textAllCaps="false"android:gravity="center"android:textSize="25sp"Buttonandroid:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:text="@string/backspace"android:id="@+id/backSpace"android:textAllCaps="false"android:gravity="center"android:textSize="25sp" LinearLayout<!--789/-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/seven"android:id="@+id/btn7"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/eight"android:id="@+id/btn8"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/nine"android:id="@+id/btn9"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/del"android:id="@+id/btn13"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1" LinearLayout<!--456*-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/four"android:id="@+id/btn4"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/five"android:id="@+id/btn5"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/six"android:id="@+id/btn6"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/mul"android:id="@+id/btn12"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1" LinearLayout<!--123- -->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/one"android:textSize="30sp"android:id="@+id/btn1"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/two"android:id="@+id/btn2"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/three"android:id="@+id/btn3"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/sub"android:id="@+id/btn11"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1" LinearLayout<!--0.=+-->LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/zero1"android:id="@+id/btn0"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/dec"android:id="@+id/btn14"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/equal"android:id="@+id/btn15"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"Buttonandroid:layout_width="0dp"android:text="@string/add"android:id="@+id/btn10"android:textSize="30sp"android:gravity="center"android:layout_height="wrap_content"android:layout_weight="1"LinearLayoutLinearLayoutandroid:layout_weight="4"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="0dp"TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"android:text="@string/string1"TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/string2"android:textSize="20sp"LinearLayoutLinearLayout。
Android计算器源码
计算器源码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>。
计算器Android版(源码)
计算器Android版(源码)100%运行通过这是自己编写的,通过eclipse运行简易计算器Android版(源码)这是自己整理得,欢迎大家下载说明:每完成一次计算后,需先clear 清除记忆,当然也可以连算XML布局<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TableLayoutandroid:id="@+id/tableLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:collapseColumns="4"><TableRowandroid:id="@+id/tableRow_et"android:layout_width="fill_parent"android:layout_height="fill_parent"><EditTextandroid:id="@+id/et"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_span="4"android:focusable="false"android:singleLine="true"android:inputType="text"android:gravity="right"></EditText></TableRow><TableRowandroid:id="@+id/tableRow1"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_7"android:layout_width="40px"android:layout_height="40px"android:text="7"/><Buttonandroid:id="@+id/bt_8"android:layout_width="40px"android:layout_height="40px"android:text="8"/><Buttonandroid:id="@+id/bt_9"android:layout_width="40px"android:layout_height="40px"android:text="9"/><Buttonandroid:id="@+id/bt_back"android:layout_width="40px"android:layout_height="40px"android:text="back"/></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_4"android:layout_width="40px"android:layout_height="40px"android:text="4"/><Buttonandroid:id="@+id/bt_5"android:layout_width="40px"android:layout_height="40px"android:text="5"/><Buttonandroid:id="@+id/bt_6"android:layout_width="40px"android:layout_height="40px"android:text="6"/><Buttonandroid:id="@+id/bt_divide"android:layout_width="40px"android:layout_height="40px"android:text="/"/></TableRow><TableRowandroid:id="@+id/tableRow3"android:layout_width="fill_parent"android:layout_height="fill_parent">android:layout_width="50px"android:layout_height="40px"android:text="+"/><Buttonandroid:id="@+id/bt_sub"android:layout_width="50px"android:layout_height="40px"android:text="-"/></TableRow><TableRowandroid:id="@+id/tableRow4"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_equal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_span="3"android:text="="/><Buttonandroid:id="@+id/bt_clear"android:layout_width="50px"android:layout_height="40px"android:text="clear"/></TableRow></TableLayout></LinearLayout>源代码:package android.sdk;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class Android_calculatorActivity extends Activity{private Button bt_1;private Button bt_2;private Button bt_3;private Button bt_4;private Button bt_5;private Button bt_6;private Button bt_7;private Button bt_8;private Button bt_9;private Button bt_0;private Button bt_add;private Button bt_sub; // 减private Button bt_multiply; // 乘private Button bt_divide; // 除private Button bt_back;private Button bt_equal; // 等于private Button bt_point; // 点private Button bt_clear; // 清除private EditText et_play; // 显示private String str_oper = "+"; // 运算符private StringBuffer str_display = new StringBuffer();; // 显示private String str_result; // 结果显示private double num1;private double num2;private boolean flag = true; // 小数点个数开关控制;private boolean b_sub, b_mul, b_div; // 运算符开关控制@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(yout.main);bt_0 = (Button) findViewById(R.id.bt_0);bt_1 = (Button) findViewById(R.id.bt_1);bt_2 = (Button) findViewById(R.id.bt_2);bt_3 = (Button) findViewById(R.id.bt_3);bt_4 = (Button) findViewById(R.id.bt_4);bt_5 = (Button) findViewById(R.id.bt_5);bt_6 = (Button) findViewById(R.id.bt_6);bt_7 = (Button) findViewById(R.id.bt_7);bt_8 = (Button) findViewById(R.id.bt_8);bt_9 = (Button) findViewById(R.id.bt_9);bt_add = (Button) findViewById(R.id.bt_add);bt_sub = (Button) findViewById(R.id.bt_sub);bt_multiply = (Button) findViewById(R.id.bt_multiply); bt_divide = (Button) findViewById(R.id.bt_divide);bt_back = (Button) findViewById(R.id.bt_back);bt_equal = (Button) findViewById(R.id.bt_equal);bt_point = (Button) findViewById(R.id.bt_point);bt_clear = (Button) findViewById(R.id.bt_clear);et_play = (EditText) findViewById(R.id.et);et_play.setText("0.0");bt_0.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("0");et_play.setText(str_display.toString());}});bt_1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("1");et_play.setText(str_display.toString());}});bt_2.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("2");et_play.setText(str_display.toString());}});bt_3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("3");et_play.setText(str_display.toString());}});bt_4.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("4");et_play.setText(str_display.toString());}});bt_5.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("5");et_play.setText(str_display.toString());}});bt_6.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("6");et_play.setText(str_display.toString());}});bt_7.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("7");et_play.setText(str_display.toString());}});bt_8.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("8");et_play.setText(str_display.toString());}});bt_9.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("9");et_play.setText(str_display.toString());}});bt_point.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){if (flag){str_display.append(".");flag = false;}}});bt_back.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (str_display.length() != 0){str_display.deleteCharAt(str_display.length() - 1);et_play.setText(str_display.toString());}}});bt_add.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "+";if (!(str_display.toString() == "")){num1 += Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));flag = true;}});bt_sub.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "-";if (!b_sub && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_sub = true;} else{if (!(str_display.toString() == "")){num1 -=Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_multiply.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "*";if (!b_mul && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_mul = true;} else{if (!(str_display.toString() == "")){num1 *=Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_divide.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "/";if (!b_div && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_div = true;} else{if (!(str_display.toString() == "")){if(Double.parseDouble(str_display.toString()) == 0){Toast.makeText(Android_calculatorActivity.this,"除数不能为0!",Toast.LENGTH_LONG).show();} else{num1 /=Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_clear.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "+";str_display = new StringBuffer("");str_result = null;num1 = 0;num2 = 0;flag = true;b_sub = false;b_mul = false;b_div = false;et_play.setText("0.0");}});bt_equal.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (str_oper.equals("+")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 + num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("-")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 - num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("*")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 * num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("/")){num2 = Double.parseDouble(str_display.toString());if (!(num2 == 0)){str_result = String.valueOf((num1 / num2));et_play.setText(str_result);} else{Toast.makeText(Android_calculatorActivity.this,"除数不能为0!", Toast.LENGTH_LONG).show();}str_display = new StringBuffer("");}}});}。
Android计算器编写代码
Android计算器编写代码其实这个安卓计算机,所有的后台思想与《》是⼀模⼀样的。
Win窗体程序移植到安卓,从C#到Java其实很简单的,因为两者的基本语法都很相像,唯⼀的难点是安卓的xml布局部分,不像C#窗体能够直接拖。
还是如下图⼀个能够完成基本四则运算的计算器:先在res\values\strings.xml设置按钮相应的字体,以免布局⽂件警告满天飞:<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">计算器</string><string name="bt_1">1</string><string name="bt_2">2</string><string name="bt_3">3</string><string name="bt_4">4</string><string name="bt_5">5</string><string name="bt_6">6</string><string name="bt_7">7</string><string name="bt_8">8</string><string name="bt_9">9</string><string name="bt_0">0</string><string name="bt_point">.</string><string name="bt_ce">CE</string><string name="bt_plus">+</string><string name="bt_minus">-</string><string name="bt_multi">×</string><string name="bt_div">÷</string><string name="bt_result">=</string></resources>之后,布局部分采⽤了《【Android】关于百分⽐布局多个LinearLayout嵌套时出现的问题与解决⽅案》(点击打开链接)的思想,具体如下图,⼀个TextView、⼀个EditText,皆直接⽤match_parent占据整⾏的宽度,之后利⽤LinearLayout与TableLayout作横向⽐例的划分。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Mxl代码<?xml version="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><EditTextandroid:id="@+id/etext"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_margin="5px"></EditText><Buttonandroid:id="@+id/but7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/etext"android:text=" 7 "android:textSize="30px"/><Buttonandroid:id="@+id/but8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/etext"android:layout_toRightOf="@id/but7"android:text=" 8 "android:textSize="30px"/><Buttonandroid:id="@+id/but9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/etext"android:layout_toRightOf="@id/but8"android:text=" 9 "android:textSize="30px"/><Buttonandroid:id="@+id/but4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but7"android:text=" 4 "android:textSize="30px"/><Buttonandroid:id="@+id/but5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but8"android:layout_toRightOf="@id/but4"android:text=" 5 "android:textSize="30px"/><Buttonandroid:id="@+id/but6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but9"android:layout_toRightOf="@id/but5"android:text=" 6 "android:textSize="30px"/><Buttonandroid:id="@+id/but1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but4"android:text=" 1 "android:textSize="30px"/><Buttonandroid:id="@+id/but2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but5"android:layout_toRightOf="@id/but1"android:textSize="30px"/><Buttonandroid:id="@+id/but3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but6"android:layout_toRightOf="@id/but2"android:text=" 3 "android:textSize="30px"/><Buttonandroid:id="@+id/but0"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but1"android:text=" 0 "android:textSize="30px"/><Buttonandroid:id="@+id/add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/etext"android:layout_toRightOf="@id/but9"android:text=" + "android:textSize="30px"/><Buttonandroid:id="@+id/sub"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/add"android:layout_toRightOf="@id/but6"android:text=" - "android:textSize="30px"/><Buttonandroid:id="@+id/mult"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/sub"android:layout_toRightOf="@id/but3"android:textSize="30px"/><Buttonandroid:id="@+id/dim"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but2"android:layout_toRightOf="@id/but0"android:text=" . "android:textSize="30px"/><Buttonandroid:id="@+id/caculate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/but3"android:layout_toRightOf="@id/dim"android:gravity="center"android:text=" = "android:textSize="30px"/><Buttonandroid:id="@+id/divid"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/mult"android:layout_toRightOf="@id/caculate"android:text=" / "android:textSize="30px"/></RelativeLayout>实现源代码import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */private Button but0 = null;private Button but1 = null;private Button but2 = null;private Button but3 = null;private Button but4 = null;private Button but5 = null;private Button but6 = null;private Button but7 = null;private Button but8 = null;private Button but9 = null;private Button dim = null;private Button add = null;private Button sub = null;private Button mult = null;private Button div = null;private Button equ = null;private EditText etext = null;private double value = 0;private String str1 = null;private String tem = "";private String sign = "a";@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);super.setContentView(yout.de);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(new AListener());sub.setOnClickListener(new AListener());mult.setOnClickListener(new AListener());div.setOnClickListener(new AListener());equ.setOnClickListener(new Listener());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);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (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);}if(v.equals(dim)){tem=tem+".";etext.setText(tem);}}class AListener implements OnClickListener {@Overridepublic void onClick(View arg) {// TODO Auto-generated method stubstr1 = etext.getText().toString();if (str1.equals("")) {} else {double d = 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 = "";}}class Listener implements OnClickListener {@Overridepublic void onClick(View arg) {// TODO Auto-generated method stubstr1 = etext.getText().toString();if (str1.equals("")) {} else {double d = 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 = "";}}}。