简单计算器(Android)

感受:1.加强了对点击事件的了解。

        2.了解到了TextView的append,setText,getText方法。

        3.了解到TableLayout中的TableRow的用法。

Activity:

package com.example.calculater;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener{

    private TextView tvScreen;
    
    private List<Item> items = new ArrayList<Item>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tvScreen = (TextView)findViewById(R.id.tvScreen);
        findViewById(R.id.btn0).setOnClickListener(this);
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
        findViewById(R.id.btn5).setOnClickListener(this);
        findViewById(R.id.btn6).setOnClickListener(this);
        findViewById(R.id.btn7).setOnClickListener(this);
        findViewById(R.id.btn8).setOnClickListener(this);
        findViewById(R.id.btn9).setOnClickListener(this);
        findViewById(R.id.btnAdd).setOnClickListener(this);
        findViewById(R.id.btnSub).setOnClickListener(this);
        findViewById(R.id.btnMul).setOnClickListener(this);
        findViewById(R.id.btnDiv).setOnClickListener(this);
        findViewById(R.id.btnResult).setOnClickListener(this);
        findViewById(R.id.btnC).setOnClickListener(this);
        findViewById(R.id.btnBack).setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.btn0:
            tvScreen.append("0");
            break;
        case R.id.btn1:
            tvScreen.append("1");
            break;
        case R.id.btn2:
            tvScreen.append("2");
            break;
        case R.id.btn3:
            tvScreen.append("3");
            break;
        case R.id.btn4:
            tvScreen.append("4");
            break;
        case R.id.btn5:
            tvScreen.append("5");
            break;
        case R.id.btn6:
            tvScreen.append("6");
            break;
        case R.id.btn7:
            tvScreen.append("7");
            break;
        case R.id.btn8:
            tvScreen.append("8");
            break;
            
        case R.id.btn9:
            tvScreen.append("9");
            break;
        case R.id.btnAdd:
            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));
            checkAndCompute();
            items.add(new Item(0, Types.ADD));
            tvScreen.setText("");
            break;
        case R.id.btnSub:
            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));
            checkAndCompute();
            items.add(new Item(0, Types.SUB));
            tvScreen.setText("");
            break;
        case R.id.btnMul:
            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));
            checkAndCompute();
            items.add(new Item(0, Types.M));
            tvScreen.setText("");
            break;
        case R.id.btnDiv:
            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));
            checkAndCompute();
            items.add(new Item(0, Types.DIV));
            tvScreen.setText("");
            break;
        case R.id.btnResult:
            items.add(new Item(Double.parseDouble(tvScreen.getText().toString()), Types.NUM));
            checkAndCompute();
            tvScreen.setText(items.get(0).value+"");
            items.clear();
            break;
        case R.id.btnC:
            tvScreen.setText("");
            items.clear();
            break;
        case R.id.btnBack:
            String res = tvScreen.getText().toString();
            try{
                tvScreen.setText(res.substring(0,res.length()-1));
            } catch (Exception e){
                tvScreen.setText("");
            }
            
        }
        
    }
    
    public void checkAndCompute(){
        if(items.size()>=3){
            
            double a = items.get(0).value;
            double b = items.get(2).value;
            int apt = items.get(1).type;
            
            items.clear();
            
            switch(apt){
            case Types.ADD:
                items.add(new Item(a+b, Types.NUM));
                break;
            case Types.SUB:
                items.add(new Item(a-b, Types.NUM));
                break;
            case Types.M:
                items.add(new Item(a*b, Types.NUM));
                break;
            case Types.DIV:
                items.add(new Item(a/b, Types.NUM));
                break;
            
            }
        }
    }
}
package com.example.calculater;

public class Item {

    public Item(double value, int type){
        
        this.value = value;
        this.type = type;
        
    }
    
    protected double value = 0;
    
    protected int type = 0;

}
package com.example.calculater;

public class Types {
    
    public static final int ADD = 1;
    
    public static final int SUB = 2;
    
    public static final int M = 3;
    
    public static final int DIV = 4;
    
    public static final int NUM = 5;

}

布局:

<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:orientation="vertical" >

    <TextView
        android:id="@+id/tvScreen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:gravity="right"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />

            <Button
                android:id="@+id/btnAdd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="+" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btn4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="4" />

            <Button
                android:id="@+id/btn5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="5" />

            <Button
                android:id="@+id/btn6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="6" />

            <Button
                android:id="@+id/btnSub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="-" />
        </TableRow>

        <TableRow
            
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btn7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="7" />

            <Button
               android:id="@+id/btn8" 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="8" />

            <Button
                android:id="@+id/btn9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="9" />

            <Button
                android:id="@+id/btnMul"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="*" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btnC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:id="@+id/btn0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="0" />

            <Button
                android:id="@+id/btnResult"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="=" />

            <Button
                android:id="@+id/btnDiv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="/" />
        </TableRow>

        <Button
            android:id="@+id/btnBack"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BackSpace" />
        
    </TableLayout>

</LinearLayout>

 

posted @ 2016-02-03 21:20  JoneZP  阅读(258)  评论(0编辑  收藏  举报