安卓编写简单的计算器

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener{

    private EditText edit;
	private Button night;
	private Button eight;
	private Button seven;
	private Button six;
	private Button fine;
	private Button four;
	private Button three;
	private Button two;
	private Button one;
	private Button zero;
	private Button divide;
	private Button add;
	private Button ride;
	private Button reduce;
	private Button piont;
	private Button equals;
	private Button delete;
	
	private String tag="";
	private boolean flag=true;
	private int i;
	private double num1;
	private double num2;
	private double result;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        edit = (EditText) findViewById(R.id.edit);
        edit.setOnClickListener(this);
        
        night = (Button) findViewById(R.id.night);
        night.setOnClickListener(this);
        
        eight = (Button) findViewById(R.id.eight);
        eight.setOnClickListener(this);
        
        seven=(Button) findViewById(R.id.seven);
        seven.setOnClickListener(this);
        
        six=(Button) findViewById(R.id.six);
        six.setOnClickListener(this);
        
        fine=(Button) findViewById(R.id.fine);
        fine.setOnClickListener(this);
        
        four=(Button) findViewById(R.id.four);
        four.setOnClickListener(this);
        
        three=(Button) findViewById(R.id.three);
        three.setOnClickListener(this);
        
        two=(Button) findViewById(R.id.two);
        two.setOnClickListener(this);
        
        one=(Button) findViewById(R.id.one);
        one.setOnClickListener(this);
        
        zero=(Button) findViewById(R.id.zero);
        zero.setOnClickListener(this);
        
        divide=(Button) findViewById(R.id.divide);
        divide.setOnClickListener(this);
        
        ride=(Button) findViewById(R.id.ride);
        ride.setOnClickListener(this);
        
        reduce=(Button) findViewById(R.id.reduce);
        reduce.setOnClickListener(this);
        
        add=(Button) findViewById(R.id.add);
        add.setOnClickListener(this);
        
        piont=(Button) findViewById(R.id.point);
        piont.setOnClickListener(this);
        
        equals=(Button) findViewById(R.id.equals);
        equals.setOnClickListener(this);
        
        delete=(Button) findViewById(R.id.delete);
        delete.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.delete:
			 edit.setText("");
			 tag="";
			 break;
		case R.id.point:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+".";
			edit.setText(tag);
			break;
		case R.id.zero:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"0";
			edit.setText(tag);
			break;
		case R.id.one:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"1";
			edit.setText(tag);
			break;
		case R.id.two:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"2";
			edit.setText(tag);
			break;
		case R.id.three:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"3";
			edit.setText(tag);
			break;
		case R.id.four:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"4";
			edit.setText(tag);
			break;
		case R.id.fine:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"5";
			edit.setText(tag);
			break;
		case R.id.six:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"6";
			edit.setText(tag);
			break;
		case R.id.seven:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"7";
			edit.setText(tag);
			break;
		case R.id.eight:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"8";
			edit.setText(tag);
			break;
		case R.id.night:
			if(flag){
				edit.setText("");
				flag=false;
			}
			tag=tag+"9";
			edit.setText(tag);
			break;
		case R.id.add:
			tag=tag+"+";
			edit.setText(tag);
            i=1;
			break;
		case R.id.divide:
			tag=tag+"/";
			edit.setText(tag);
			i=2;
			break;
		case R.id.reduce:
			tag=tag+"-";
			edit.setText(tag);
			i=3;
			break;
		case R.id.ride:
			tag=tag+"*";
			edit.setText(tag);
			i=4;
			break;
			
		case R.id.equals:
			edit.setText("");
			switch (i) {
			case 1:
				num1=Double.parseDouble(tag.substring(0, tag.indexOf("+")));
				num2=Double.parseDouble(tag.substring(tag.indexOf("+")+1, tag.length()));
				result=num1+num2;
				break;
			case 2:
				num1=Double.parseDouble(tag.substring(0, tag.indexOf("/")));
				num2=Double.parseDouble(tag.substring(tag.indexOf("/")+1, tag.length()));
				if(num2==0){
				edit.setText("除数不能为0");
				}else{
				result=num1/num2;
				}
				break;
			case 3:
				num1=Double.parseDouble(tag.substring(0, tag.indexOf("-")));
				num2=Double.parseDouble(tag.substring(tag.indexOf("-")+1, tag.length()));
				result=num1-num2;
				break;
			case 4:
				num1=Double.parseDouble(tag.substring(0, tag.indexOf("*")));
				num2=Double.parseDouble(tag.substring(tag.indexOf("*")+1, tag.length()));
				result=num1*num2;
				break;

			}
			edit.setText(String.valueOf(result));
			flag=true;
			tag="";	
			break;
		}
	
			
		
	}


    
}

展开阅读全文

没有更多推荐了,返回首页