安卓计算器

写个简单的安卓计算器

安卓计算器

activity_main.xml

<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/result"
        android:cursorVisible="false"
        android:editable = "false"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16px"
        android:layout_marginRight="16px"
        android:orientation="horizontal">
        <Button
            android:id="@+id/C"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/C"
            android:textColor="#ffff6970"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/addition"
            android:text="@string/addition"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Multiplication"
            android:text="@string/Multiplication"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/del"
            android:text="@string/del"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16px"
        android:layout_marginRight="16px">
        <Button
            android:id="@+id/seven"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/seven"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/eight"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/eight"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/nine"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/nine"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/add"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16px"
        android:layout_marginRight="16px">
        <Button
            android:id="@+id/four"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/four"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/five"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/five"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/six"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/six"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/Reduction"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/Reduction"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginLeft="16px"
        android:layout_marginRight="16px">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:orientation="vertical"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/one"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="@string/one"/>
                <Button
                    android:id="@+id/two"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="@string/two"/>
                <Button
                    android:id="@+id/three"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="@string/three"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <Button
                    android:id="@+id/zero"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="@string/zero"
                    android:layout_weight="2"/>
                <Button
                    android:id="@+id/point"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/point"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/equal"
                android:text="@string/equal"
                android:textColor="#ffff6970"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

 strings.xml

<resources>
    <string name="app_name">Calculator</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

    <string name="C">C</string>
    <string name="addition">/</string>
    <string name="Multiplication">x</string>
    <string name="del">del</string>

    <string name="seven">7</string>
    <string name="eight">8</string>
    <string name="nine">9</string>
    <string name="add">+</string>

    <string name="six">6</string>
    <string name="five">5</string>
    <string name="four">4</string>
    <string name="Reduction">-</string>

    <string name="three">3</string>
    <string name="two">2</string>
    <string name="one">1</string>
    <string name="equal">=</string>

    <string name="zero">0</string>
    <string name="point">.</string>

</resources>

 MainActivity.java

package wuxiaocheng.cn.calculator;

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


public class MainActivity extends Activity implements OnClickListener {

    //定义两个参数,接受result前后的值
    double num1 = 0, num2 = 0;

    //计算结果
    double Result = 0;

    //判断是否按了“=”按钮
    boolean isClickEqu = false;

    //判断操作数
    int op = 0;

    TextView result = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //从布局中查找按钮
        Button C = (Button) this.findViewById(R.id.C);
        Button addition = (Button)this.findViewById(R.id.addition);
        Button Multiplication = (Button)this.findViewById(R.id.Multiplication);
        Button del = (Button) this.findViewById(R.id.del);
        Button seven = (Button) this.findViewById(R.id.seven);
        Button eight = (Button) this.findViewById(R.id.eight);
        Button nine = (Button) this.findViewById(R.id.nine);
        Button add = (Button) this.findViewById(R.id.add);
        Button four = (Button) this.findViewById(R.id.four);
        Button five = (Button) this.findViewById(R.id.five);
        Button six = (Button) this.findViewById(R.id.six);
        Button Reduction = (Button) this.findViewById(R.id.Reduction);
        Button one = (Button) this.findViewById(R.id.one);
        Button two = (Button) this.findViewById(R.id.two);
        Button three = (Button) this.findViewById(R.id.three);
        Button zero = (Button) this.findViewById(R.id.zero);
        Button point = (Button) this.findViewById(R.id.point);
        Button equal = (Button) this.findViewById(R.id.equal);
        result = (TextView) this.findViewById(R.id.result);

