废话不多说,下图是计算器截图(布局是可以更改的):

下面是我创建计算器项目的代码目录结构:


下面分享的是activity_main.xml中的源代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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.dujiang.calculatordemo.MainActivity">

   <EditText
       android:layout_width="fill_parent"
       android:layout_height="80dip"
       android:id="@+id/et_input"
       android:editable="false"
       android:gravity="right|bottom"
       android:background="@drawable/edit_bg"
       />

   <LinearLayout
       android:layout_marginTop="10dp"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:orientation="horizontal"
       android:layout_below="@+id/et_input"
       android:layout_alignParentStart="true"
       android:weightSum="1">

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="清空"
          android:textSize="20sp"
          android:background="@drawable/white_selecter"
          android:id="@+id/btn_clear"
          android:layout_weight="0.54" />

      <Button
          android:layout_width="60dp"
          android:background="@drawable/white_selecter"
          android:layout_height="74dp"
          android:text="删除"
          android:textSize="20sp"
          android:id="@+id/btn_del"
          android:layout_weight="0.46" />
      <Button
          android:layout_width="59dp"
          android:background="@drawable/white_selecter"
          android:layout_height="74dp"
          android:text="/"
          android:textSize="20sp"
          android:id="@+id/btn_divide"
          />

      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="*"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_multiply"
          />


   </LinearLayout>
   //第二行开始*******************************
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:layout_marginTop="80dp"
       android:orientation="horizontal"
       android:layout_below="@+id/et_input"
       android:layout_alignParentStart="true"
       android:weightSum="1">

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="7"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_7"
          android:layout_weight="0.54" />

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="8"
          android:textSize="20sp"
          android:background="@drawable/white_selecter"
          android:id="@+id/btn_8"
          android:layout_weight="0.46" />

      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="9"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_9"
          />

      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="-"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_minus"
          />


   </LinearLayout>
   //第三行开始********************************
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:layout_marginTop="150dp"
       android:orientation="horizontal"
       android:layout_below="@+id/et_input"
       android:layout_alignParentStart="true"
       android:weightSum="1">

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="4"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_4"
          android:layout_weight="0.54" />

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="5"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_5"
          android:layout_weight="0.46" />
      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="6"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_6"
          />

      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="+"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_plus"
          />


   </LinearLayout>
   //第四行***************************************************************
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:layout_marginTop="220dp"
       android:orientation="horizontal"
       android:layout_below="@+id/et_input"
       android:layout_alignParentStart="true"
       android:weightSum="1">

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="1"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_1"
          android:layout_weight="0.54" />

      <Button
          android:layout_width="60dp"
          android:layout_height="74dp"
          android:text="2"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_2"
          android:layout_weight="0.46" />
      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="3"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_3"
          />

      <Button
          android:layout_width="59dp"
          android:layout_height="74dp"
          android:text="0"
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_0"
          />
   </LinearLayout>
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:layout_marginTop="290dp"
       android:orientation="horizontal"
       android:layout_below="@+id/et_input"
       android:layout_alignParentStart="true"
       android:weightSum="1">

      <Button
          android:layout_width="95dp"
          android:layout_height="74dp"
          android:text="."
          android:background="@drawable/white_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_point"
          android:layout_weight="0.71" />

      <Button
          android:layout_width="223dp"
          android:layout_height="74dp"
          android:text="="
          android:background="@drawable/orange_selecter"
          android:textSize="20sp"
          android:id="@+id/btn_equal"
          android:layout_weight="0.24" />
   </LinearLayout>


</RelativeLayout>

上述代码是Android的布局文件,而布局的颜色我在res>drawable中创建(上面有截图自己队长照着设置就好了),下面是后端MainActivity.java文件:

