简易计算器app的制作

制作app的话你可以用eclipse,当然你也可以用Googel推出的官方app制作软件Android studio,两者都可以。
计算器apk下载:下载

activity_main.xml文件内容:

 <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"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    tools:context="com.example.calculater.MainActivity" >
 
 <EditText
        android:id="@+id/et_result"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="left"
        android:gravity="bottom"
        android:textSize="26sp" />
    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="清除"
        android:textSize="26sp" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:columnCount="4"
        android:rowCount="4">
        <Button
            android:id="@+id/btn_1"
            android:text="1"
            android:textSize="26sp"/>
        <Button
            android:id="@+id/btn_2"
            android:text="2"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_3"
            android:text="3"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_add"
            android:text="+"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_4"
            android:text="4"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_5"
            android:text="5"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_6"
            android:text="6"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_sub"
            android:text="-"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_7"
            android:text="7"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_8"
            android:text="8"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_9"
            android:text="9"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_mul"
            android:text="×"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_dot"
            android:text="."
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_0"
            android:text="0"
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_equ"
            android:text="="
            android:textSize="26sp" />
        <Button
            android:id="@+id/btn_div"
            android:text="÷"
            android:textSize="26sp" />
    </GridLayout>

</LinearLayout>

 

MainActivity.java的内容:

