1. 程式人生 > >寫了一個簡易計算器

寫了一個簡易計算器

這裡寫圖片描述

  • 功能:能做簡單的加減乘除運算。但是隻能運算一次…………不能連續運算。
  • 佈局原始碼
<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"
    tools:context="com.example.a.MainActivity" >

    <TableLayout 
        android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="7"> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="3" android:text="" android:textSize="24sp"
/> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > <Button android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="fill_parent"
android:layout_weight="1" android:text="1" /> <Button android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/three" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="3" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="+" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_span="4" android:layout_weight="1" > <Button android:id="@+id/four" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="4" /> <Button android:id="@+id/five" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="5" /> <Button android:id="@+id/six" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="6" /> <Button android:id="@+id/remove" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="-" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > <Button android:id="@+id/seven" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="7" /> <Button android:id="@+id/eight" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="8" /> <Button android:id="@+id/nine" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="9" /> <Button android:id="@+id/x" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="x" /> </TableRow> <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" > <Button android:id="@+id/zero" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="0" /> <Button android:id="@+id/qingchu" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="C"/> <Button android:id="@+id/deng" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="=" /> <Button android:id="@+id/chu" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:text="/" /> </TableRow> </TableLayout> </RelativeLayout>
  • 邏輯原始碼
package com.example.a;

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.TextView;

public class MainActivity extends Activity implements OnClickListener {

    public StringBuffer sb = new StringBuffer();//用於將每次按鍵按下的值連起來
    private int number = 0;//計數器:按下+為0;按下-為1;按下x為3;按下/為3;
    private TextView tv_xs;
    public long resultnum = 0;//結果值

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_xs = (TextView) findViewById(R.id.tv);
        Button one = (Button) findViewById(R.id.one);
        Button two = (Button) findViewById(R.id.two);
        Button three = (Button) findViewById(R.id.three);
        Button four = (Button) findViewById(R.id.four);
        Button five = (Button) findViewById(R.id.five);
        Button six = (Button) findViewById(R.id.six);
        Button seven = (Button) findViewById(R.id.seven);
        Button eight = (Button) findViewById(R.id.eight);
        Button nine = (Button) findViewById(R.id.nine);
        Button zero = (Button) findViewById(R.id.zero);
        Button add = (Button) findViewById(R.id.add);//加
        Button chu = (Button) findViewById(R.id.chu);//除
        Button remove = (Button) findViewById(R.id.remove);//減
        Button deng = (Button) findViewById(R.id.deng);//等號
        Button x = (Button) findViewById(R.id.x);//乘
        Button qingchu =(Button)findViewById(R.id.qingchu);//清零
        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);
        zero.setOnClickListener(this);
        add.setOnClickListener(this);
        chu.setOnClickListener(this);
        remove.setOnClickListener(this);
        deng.setOnClickListener(this);
        x.setOnClickListener(this);
        qingchu.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        try {
            switch (v.getId()) {
            case R.id.one:
                int one = 1;
                sb.append(one);
                tv_xs.setText(sb.toString());
                break;
            case R.id.two:
                int two = 2;
                sb.append(two);
                tv_xs.setText(sb.toString());
                break;
            case R.id.three:
                int three = 3;
                sb.append(three);
                tv_xs.setText(sb.toString());
                break;
            case R.id.four:
                int four = 4;
                sb.append(four);
                tv_xs.setText(sb.toString());
                break;

            case R.id.five:
                int five = 5;
                sb.append(five);
                tv_xs.setText(sb.toString());
                break;

            case R.id.six:
                int six = 6;
                sb.append(six);
                tv_xs.setText(sb.toString());
                break;

            case R.id.seven:
                int seven = 7;
                sb.append(seven);
                tv_xs.setText(sb.toString());
                break;
            case R.id.eight:
                int eight = 8;
                sb.append(eight);
                tv_xs.setText(sb.toString());
                break;
            case R.id.nine:
                int nine = 9;
                sb.append(nine);
                tv_xs.setText(sb.toString());
                break;
            case R.id.zero:
                int zero = 0;
                sb.append(zero);
                tv_xs.setText(sb.toString());
                break;
            case R.id.add:
                tv_xs.setText("+");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 0;
                break;
            case R.id.remove:
                tv_xs.setText("-");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 1;
                break;
            case R.id.x:
                tv_xs.setText("x");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 2;
                break;
            case R.id.chu:
                tv_xs.setText("%");
                resultnum = Long.valueOf(sb.toString());
                sb.delete(0, sb.length());
                number = 3;
                break;
            case R.id.deng:
                long deng_long = Long.valueOf(sb.toString());
                switch (number) {
                case 0:
                    long relust0 = resultnum + deng_long;
                    tv_xs.setText(relust0 + "");
                    break;
                case 1:
                    long relust1 = resultnum - deng_long;
                    tv_xs.setText(relust1 + "");
                    break;
                case 2:
                    long relust2 = resultnum * deng_long;
                    tv_xs.setText(relust2 + "");
                    break;
                case 3:
                    long relust3 = resultnum / deng_long;
                    tv_xs.setText(relust3 + "");
                    break;

                default:
                    break;
                }
                sb.delete(0, sb.length());
                break;
            case R.id.qingchu:
                tv_xs.setText("");
                sb.delete(0, sb.length());
                break;
            default:
                break;
            }
        } catch (Exception e) {
            // TODO: handle exception
            tv_xs.setText("");
        }

    }
}