Android计算器源码
android实现简易计算器
android实现简易计算器本⽂实例为⼤家分享了android实现简易计算器展⽰的具体代码,供⼤家参考,具体内容如下效果图:⼀、如图,⾸先布局计算器主页显⽰activity_main.xml<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><EditTextandroid:id="@+id/num1"android:layout_width="80dp"android:layout_height="wrap_content"android:inputType="number" /><TextViewandroid:id="@+id/type"android:layout_width="40dp"android:layout_height="50dp"android:layout_toRightOf="@id/num1"android:gravity="center" /><EditTextandroid:id="@+id/num2"android:layout_width="80dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/num1"android:layout_marginLeft="50dp"android:inputType="number" /><TextViewandroid:id="@+id/equal"android:layout_width="80dp"android:layout_height="50dp"android:layout_toRightOf="@id/num2"android:gravity="center" /><Buttonandroid:id="@+id/add"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_below="@id/num1"android:text="@string/add" /><Buttonandroid:id="@+id/sub"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/add"android:layout_alignTop="@id/add"android:text="@string/sub" /><Buttonandroid:id="@+id/mul"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/sub"android:layout_alignTop="@id/sub"android:text="@string/mul" /><Buttonandroid:id="@+id/div"android:layout_width="70dp"android:layout_height="wrap_content"android:layout_toRightOf="@id/mul"android:layout_alignTop="@id/mul"android:text="@string/div" /></RelativeLayout>⼆、计算器实现的⽅法public class CalculatorBiz {public float add(float num1,float num2){return num1+num2;}public float sub(float num1,float num2){return num1-num2;}public float mul(float num1,float num2){return num1*num2;}public float div(float num1,float num2){return num1/num2;}}三、MainActivity主要⽅法public class MainActivity extends Activity {//定义组件private EditText inputnum1;private EditText inputnum2;private TextView type;private TextView equal;private Button add;private Button sub;private Button mul;private Button div;CalculatorBiz calculator=new CalculatorBiz();@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(yout.activity_main);//查找组件inputnum1=(EditText) findViewById(R.id.num1); inputnum2=(EditText) findViewById(R.id.num2); type=(TextView) findViewById(R.id.type);equal=(TextView) findViewById(R.id.equal);add=(Button) findViewById(R.id.add);sub=(Button) findViewById(R.id.sub);mul=(Button) findViewById(R.id.mul);div=(Button) findViewById(R.id.div);type.setTextColor(Color.WHITE);equal.setTextColor(Color.WHITE);add.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {String num1=inputnum1.getText().toString();String num2=inputnum2.getText().toString();float a=Float.parseFloat(num1);float b=Float.parseFloat(num2);type.setText("+");equal.setText("="+calculator.add(a, b));}});sub.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String num1=inputnum1.getText().toString();String num2=inputnum2.getText().toString();float a=Float.parseFloat(num1);float b=Float.parseFloat(num2);type.setText("-");equal.setText("="+calculator.sub(a, b));}});mul.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String num1=inputnum1.getText().toString();String num2=inputnum2.getText().toString();float a=Float.parseFloat(num1);float b=Float.parseFloat(num2);type.setText("*");equal.setText("="+calculator.mul(a, b));}});div.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String num1=inputnum1.getText().toString();String num2=inputnum2.getText().toString();float a=Float.parseFloat(num1);float b=Float.parseFloat(num2);type.setText("/");equal.setText("="+calculator.div(a, b));}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);menu.add(0, 1, 0, "关于");menu.add(0, 2, 1, "退出");menu.findItem(2);return true;}public boolean onOptionsItemSelected(MenuItem item){if(item.getItemId() == 1){Toast.makeText(this, "详细内容\n"+"作者:dyp\n"+"版本:version1", Toast.LENGTH_LONG).show(); }else if(item.getItemId() == 2){Toast.makeText(this, "退出\n"+"你确定退出程序?", Toast.LENGTH_LONG).show();}return super.onOptionsItemSelected(item);}}以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
计算器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简单计算器代码————————————————————————————————作者:————————————————————————————————日期: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。
android简易计算器的实现
android简易计算器的实现最近接触了android开发就试着写了⼀个计算器的⼩程序:在xml⽂件中的布局代码如下:1<?xml version="1.0" encoding="utf-8"?>2<LinearLayout xmlns:android="/apk/res/android"3 xmlns:tools="/tools"4 android:id="@+id/activity_ji_suan02"5 android:layout_width="match_parent"6 android:layout_height="match_parent"7 android:orientation="vertical"8 tools:context="com.example.whs.sample01_1_activity.JiSuanActivity02">910<TextView11android:id="@+id/tv"12 android:layout_width="match_parent"13 android:layout_height="40dp"14 android:textSize="30dp"15 android:text="0"16 android:textColor="#ff0000"17 android:gravity="center_vertical|right"18 android:layout_marginRight="5dp"19 android:layout_marginLeft="5dp"20 android:background="#FFFF00"21/>22<!--7 8 9 + -->23<LinearLayout24android:layout_width="match_parent"25 android:layout_height="wrap_content"26 android:paddingTop="5dp"27 android:orientation="horizontal"28>2930<Button31android:id="@+id/Button07"32 android:text="7"33 android:textSize="25dp"34 android:layout_width="80dp"35 android:layout_height="wrap_content"/>36<Button37android:id="@+id/Button08"38 android:text="8"39 android:textSize="25dp"40 android:layout_width="80dp"41 android:layout_height="wrap_content"/>42<Button43android:id="@+id/Button09"44 android:text="9"45 android:textSize="25dp"46 android:layout_width="80dp"47 android:layout_height="wrap_content"/> 48<Button49android:id="@+id/ButtonJia"50 android:text="+"51 android:textSize="25dp"52 android:layout_width="80dp"53 android:layout_height="wrap_content"/> 5455</LinearLayout>5657<!--4 5 6 - -->58<LinearLayout59android:layout_width="match_parent"60 android:layout_height="wrap_content"61 android:paddingTop="5dp"62 android:orientation="horizontal"63>6465<Button66android:id="@+id/Button04"67 android:text="4"68 android:textSize="25dp"69 android:layout_width="80dp"70 android:layout_height="wrap_content"/> 71<Button72android:id="@+id/Button05"73 android:text="5"74 android:textSize="25dp"75 android:layout_width="80dp"76 android:layout_height="wrap_content"/> 77<Button78android:id="@+id/Button06"79 android:text="6"80 android:textSize="25dp"81 android:layout_width="80dp"82 android:layout_height="wrap_content"/> 83<Button84android:id="@+id/ButtonJian"85 android:text="-"86 android:textSize="25dp"87 android:layout_width="80dp"88 android:layout_height="wrap_content"/> 8990</LinearLayout>9192<!--1 2 3 * -->93<LinearLayout94android:layout_width="match_parent"95 android:layout_height="wrap_content"96 android:paddingTop="5dp"97 android:orientation="horizontal"98>99100<Button101android:id="@+id/Button01"102 android:text="1"103 android:textSize="25dp"104 android:layout_width="80dp"105 android:layout_height="wrap_content"/> 106<Button107android:id="@+id/Button02"108 android:text="2"109 android:textSize="25dp"110 android:layout_width="80dp"111 android:layout_height="wrap_content"/> 112<Button113android:id="@+id/Button03"114 android:text="3"115 android:textSize="25dp"116 android:layout_width="80dp"117 android:layout_height="wrap_content"/> 118<Button119android:id="@+id/ButtonCheng"120 android:text="*"121 android:textSize="25dp"122 android:layout_width="80dp"123 android:layout_height="wrap_content"/> 124125</LinearLayout>126127<!--0 C = / -->128<LinearLayout129android:layout_width="match_parent"130 android:layout_height="wrap_content"131 android:paddingTop="5dp"132 android:orientation="horizontal"133>134135<Button136android:id="@+id/Button00"137 android:text="0"138 android:textSize="25dp"139 android:layout_width="80dp"140 android:layout_height="wrap_content"/>141<Button142android:id="@+id/ButtonC"143 android:text="C"144 android:textSize="25dp"145 android:layout_width="80dp"146 android:layout_height="wrap_content"/>147<Button148android:id="@+id/ButtonDeng"149 android:text="="150 android:textSize="25dp"151 android:layout_width="80dp"152 android:layout_height="wrap_content"/>153<Button154android:id="@+id/ButtonChu"155 android:text="/"156 android:textSize="25dp"157 android:layout_width="80dp"158 android:layout_height="wrap_content"/>159160</LinearLayout>161162163164165166</LinearLayout>在activity中实现具体的功能1package com.example.whs.sample01_1_activity;23import android.provider.Settings;4import android.support.v7.app.AppCompatActivity;5import android.os.Bundle;6import android.view.View;7import android.widget.Button;8import android.widget.TextView;9import android.widget.Toast;1011public class JiSuanActivity02 extends AppCompatActivity {1213 TextView tv;14int[] buttons; //数字按钮数组15int result;16int result0;17int result1;1819//按钮对象声明20 Button buttonC;21 Button buttonJia;22 Button buttonJian;23 Button buttonCheng;24 Button buttonChu;25 Button buttonDengyu;2627 String str1; //旧输⼊的值28 String str2; //新输⼊的值2930int flag=0; //计算标志位,0第⼀次输⼊;1加; 2减; 3乘; 4除; 5等于 31 Button temp;3233 @Override34protected void onCreate(Bundle savedInstanceState) {35super.onCreate(savedInstanceState);36 setContentView(yout.activity_ji_suan02);3738 initButton();39//清空按钮点击事件40 buttonC.setOnClickListener(new View.OnClickListener() {41 @Override42public void onClick(View view) {43 str1 = "";44 str2 = "";45 tv.setText("0");46 result = 0;47 result1 = 0;48 result0 = 0;49 flag = 0;50 }51 });5253//监听54for (int i = 0; i < buttons.length; i++){55 temp = getBtnForId(buttons[i]);56 temp.setOnClickListener(new View.OnClickListener() {57 @Override58public void onClick(View view) {59if (flag != 0){60 str1 = "";61 }else {62 str1 = tv.getText().toString().trim();63if (str1.equals("0")){64 str1 = "";65 }66 }6768 str1 = str1 + String.valueOf(((Button)view).getText()); //获取新值69 tv.setText(str1);70 }71 });72 }7374 buttonListener(buttonJia, 1);75 buttonListener(buttonJian, 2);76 buttonListener(buttonCheng, 3);77 buttonListener(buttonChu, 4);7879 buttonDengyu.setOnClickListener(new View.OnClickListener() {80 @Override81public void onClick(View view) {8283 result1 = Integer.parseInt(str1);8485if (flag == 1){86 result = result0 + result1;87 }else if (flag == 2){88 result = result0 - result1;89 }else if (flag == 3){90 result = result0 * result1;91 }else if (flag == 4){92if (result1 == 0){93 Toast.makeText(JiSuanActivity02.this, "除数不能为0", Toast.LENGTH_SHORT).show();94 }else {95 result = result0 / result1;96 }9798 }else if (flag == 0){99 result = result1;100 }101 String str = (result + "").trim();102103if (result1 == 0 && flag == 4){104 str = "错误";105 }106 tv.setText(str);107 Toast.makeText(JiSuanActivity02.this, "结果是:" + result, Toast.LENGTH_SHORT).show(); 108 }109 });110 }111112//初始化控件资源113public void initButton(){114 tv = (TextView)this.findViewById();115 str1 = String.valueOf(tv.getText());116 str2 = "";117 buttonC = getBtnForId(R.id.ButtonC);118 buttonJia = getBtnForId(R.id.ButtonJia);119 buttonJian = getBtnForId(R.id.ButtonJian);120 buttonCheng = getBtnForId(R.id.ButtonCheng);121 buttonChu = getBtnForId(R.id.ButtonChu);122 buttonDengyu = getBtnForId(R.id.ButtonDeng);123124 buttons = new int[]{125 R.id.Button00,R.id.Button01,R.id.Button02,126 R.id.Button03,127 R.id.Button04,R.id.Button05,R.id.Button06,128 R.id.Button07,R.id.Button08,R.id.Button09129 };130 }131//根据id获取Button132public Button getBtnForId(int rID){133 Button btn = (Button)this.findViewById(rID);134return btn;135 }136//按钮监听137public void buttonListener(Button button, final int id){138 button.setOnClickListener(new View.OnClickListener() { 139 @Override140public void onClick(View view) {141 String str = tv.getText().toString().trim();142 result0 = Integer.parseInt(str);143//tv.setText("");144 flag = id;145 }146 });147 }148 }。
最新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); }}。
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作横向⽐例的划分。
Android计算器代码编写
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/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实现计算器功能示例分享(计算器源码)
从零开始学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计算器源码
计算器源码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>。
安卓开发的自制计算器源代码
这是本人自己用安卓编写的计算器,代码的优化可能有所问题,不过大家都在学习阶段,欢迎大家批评指正,还是上源码吧。
先来运行图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版(源码)
简易计算器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简单句计算器代码
第一:功能描述:1 实现简单的开发加减乘除运算2 可以实现删除数据功能第二:源代码:Calculator/res/layout/main.xml<?xml version="1.0"encoding="utf-8"?><AbsoluteLayout android:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="/apk/res/android"><TextView android:id="@+id/txtResult" android:layout_width="296px"android:layout_height="43px"android:background="#ffffffff"android:text="0.0"android:textSize="28sp" android:textStyle="bold"android:textColor="#ff333333"android:layout_x="11dp"android:layout_y="16dp"android:gravity="right"android:padding="2px"/> <Button android:id="@+id/btn1" android:layout_width="60px"android:layout_height="60px"android:text="1"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="15dp"android:layout_y="85dp"android:onClick="digital_click"/><Button android:id="@+id/btn2" android:layout_width="60px"android:layout_height="60px"android:text="2"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="85dp"android:layout_y="85dp"android:onClick="digital_click"/><Button android:id="@+id/btn3" android:layout_width="60px"android:layout_height="60px"android:text="3"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="155dp"android:layout_y="85dp"android:onClick="digital_click"/><Button android:id="@+id/btnAdd" android:layout_width="80px"android:layout_height="60px"android:text="+"android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="225dp"android:layout_y="85dp"android:onClick="add"/><Button android:id="@+id/btn4" android:layout_width="60px"android:layout_height="60px"android:text="4"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="15dp"android:layout_y="155dp"android:onClick="digital_click"/><Button android:id="@+id/btn5" android:layout_width="60px"android:layout_height="60px"android:text="5"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="85dp"android:layout_y="155dp"android:onClick="digital_click"/><Button android:id="@+id/btn6" android:layout_width="60px"android:layout_height="60px"android:text="6"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="155dp"android:layout_y="155dp"android:onClick="digital_click"/><Button android:id="@+id/btnsub" android:layout_width="80px"android:layout_height="60px"android:text="-"android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="225dp"android:layout_y="155dp"android:onClick="sub"/><Button android:id="@+id/btn7" android:layout_width="60px"android:layout_height="60px"android:text="7"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="15dp"android:layout_y="225dp"android:onClick="digital_click"/><Button android:id="@+id/btn8" android:layout_width="60px"android:layout_height="60px"android:text="8"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="85dp"android:layout_y="225dp"android:onClick="digital_click"/><Button android:id="@+id/btn9" android:layout_width="60px"android:layout_height="60px"android:text="9"android:textSize="20sp"android:background="#ff00ff99"android:textStyle="bold"android:gravity="center"android:layout_x="155dp"android:layout_y="225dp"android:onClick="digital_click"/><Button android:id="@+id/btnMul" android:layout_width="80px"android:layout_height="60px"android:text="*"android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="225dp"android:layout_y="225dp"android:onClick="mul"/><Button android:id="@+id/btn0" android:layout_width="130px"android:layout_height="60px"android:text="0"android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="15dp"android:layout_y="295dp"android:onClick="digital_click"/><Button android:id="@+id/btnPoint" android:layout_width="60px"android:layout_height="60px"android:text="."android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="155dp"android:layout_y="295dp"android:onClick="point_click"/><Button android:id="@+id/btnDiv" android:layout_width="80px"android:layout_height="60px"android:text="/"android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="225dp"android:layout_y="295dp"android:onClick="div"/><Button android:id="@+id/btndel"android:layout_width="60px"android:layout_height="60px"android:text="DE>"android:textSize="20sp"android:textStyle="bold"android:textColor="#ffff3333"android:gravity="center"android:layout_x="15dp"android:layout_y="365dp"android:onClick="del"/><Button android:id="@+id/btnequ" android:layout_width="220px"android:layout_height="60px"android:text="=" android:text="="android:textSize="20sp"android:textStyle="bold"android:gravity="center"android:layout_x="85dp"android:layout_y="365dp"android:onClick="equ"/></AbsoluteLayout>第三:calculator/src/com.gallop.ma/calcu.javapackage com.gallop.ma;import java.text.NumberFormat;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class calcu extends Activity {/** Called when the activity is first created. */double firstNum = 0;// 第一个输入的数据char currentSign = '+';// 记录第一次输入的符号StringBuffer currentNum = new StringBuffer();// 得到textview中的数据boolean isFirstPoint = false;// 第一个数据是否是小数点TextView txtResult;// 输出结果@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);txtResult = (TextView) findViewById(R.id.txtResult);}/*** 对数据进行初始化** @return*/public void init() {currentNum.delete(0, currentNum.length());// 设置当前textView中的值为0isFirstPoint = false;}/***将输入的数据转换成double类型** @return*/public double stringToDouble() {if (currentNum.length() == 0) {// 如果没有输入的数据return 0;}double result = Double.parseDouble(currentNum.toString());return result;}/*** 进行计算处理* @return*/public double calcu(){double result=0;switch(currentSign){case '+':result=firstNum+stringToDouble();break;case '-':result=firstNum-stringToDouble();break;case '*':result=firstNum*stringToDouble();break;case '/':result=firstNum/stringToDouble();break;}//对小数点后的数据进行格式化NumberFormat format =NumberFormat.getInstance();format.setMaximumFractionDigits(2);result=Double.parseDouble(format.format(result ));return result;}/*** 显示数据*/public void display(){txtResult.setText(currentNum.toString()); }/*** 处理数据按钮的点击** @param view*/public void digital_click(View view) {Button btnDigital=(Button) view;char text=btnDigital.getText().charAt(0); currentNum.append(text);display();}/*** 处理加法**/public void add(View view) {double result=calcu();txtResult.setText(String.valueOf(result));firstNum=result;currentSign='+';init();}/*** 处理减法**/public void sub(View view) {double result=calcu();txtResult.setText(String.valueOf(result)); firstNum=result;currentSign='-';init();}/*** 处理乘法**/public void mul(View view) {double result=calcu();txtResult.setText(String.valueOf(result)); firstNum=result;currentSign='*';init();}/*** 处理除法**/public void div(View view) {double result=calcu();txtResult.setText(String.valueOf(result)); firstNum=result;currentSign='/';init();}/*** 处理等于**/public void equ(View view) {double result=calcu();txtResult.setText(String.valueOf(result)); firstNum=result;currentSign='+';init();}/*** 处理小数点**/public void point_click(View view) {if(isFirstPoint){//当地一个数据为小数点时程序返回return;}if(currentNum.length()==0){//当没有输入的数据时返回return;}Button btnPoint=(Button)view;char text=btnPoint.getText().charAt(0); currentNum.append(text);isFirstPoint=true;display();}/*** 处理删除数据*/public void del(View view){if(currentNum.length()>=1){currentNum.delete(currentNum.length()-1,curren tNum.length());}if(currentNum.length()==0){init();display();}txtResult.setText(currentNum);}}。
计算器安卓源代码
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计算器源代码
界面设计<LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="2"android:orientation="horizontal"><EditTextandroid:id="@+id/led"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_margin="3dip"android:background="#ccc"android:gravity="top|right"android:text=""android:textColor="#000"android:textSize="12pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/time"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="time"android:textSize="10pt"/><Buttonandroid:id="@+id/pow"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="pow"android:textSize="10pt"/><Buttonandroid:id="@+id/sqrt"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="sqrt"android:textSize="10pt"/><Buttonandroid:id="@+id/clear"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="clear"android:textSize="10pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/seven"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="7"android:textSize="12pt"/><Buttonandroid:id="@+id/eight"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="8"android:textSize="12pt"/><Buttonandroid:id="@+id/nine"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="9"android:textSize="12pt"/><Buttonandroid:id="@+id/division"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="÷"android:textSize="12pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/four"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="4"android:textSize="12pt"/><Buttonandroid:id="@+id/five"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="5"android:textSize="12pt"/><Buttonandroid:id="@+id/six"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="6"android:textSize="12pt"/><Buttonandroid:id="@+id/mult"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="*"android:textSize="12pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/one"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="1"android:textSize="12pt"/><Buttonandroid:id="@+id/two"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="2"android:textSize="12pt"/><Buttonandroid:id="@+id/three"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="3"android:textSize="12pt"/><Buttonandroid:id="@+id/sub"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="-"android:textSize="12pt"/></LinearLayout>Java源代码package youtclss;import java.util.Date;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 { private Button one;private Button two;private Button three;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 time;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.main);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(new OnClickListener() { 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(new OnClickListener() {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());}});}。
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为附加信息。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
计算器源码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>。