package com.example.calculater;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

	private Button btn1; // 数字1
	private Button btn2; // 数字2
	private Button btn3; // 数字3
	private Button btn4; // 数字4
	private Button btn5; // 数字5
	private Button btn6; // 数字6
	private Button btn7; // 数字7
	private Button btn8; // 数字8
	private Button btn9; // 数字9
	private Button btn0; // 数字0
	private Button add; // +号
	private Button sub; // -号
	private Button mul; // *号
	private Button div; // 除号
	private Button dot; // 小数点
	private Button equ; // =号
	private Button clear; //清除
	private EditText result; // 显示文本
    
    double num1 = 0, num2 = 0; //计算的数字
    double Result = 0;//计算结果
    int op = 0;//判断操作符
    String opd = " ";//显示操作符
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取页面上的控件
        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btn4 = (Button) findViewById(R.id.btn_4);
        btn5 = (Button) findViewById(R.id.btn_5);
        btn6 = (Button) findViewById(R.id.btn_6);
        btn7 = (Button) findViewById(R.id.btn_7);
        btn8 = (Button) findViewById(R.id.btn_8);
        btn9 = (Button) findViewById(R.id.btn_9);
        btn0 = (Button) findViewById(R.id.btn_0);
        add = (Button) findViewById(R.id.btn_add);
        sub = (Button) findViewById(R.id.btn_sub);
        mul = (Button) findViewById(R.id.btn_mul);
        div = (Button) findViewById(R.id.btn_div);
        equ = (Button) findViewById(R.id.btn_equ);
        dot = (Button) findViewById(R.id.btn_dot);
        clear = (Button) findViewById(R.id.btn_clear);
        result = (EditText) findViewById(R.id.et_result);

        // 按钮的单击事件
        btn1.setOnClickListener(new Click());
        btn2.setOnClickListener(new Click());
        btn3.setOnClickListener(new Click());
        btn4.setOnClickListener(new Click());
        btn5.setOnClickListener(new Click());
        btn6.setOnClickListener(new Click());
        btn7.setOnClickListener(new Click());
        btn8.setOnClickListener(new Click());
        btn9.setOnClickListener(new Click());
        btn0.setOnClickListener(new Click());
        add.setOnClickListener(new Click());
        sub.setOnClickListener(new Click());
        mul.setOnClickListener(new Click());
        div.setOnClickListener(new Click());
        equ.setOnClickListener(new Click());
        dot.setOnClickListener(new Click());
        clear.setOnClickListener(new Click());
        result.setOnClickListener(new Click());
    }

    // 设置按钮点击后的监听
    class Click implements View.OnClickListener {
        public void onClick(View v) {
            switch (v.getId()) {                //switch循环获取点击按钮后的值
                case R.id.btn_0:                //获取,0-9、小数点,并在编辑框显示
                    String myString = result.getText().toString();
                    myString += "0";
                    result.setText(myString);
                    break;
                case R.id.btn_1:
                    String myString1 = result.getText().toString();
                    myString1 += "1";
                    result.setText(myString1);
                    break;
                case R.id.btn_2:
                    String myString2 = result.getText().toString();
                    myString2 += "2";
                    result.setText(myString2);
                    break;
                case R.id.btn_3:
                    String myString3 = result.getText().toString();
                    myString3 += "3";
                    result.setText(myString3);
                    break;
                case R.id.btn_4:
                    String myString4 = result.getText().toString();
                    myString4 += "4";
                    result.setText(myString4);
                    break;
                case R.id.btn_5:
                    String myString5 = result.getText().toString();
                    myString5 += "5";
                    result.setText(myString5);
                    break;
                case R.id.btn_6:
                    String myString6 = result.getText().toString();
                    myString6 += "6";
                    result.setText(myString6);
                    break;
                case R.id.btn_7:
                    String myString7 = result.getText().toString();
                    myString7 += "7";
                    result.setText(myString7);
                    break;
                case R.id.btn_8:
                    String myString8 = result.getText().toString();
                    myString8 += "8";
                    result.setText(myString8);
                    break;
                case R.id.btn_9:
                    String myString9 = result.getText().toString();
                    myString9 += "9";
                    result.setText(myString9);
                    break;
                case R.id.btn_dot:
                    String myStringDot = result.getText().toString();
                    myStringDot += ".";
                    result.setText(myStringDot);
                    break;
                case R.id.btn_add:             //判断,使用加减乘除的操作符
                    String myStringAdd = result.getText().toString();
                    if (myStringAdd.equals(null)) {
                        return;
                    }
                    num1 = Double.valueOf(myStringAdd);
                    result.setText(null);
                    op = 1;
                    opd = "+";
                    break;
                case R.id.btn_sub:
                    String myStringSub = result.getText().toString();
                    if (myStringSub.equals(null)) {
                        return;
                    }
                    num1 = Double.valueOf(myStringSub);
                    result.setText(null);
                    op = 2;
                    opd = "-";
                    break;
                case R.id.btn_mul:
                    String myStringMul = result.getText().toString();
                    if (myStringMul.equals(null)) {
                        return;
                    }
                    num1 = Double.valueOf(myStringMul);
                    result.setText(null);
                    op = 3;
                    opd = "×";
                    break;
                case R.id.btn_div:
                    String myStringDiv = result.getText().toString();
                    if (myStringDiv.equals(null)) {
                        return;
                    }
                    num1 = Double.valueOf(myStringDiv);
                    result.setText(null);
                    op = 4;
                    opd = "÷";
                    break;
                case R.id.btn_clear:                 //清除,将编辑框文本显示为空
                    result.setText(null);
                    break;
                case R.id.btn_equ:                   //计算,以操作符为判断,选择所需的运算,并将结果输出
                    String myStringEqu = result.getText().toString();
                    if (myStringEqu.equals(null)) {
                        return;
                    }
                    num2 = Double.valueOf(myStringEqu);
                    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:
                            if (num2 == 0)                //除法中分子与分母之分
                                Result = 0;
                            else
                                Result = num1 / num2;
                            break;
                        default:
                            Result = 0;
                            break;
                    }
                    result.setText(Double.toString(num1) + opd + Double.toString(num2) + "=" + Double.toString(Result));    //将结果完整输出
                    break;
                default:
                    break;
            }
        }
    }
}

运行结果:
简易计算器app的制作_第1张图片
简易计算器app的制作_第2张图片
简易计算器app的制作_第3张图片
简易计算器app的制作_第4张图片

你可能感兴趣的:(android,移动开发,android,studio,app,java)