首页 > 移动平台 > 详细

Android---9---简单的计算器

时间:2015-04-11 22:37:14      阅读:276      评论:0      收藏:0      [点我收藏+]

自己做的一个简单的计算器,只能实现简单的四则运算。

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    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="com.example.calculator.MainActivity" >

    <TextView
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:hint="0"
        android:minLines="3" />

    <Button
        android:id="@+id/clean"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/show"
        android:text="清除" />

    <Button
        android:id="@+id/seven"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/clean"
        android:text="7" />

    <Button
        android:id="@+id/eight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/seven"
        android:layout_toRightOf="@+id/seven"
        android:text="8" />

    <Button
        android:id="@+id/nine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/eight"
        android:layout_alignBottom="@+id/eight"
        android:layout_toRightOf="@+id/eight"
        android:text="9" />

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/nine"
        android:layout_alignBottom="@+id/nine"
        android:layout_toRightOf="@+id/nine"
        android:text="+" />

    <Button
        android:id="@+id/four"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/seven"
        android:layout_below="@+id/seven"
        android:text="4" />

    <Button
        android:id="@+id/five"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/four"
        android:layout_alignBottom="@+id/four"
        android:layout_alignLeft="@+id/eight"
        android:text="5" />

    <Button
        android:id="@+id/six"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/five"
        android:layout_alignBottom="@+id/five"
        android:layout_toRightOf="@+id/five"
        android:text="6" />

    <Button
        android:id="@+id/subtract"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/six"
        android:layout_alignBottom="@+id/six"
        android:layout_toRightOf="@+id/six"
        android:text="-" />

    <Button
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/four"
        android:layout_below="@+id/four"
        android:text="1" />

    <Button
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/five"
        android:layout_below="@+id/five"
        android:text="2" />

    <Button
        android:id="@+id/three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/two"
        android:layout_alignBottom="@+id/two"
        android:layout_alignLeft="@+id/six"
        android:text="3" />

    <Button
        android:id="@+id/multiply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/three"
        android:layout_alignBottom="@+id/three"
        android:layout_toRightOf="@+id/three"
        android:text="*" />

    <Button
        android:id="@+id/zero"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/one"
        android:layout_alignRight="@+id/one"
        android:layout_below="@+id/one"
        android:text="0" />

    <Button
        android:id="@+id/point"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/zero"
        android:layout_alignBottom="@+id/zero"
        android:layout_toLeftOf="@+id/multiply"
        android:text="." />

    <Button
        android:id="@+id/divide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/multiply"
        android:layout_below="@+id/multiply"
        android:text="/" />

    <Button
        android:id="@+id/Symbol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/one"
        android:layout_toRightOf="@+id/one"
        android:text="-/+" />

    <Button
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/zero"
        android:layout_alignRight="@+id/divide"
        android:layout_below="@+id/zero"
        android:text="=" />

</RelativeLayout>


MainActivity.java:


package com.example.calculator;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private Button one, two, three, four, five, six, seven, eight, nine, zero;
	private Button add, subtract, multiply, divide;
	private Button Symbol, point;
	private Button clean, result;
	private TextView ShowResult;
	double num1, num2, num;
	int op = 0;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 0---9
		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);

		// +-*/
		add = (Button) findViewById(R.id.add);
		subtract = (Button) findViewById(R.id.subtract);
		multiply = (Button) findViewById(R.id.multiply);
		divide = (Button) findViewById(R.id.divide);

		point = (Button) findViewById(R.id.point);
		clean = (Button) findViewById(R.id.clean);
		result = (Button) findViewById(R.id.result);

		ShowResult = (TextView) findViewById(R.id.show);

		clean.setOnClickListener(this);

		zero.setOnClickListener(this);
		one.setOnClickListener(this);
		two.setOnClickListener(this);
		three.setOnClickListener(this);
		four.setOnClickListener(this);
		five.setOnClickListener(this);
		six.setOnClickListener(this);
		seven.setOnClickListener(this);
		eight.setOnClickListener(this);
		nine.setOnClickListener(this);

		add.setOnClickListener(this);
		subtract.setOnClickListener(this);
		multiply.setOnClickListener(this);
		divide.setOnClickListener(this);

	
		point.setOnClickListener(this);
		result.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		String content = "";

		switch (v.getId()) {
		// 0---9
		case R.id.one:
			content = ShowResult.getText().toString() + "1";
			ShowResult.setText(content);
			break;
		case R.id.two:
			content = ShowResult.getText().toString() + "2";
			ShowResult.setText(content);
			break;
		case R.id.three:
			content = ShowResult.getText().toString() + "3";
			ShowResult.setText(content);
			break;
		case R.id.four:
			content = ShowResult.getText().toString() + "4";
			ShowResult.setText(content);
			break;
		case R.id.five:
			content = ShowResult.getText().toString() + "5";
			ShowResult.setText(content);
			break;
		case R.id.six:
			content = ShowResult.getText().toString() + "6";
			ShowResult.setText(content);
			break;
		case R.id.seven:
			content = ShowResult.getText().toString() + "7";
			ShowResult.setText(content);
			break;
		case R.id.eight:
			content = ShowResult.getText().toString() + "8";
			ShowResult.setText(content);
			break;
		case R.id.nine:
			content = ShowResult.getText().toString() + "9";
			ShowResult.setText(content);
			break;
		case R.id.zero:
			content = ShowResult.getText().toString() + "0";
			ShowResult.setText(content);
			break;
		case R.id.point:
			content = ShowResult.getText().toString() + ".";
			ShowResult.setText(content);
			break;

		// +-*/
		case R.id.add:
			String addString = ShowResult.getText().toString();
			if (addString.equals("")) {
				return;
			}

			num1 = Double.valueOf(addString);
			op = 1;
			ShowResult.setText("");
			break;
		case R.id.subtract:
			String subString = ShowResult.getText().toString();
			if (subString.equals("")) {
				return;
			}

			num1 = Double.valueOf(subString);
			op = 2;
			ShowResult.setText("");

			break;
		case R.id.multiply:
			String mulString = ShowResult.getText().toString();
			if (mulString.equals("")) {
				return;
			}

			num1 = Double.valueOf(mulString);
			op = 3;
			ShowResult.setText("");
			break;
		case R.id.divide:
			String divString = ShowResult.getText().toString();
			if (divString.equals("")) {
				return;
			}
			num1 = Double.valueOf(divString);
			op = 4;
			ShowResult.setText("");
			break;
		// clean
		case R.id.clean:
			ShowResult.setText("");
			break;
		// =
		case R.id.result:
			String resultString = ShowResult.getText().toString();
			if (resultString.equals("")) {
				return;
			}
			num2 = Double.valueOf(resultString);
			switch (op) {
			case 0:
				num = num2;
				break;
			case 1:
				num = num1 + num2;
				break;
			case 2:
				num = num1 - num2;
				break;

			case 3:
				num = num1 * num2;
				break;

			case 4:
				num = num1 / num2;
				break;

			default:
				break;
			}
			ShowResult.setText(num + "");
			break;
		default:
			break;
		}
	}

}


Android---9---简单的计算器

原文:http://blog.csdn.net/u013476556/article/details/44998913

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!