Android项目之简单计算器

来源:互联网 发布:北京狮岛编程下载 编辑:程序博客网 时间:2024/05/01 15:58

MainActivity.java

package com.LC.jsq;import java.text.DecimalFormat;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {Button 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 btnAdd = null;Button btnSub = null;Button btnMul = null;Button btnDiv = null;Button btnEqu = null;Button btnpoint = null;Button btnPom = null;TextView tvResult = null;StringBuffer str_display = new StringBuffer();double num1 = 0, num2 = 0;double Result = 0;int op = 0;boolean isClickEqu = false;double get_double;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);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);btnBackspace = (Button) findViewById(R.id.btnBackspace);btnCE = (Button) findViewById(R.id.btnCE);btnEqu = (Button) findViewById(R.id.btnEqu);btnAdd = (Button) findViewById(R.id.btnAdd);btnSub = (Button) findViewById(R.id.btnSub);btnMul = (Button) findViewById(R.id.btnMul);btnDiv = (Button) findViewById(R.id.btnDiv);btnpoint = (Button) findViewById(R.id.btnpoint);btnPom = (Button) findViewById(R.id.btnpom);tvResult = (TextView) findViewById(R.id.tvResult);btnBackspace.setOnClickListener(this);btnCE.setOnClickListener(this);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);btnMul.setOnClickListener(this);btnDiv.setOnClickListener(this);btnEqu.setOnClickListener(this);btnpoint.setOnClickListener(this);btnPom.setOnClickListener(this);}public void onClick(View v) {switch (v.getId()) {case R.id.btnBackspace:String myStr = tvResult.getText().toString();op = 0;try {tvResult.setText(myStr.substring(0, myStr.length() - 1));} catch (Exception e) {tvResult.setText("");}break;case R.id.btnCE:tvResult.setText(null);op = 0;break;case R.id.btnpoint:String num3 = tvResult.getText().toString();if (num3.indexOf(".") < 0) {if (num3.length() == 0) {tvResult.setText(0 + ".");} else {tvResult.setText(num3 + ".");}} else {tvResult.setText(num3);}break;case R.id.btn0:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString = tvResult.getText().toString();if (myString.length() == 0) {return;}myString += "0";tvResult.setText(myString);break;case R.id.btn1:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString1 = tvResult.getText().toString();myString1 += "1";tvResult.setText(myString1);break;case R.id.btn2:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString2 = tvResult.getText().toString();myString2 += "2";tvResult.setText(myString2);break;case R.id.btn3:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString3 = tvResult.getText().toString();myString3 += "3";tvResult.setText(myString3);break;case R.id.btn4:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString4 = tvResult.getText().toString();myString4 += "4";tvResult.setText(myString4);break;case R.id.btn5:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString5 = tvResult.getText().toString();myString5 += "5";tvResult.setText(myString5);break;case R.id.btn6:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString6 = tvResult.getText().toString();myString6 += "6";tvResult.setText(myString6);break;case R.id.btn7:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString7 = tvResult.getText().toString();myString7 += "7";tvResult.setText(myString7);break;case R.id.btn8:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString8 = tvResult.getText().toString();myString8 += "8";tvResult.setText(myString8);break;case R.id.btn9:if (isClickEqu) {tvResult.setText(null);isClickEqu = false;}String myString9 = tvResult.getText().toString();myString9 += "9";tvResult.setText(myString9);break;case R.id.btnAdd:String myStringAdd = tvResult.getText().toString();if (myStringAdd.equals("")) {return;}num1 = Double.valueOf(myStringAdd);tvResult.setText(null);op = 1;isClickEqu = false;break;case R.id.btnSub:String myStringSub = tvResult.getText().toString();if (myStringSub.equals("")) {return;}num1 = Double.valueOf(myStringSub);tvResult.setText(null);op = 2;isClickEqu = false;break;case R.id.btnMul:String myStringMul = tvResult.getText().toString();if (myStringMul.equals("")) {return;}num1 = Double.valueOf(myStringMul);tvResult.setText(null);op = 3;isClickEqu = false;break;case R.id.btnDiv:String myStringDiv = tvResult.getText().toString();if (myStringDiv.equals("")) {return;}num1 = Double.valueOf(myStringDiv);tvResult.setText(null);op = 4;isClickEqu = false;break;case R.id.btnpom:String myStringPom = (String) tvResult.getText();if (myStringPom.equals("0"))return;if (myStringPom.startsWith("-")) {tvResult.setText(myStringPom.substring(1));return;}tvResult.setText("-" + myStringPom);break;case R.id.btnEqu:String myStringEqu = tvResult.getText().toString();if (myStringEqu.equals("")) {return;}if (Double.valueOf(myStringEqu) == get_double) {return;}num2 = Double.valueOf(myStringEqu);switch (op) {case 0:Result = num2;break;case 1:Result = num1 + num2;break;case 2:Result = num1 - num2;break;case 3:Result = num1 * num2;break;case 4:if (num2 == 0) {return;}Result = num1 / num2;break;default:Result = 0;break;}DecimalFormat df = new DecimalFormat("#.####");get_double = Double.parseDouble(df.format(Result));tvResult.setText(String.valueOf(get_double));isClickEqu = true;break;default:break;}}}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ADADAD"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/tvResult"        android:layout_width="fill_parent"        android:layout_height="60dip"        android:background="@drawable/white_bg"        android:gravity="bottom|right"        android:textColor="#000000"        android:textSize="20sp" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_marginTop="20dip"        android:gravity="center_horizontal"        android:orientation="vertical" >        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:gravity="center_horizontal"            android:orientation="horizontal" >            <Button                android:id="@+id/btnCE"                android:layout_width="70dp"                android:layout_height="70dp"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="C"                android:textSize="20sp" />            <Button                android:id="@+id/btnBackspace"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="←"                android:textSize="20sp" />            <Button                android:id="@+id/btnDiv"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="÷"                android:textSize="20sp" />            <Button                android:id="@+id/btnMul"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="×"                android:textSize="20sp" />        </LinearLayout>        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dip"            android:gravity="center_horizontal"            android:orientation="horizontal" >            <Button                android:id="@+id/btn7"                android:layout_width="70dp"                android:layout_height="70dp"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="7"                android:textSize="20sp" />            <Button                android:id="@+id/btn8"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="8"                android:textSize="20sp" />            <Button                android:id="@+id/btn9"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="9"                android:textSize="20sp" />            <Button                android:id="@+id/btnSub"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="-"                android:textSize="20sp" />        </LinearLayout>        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dip"            android:gravity="center_horizontal"            android:orientation="horizontal" >            <Button                android:id="@+id/btn4"                android:layout_width="70dp"                android:layout_height="70dp"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="4"                android:textSize="20sp" />            <Button                android:id="@+id/btn5"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="5"                android:textSize="20sp" />            <Button                android:id="@+id/btn6"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="6"                android:textSize="20sp" />            <Button                android:id="@+id/btnAdd"                android:layout_width="70dp"                android:layout_height="70dp"                android:layout_marginLeft="10dip"                android:paddingBottom="10dp"                android:paddingRight="10dp"                android:text="+"                android:textSize="20sp" />        </LinearLayout>        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_marginTop="10dip"            android:gravity="center_horizontal"            android:orientation="horizontal" >            <LinearLayout                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:orientation="vertical" >                <LinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:orientation="horizontal" >                    <Button                        android:id="@+id/btn1"                        android:layout_width="70dp"                        android:layout_height="70dp"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:text="1"                        android:textSize="20sp" />                    <Button                        android:id="@+id/btn2"                        android:layout_width="70dp"                        android:layout_height="70dp"                        android:layout_marginLeft="10dip"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:text="2"                        android:textSize="20sp" />                    <Button                        android:id="@+id/btn3"                        android:layout_width="70dp"                        android:layout_height="70dp"                        android:layout_marginLeft="10dip"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:text="3"                        android:textSize="20sp" />                    <Button                        android:id="@+id/btnpom"                        android:layout_width="70dp"                        android:layout_height="70dp"                        android:layout_marginLeft="10dip"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:text="±"                        android:textSize="20sp" />                </LinearLayout>                <LinearLayout                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:layout_marginTop="10dip"                    android:orientation="horizontal" >                    <Button                        android:id="@+id/btn0"                        android:layout_width="150dp"                        android:layout_height="70dp"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:text="0"                        android:textSize="20sp" />                    <Button                        android:id="@+id/btnpoint"                        android:layout_width="70dp"                        android:layout_height="70dp"                        android:layout_marginLeft="10dip"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:text="."                        android:textSize="20sp" />                    <Button                        android:id="@+id/btnEqu"                   android:layout_width="70dp"                        android:layout_height="70dp"                        android:layout_marginLeft="10dip"                        android:paddingBottom="10dp"                        android:paddingRight="10dp"                        android:background="@drawable/orange_selector"                        android:text="="                        android:textSize="20sp" />                </LinearLayout>            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout>

0 0
原创粉丝点击