android实现计算器功能

设计一个简单的计算器。

第一个Activity的界面。

 

第二个Activity显示算式和计算结果。

第一个Activity代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	Button one,two,three,four,five,six,seven,eight,nine,zero,div,mod,mul,fenshu,sub,equal,point,add;
	double num1,num2,num,xiaoshudian;
	int flag,ispoint;
	char operation[]=new char[2];
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		num1=0;num2=0;num=0;flag=0;ispoint=0;xiaoshudian=0.1;
		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);
		zero=(Button)findViewById(R.id.zero);
		div=(Button)findViewById(R.id.div);
		mod=(Button)findViewById(R.id.mod);
		mul=(Button)findViewById(R.id.mul);
		fenshu=(Button)findViewById(R.id.fenshu);
		sub=(Button)findViewById(R.id.sub);
		add=(Button)findViewById(R.id.add);
		equal=(Button)findViewById(R.id.equal);
		point=(Button)findViewById(R.id.point);
		one.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+1;
					else {num1=num1+1*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+1;
					else{num2=num2+1*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		two.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+2;
					else {num1=num1+2*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+2;
					else{num2=num2+2*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		three.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+3;
					else {num1=num1+3*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+3;
					else{num2=num2+3*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		four.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+4;
					else {num1=num1+4*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+4;
					else{num2=num2+4*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		five.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+5;
					else {num1=num1+5*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+5;
					else{num2=num2+5*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		six.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+6;
					else {num1=num1+6*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+6;
					else{num2=num2+6*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		seven.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+7;
					else {num1=num1+7*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+7;
					else{num2=num2+7*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		eight.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+8;
					else {num1=num1+8*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+8;
					else{num2=num2+8*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		nine.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10+9;
					else {num1=num1+9*xiaoshudian;xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10+9;
					else{num2=num2+9*xiaoshudian;xiaoshudian/=10;}
				}
			}
		});
		zero.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==0){
					if(ispoint==0)num1=num1*10;
					else {xiaoshudian/=10;}
				}
				else{
					if(ispoint==0)num2=num2*10;
					else{xiaoshudian/=10;}
				}
			}
		});
		point.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				ispoint=1;
			}
		});
		add.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				flag=1;
				ispoint=0;
			}
		});
		sub.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				flag=2;
				ispoint=0;
			}
		});
		mul.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				flag=3;
				ispoint=0;
			}
		});
		div.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				flag=4;
				ispoint=0;
			}
		});
		equal.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if(flag==1){num=num1+num2;operation[0]='+';}
				else if(flag==2){num=num1-num2;operation[0]='-';}
				else if(flag==3){num=num1*num2;operation[0]='*';}
				else if(flag==4){num=num1/num2;operation[0]='/';}
				Intent intent=new Intent(MainActivity.this,SecondActivity.class);
				intent.putExtra("num1", new Double(num1).toString());
				intent.putExtra("opreation",new String(operation).toString());
				intent.putExtra("num2", new Double(num2).toString());
				intent.putExtra("num", new Double(num).toString());
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


}

第二个Activity代码:

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;

public class SecondActivity extends Activity{
	EditText text;
	Button myButton;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		text=(EditText)findViewById(R.id.edt);
		myButton=(Button)findViewById(R.id.btn);
		Intent intent=getIntent();
		String num1=intent.getStringExtra("num1");
		String operation=intent.getStringExtra("opreation");
		String num2=intent.getStringExtra("num2");
		String num=intent.getStringExtra("num");
		text.setText(num1+operation+num2+"="+num);
		myButton.setOnClickListener(new myButtonListener());
	}
	class myButtonListener implements OnClickListener{
		public void onClick(View arg0){
			Intent intent=new Intent(SecondActivity.this,MainActivity.class);
			startActivity(intent);
		}
	}
}

第一个Activity布局:

<RelativeLayout 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: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" >
	<Button
	    android:id="@+id/seven"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="7"
	    />
	<Button
	    android:id="@+id/eight"
	    android:layout_toRightOf="@id/seven"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="8"
	    />
	<Button
	    android:id="@+id/nine"
	    android:layout_toRightOf="@id/eight"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="9"
	    />
	<Button
	    android:id="@+id/div"
	    android:layout_toRightOf="@id/nine"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="/"
	    />
	<Button
	    android:id="@+id/mod"
	    android:layout_toRightOf="@id/div"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="%"
	    />
	<Button
	    android:id="@+id/four"
	    android:layout_below="@id/seven"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="4"
	    />
	<Button
	    android:id="@+id/five"
	    android:layout_toRightOf="@id/four"
	    android:layout_below="@id/eight"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="5"
	    />
	<Button
	    android:id="@+id/six"
	    android:layout_toRightOf="@id/five"
	    android:layout_below="@id/nine"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="6"
	    />
	<Button
	    android:id="@+id/mul"
	    android:layout_toRightOf="@id/six"
	    android:layout_below="@id/div"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="*"
	    />
	<Button
	    android:id="@+id/fenshu"
	    android:layout_toRightOf="@id/mul"
	    android:layout_below="@id/mod"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="1/x"
	    />
	<Button
	    android:id="@+id/one"
	    android:layout_below="@id/four"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="1"
	    />
	<Button
	    android:id="@+id/two"
	    android:layout_toRightOf="@id/one"
	    android:layout_below="@id/five"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="2"
	    />
	<Button
	    android:id="@+id/three"
	    android:layout_toRightOf="@id/two"
	    android:layout_below="@id/six"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="3"
	    />
	<Button
	    android:id="@+id/sub"
	    android:layout_toRightOf="@id/three"
	    android:layout_below="@id/mul"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="-"
	    />
	<Button
	    android:id="@+id/equal"
	    android:layout_toRightOf="@id/sub"
	    android:layout_below="@id/fenshu"
	    android:layout_width="40px"
        android:layout_height="70px"
        android:text="="
	    />
	<Button
	    android:id="@+id/zero"
	    android:layout_below="@id/one"
	    android:layout_width="80px"
        android:layout_height="wrap_content"
        android:text="0"
	    />
	<Button
	    android:id="@+id/point"
	    android:layout_toRightOf="@id/zero"
	    android:layout_below="@id/three"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="."
	    />
	<Button
	    android:id="@+id/add"
	    android:layout_toRightOf="@id/point"
	    android:layout_below="@id/sub"
	    android:layout_width="40px"
        android:layout_height="wrap_content"
        android:text="+"
	    />

</RelativeLayout>

第二个Activity布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<EditText
     android:id="@+id/edt"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/one"
     />  
<Button
     android:id="@+id/btn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/edt"
     android:text="返回"
     />  
</LinearLayout>


posted @ 2014-07-17 19:06  hrhguanli  阅读(505)  评论(0编辑  收藏  举报