        //让activity实现点击事件的接口
        C.setOnClickListener(this);
        addition.setOnClickListener(this);
        Multiplication.setOnClickListener(this);
        del.setOnClickListener(this);
        seven.setOnClickListener(this);
        eight.setOnClickListener(this);
        nine.setOnClickListener(this);
        add.setOnClickListener(this);
        four.setOnClickListener(this);
        five.setOnClickListener(this);
        six.setOnClickListener(this);
        Reduction.setOnClickListener(this);
        one.setOnClickListener(this);
        two.setOnClickListener(this);
        three.setOnClickListener(this);
        zero.setOnClickListener(this);
        point.setOnClickListener(this);
        equal.setOnClickListener(this);
    }

    //接口里面未实现的方法
    public void onClick(View v){
        switch (v.getId()){
            //清零键
            case R.id.C:
                result.setText(null);
                break;

            //删除键
            case R.id.del:
                String myStr = result.getText().toString();
            {
                try {
                    result.setText(myStr.substring(0,myStr.length()-1));
                }catch (Exception e){
                    result.setText("");
                }
            }
            break;

            //按钮0-9
            case R.id.zero:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString0=result.getText().toString();
                myString0+="0";
                result.setText(myString0);
                break;
            case R.id.one:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString1=result.getText().toString();
                myString1+="1";
                result.setText(myString1);
                break;

            case R.id.two:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString2=result.getText().toString();
                myString2+="2";
                result.setText(myString2);
                break;

            case R.id.three:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString3=result.getText().toString();
                myString3+="3";
                result.setText(myString3);
                break;
            case R.id.four:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString4=result.getText().toString();
                myString4+="4";
                result.setText(myString4);
                break;

            case R.id.five:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString5=result.getText().toString();
                myString5+="5";
                result.setText(myString5);
                break;

            case R.id.six:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString6=result.getText().toString();
                myString6+="6";
                result.setText(myString6);
                break;
            case R.id.seven:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString7=result.getText().toString();
                myString7+="7";
                result.setText(myString7);
                break;

            case R.id.eight:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString8=result.getText().toString();
                myString8+="8";
                result.setText(myString8);
                break;

            case R.id.nine:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myString9=result.getText().toString();
                myString9+="9";
                result.setText(myString9);
                break;

            //点
            case R.id.point:
                if(isClickEqu)
                {
                    result.setText(null);
                    isClickEqu=false;
                }
                String myStringp=result.getText().toString();
                myStringp+=".";
                result.setText(myStringp);
                break;

            //加
            case R.id.add:
                String mystringadd = result.getText().toString();
                if(mystringadd.equals(null))
                {
                    return;
                }
                num1 = Double.valueOf(mystringadd);
                result.setText(null);
                op = 1;
                isClickEqu = false;
                break;
            //减
            case R.id.Reduction:
                String mystringred = result.getText().toString();
                if(mystringred.equals(null))
                {
                    return;
                }
                num1 = Double.valueOf(mystringred);
                result.setText(null);
                op = 2;
                isClickEqu = false;
                break;
            //乘
            case R.id.Multiplication:
                String mystringmul = result.getText().toString();
                if(mystringmul.equals(null))
                {
                    return;
                }
                num1 = Double.valueOf(mystringmul);
                result.setText(null);
                op = 3;
                isClickEqu = false;
                break;
            //除
            case R.id.addition:
                String mystringat = result.getText().toString();
                if(mystringat.equals(null))
                {
                    return;
                }
                num1 = Double.valueOf(mystringat);
                result.setText(null);
                op = 4;
                isClickEqu = false;
                break;

            //等于
            case R.id.equal:
                String myStringeq=result.getText().toString();
                if (myStringeq.equals(null)){
                    return;
                }
                num2 = Double.valueOf(myStringeq);
                result.setText(null);
                switch (op){
                    case 0:
                        Result = num2;
                        break;
                    case 1:
                        Result = num1 + num2;
                        break;
                    case 2:
                        Result = num1 - num2;
                        break;
                    case 3:
                        Result = num1 * num2;
                        break;
                    case 4:
                        Result = num1 / num2;
                        break;
                }
                result.setText(String.valueOf(Result));
                isClickEqu = true;
                break;
        }
    }
}

 安卓计算器

你可能感兴趣的:(计算器)