package com.example.dujiang.calculatordemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_0 ;
    Button btn_1;
    Button btn_2;
    Button btn_3 ;
    Button btn_4 ;
    Button btn_5 ;
    Button btn_6 ;  //数字按钮
    Button btn_7 ;
    Button btn_8 ;
    Button btn_9 ;
    Button btn_point ;  //小数点按钮
    Button btn_clear ;
    Button btn_del ;
    Button btn_pluse ;
    Button btn_minus ;
    Button btn_multiply ;
    Button btn_divide ;
    Button btn_equle ;
    EditText et_input ;
    boolean clear_flag ;//清空标识

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_0 = (Button) findViewById(R.id.btn_0) ;
        btn_1 = (Button) findViewById(R.id.btn_1) ;
        btn_2 = (Button) findViewById(R.id.btn_2) ;
        btn_3 = (Button) findViewById(R.id.btn_3) ;
        btn_4 = (Button) findViewById(R.id.btn_4) ;
        btn_5 = (Button) findViewById(R.id.btn_5) ;
        btn_6 = (Button) findViewById(R.id.btn_6) ;
        btn_7 = (Button) findViewById(R.id.btn_7) ;
        btn_8 = (Button) findViewById(R.id.btn_8) ;
        btn_9 = (Button) findViewById(R.id.btn_9) ;
        btn_point = (Button) findViewById(R.id.btn_point) ;
        btn_clear = (Button) findViewById(R.id.btn_clear) ;
        btn_del = (Button) findViewById(R.id.btn_del) ;
        btn_pluse = (Button) findViewById(R.id.btn_pluse) ;
        btn_minus = (Button) findViewById(R.id.btn_minus) ;
        btn_multiply = (Button) findViewById(R.id.btn_multiply) ;
        btn_divide = (Button) findViewById(R.id.btn_divide) ;
        btn_equle = (Button) findViewById(R.id.btn_equal) ;
//以上实例化按钮
        et_input = (EditText) findViewById(R.id.et_input);  //实例化之后的显示屏

        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_del.setOnClickListener(this);
        btn_pluse.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_equle.setOnClickListener(this);
        //设置以上按钮的点击事件


    }

    @Override
    public void onClick(View v) {
        String str = et_input.getText().toString();
        switch (v.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                if (clear_flag) {
                    clear_flag =false ;
                    str ="" ;
                    et_input.setText("");
                }
                et_input.setText(str + ((Button)v).getText());
                break ;
            case R.id.btn_pluse:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                if (clear_flag) {
                    clear_flag =false ;
                    str ="" ;
                    et_input.setText("");
                }
                et_input.setText(str+ " " + ((Button)v).getText()+" ");
                break;
            case R.id.btn_del:
                if (clear_flag) {
                    clear_flag =false ;
                    str ="" ;
                    et_input.setText("");
                }else if (str!=null&&!str.equals("")){
                    et_input.setText(str.substring(0,str.length()-1));
                }
                break;
            case R.id.btn_clear:
                clear_flag =false ;
                str ="" ;
                et_input.setText("");
            case R.id.btn_equal:
                getResult();
                break ;

        }
    }
   /* 单独的调用运算结果
   *
   *
   * */
    private void getResult(){
        String exp = et_input.getText().toString();
        if (exp == null||exp.equals("")){
            return;
        }
        if(!exp.contains(" ")) {
           return;
        }
        if (clear_flag){
            clear_flag = false ;
            return;

        }
        clear_flag = true ;
        double result = 0 ;
        String s1 = exp.substring(0,exp.indexOf(" ")); //运算符前面的字符串
        String op = exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2) ;
        String s2 = exp.substring(exp.indexOf(" ")+3) ;
        if (!s1.equals(" ")&&!s2.equals(" ")){
            double d1 = Double.parseDouble(s1) ;
            double d2 = Double.parseDouble(s2) ;
            if (op.equals("+")){
                result = d1 + d2 ;

            }else  if (op.equals("-")){
                result = d1 - d2 ;

            }else  if (op.equals("*")){
                result = d1 * d2 ;

            }else  if (op.equals("/")){
                if(d2 == 0){
                    result = 0 ;
                }else {
                    result = d1/d2 ;
                }
            }
            if (s1.contains(".")&&s2.contains(".")) {
                int r = (int) result;
                et_input.setText(r+"");
            }else {
                et_input.setText(result+"");

            }
        }else if (!s1.equals("")&&s2.equals("")){
            et_input.setText(exp);
        }else if (s1.equals("")&&!s2.equals("")){
            double d2 = Double.parseDouble(s2) ;
            if (op.equals("+")){
                result = 0 + d2 ;

            }else  if (op.equals("-")){
                result = 0 - d2 ;

            }else  if (op.equals("*")){
                result = 0 ;

            }else  if (op.equals("/")){
               result = 0 ;
            }
            if (s2.contains(".")) {
                int r = (int) result;
                et_input.setText(r+"");
            }else {
                et_input.setText(result+"");
            }
        }else {
            et_input.setText("");

        }
    }
}


最后注意AndroidMainfest.xml中的相关设置,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dujiang.calculatordemo">

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action

                    android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局可以随意更改,只要对应上 android:id=""   就万事大吉!

很适合练练手感和脑力的小程序,有一些小细节,就靠各位在练习中总结了,我就不一一赘述了,感谢各位看客!!!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