zoukankan      html  css  js  c++  java
  • 简易计算器功能的实现

    ---恢复内容开始---

    简易计算器功能主要是需要实现加,减,乘,除,求余,开根号,求平方。

    界面相对简洁,如图1所示:

    实现的基本代码及其思路:

    第一步,创建安卓工程,将其相关名字补全,注意package的命名规范性

    第二步,在res下的layout下找到activity_main.xml

    添加相应的代码

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <EditText
    12         android:id="@+id/editText1"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:layout_alignParentTop="true"
    16         android:layout_centerHorizontal="true"
    17         android:layout_marginTop="30dp"
    18         android:ems="10"
    19         android:hint="请输入第一个数字:" />
    20 
    21     <EditText
    22         android:id="@+id/editText2"
    23         android:layout_width="wrap_content"
    24         android:layout_height="wrap_content"
    25         android:layout_alignLeft="@+id/editText1"
    26         android:layout_below="@+id/editText1"
    27         android:layout_marginTop="22dp"
    28         android:ems="10"
    29         android:hint="请输入第二个数字:" />
    30 
    31     <EditText
    32         android:id="@+id/editText3"
    33         android:layout_width="wrap_content"
    34         android:layout_height="wrap_content"
    35         android:layout_alignLeft="@+id/editText2"
    36         android:layout_below="@+id/editText2"
    37         android:layout_marginTop="20dp"
    38         android:ems="10"
    39         android:hint="请输入运算符:" >
    40 
    41         <requestFocus />
    42     </EditText>
    43 
    44     <Button
    45         android:id="@+id/button1"
    46         android:layout_width="wrap_content"
    47         android:layout_height="wrap_content"
    48         android:layout_centerHorizontal="true"
    49         android:layout_centerVertical="true"
    50         android:text="计算"
    51         android:onClick="doClick" />
    52 
    53     <TextView
    54         android:id="@+id/textView1"
    55         android:layout_width="wrap_content"
    56         android:layout_height="wrap_content"
    57         android:layout_below="@+id/button1"
    58         android:layout_marginTop="50dp"
    59         android:layout_toLeftOf="@+id/button1"
    60         android:text="计算结果:" />
    61 
    62 </RelativeLayout>
    View Code
     

    ---恢复内容结束---

    简易计算器功能主要是需要实现加,减,乘,除,求余,开根号,求平方。

    界面相对简洁,如图1所示:

    实现的基本代码及其思路:

    第一步,创建安卓工程,将其相关名字补全,注意package的命名规范性

    第二步,在res下的layout下找到activity_main.xml

    添加相应的代码

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <EditText
    12         android:id="@+id/editText1"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:layout_alignParentTop="true"
    16         android:layout_centerHorizontal="true"
    17         android:layout_marginTop="30dp"
    18         android:ems="10"
    19         android:hint="请输入第一个数字:" />
    20 
    21     <EditText
    22         android:id="@+id/editText2"
    23         android:layout_width="wrap_content"
    24         android:layout_height="wrap_content"
    25         android:layout_alignLeft="@+id/editText1"
    26         android:layout_below="@+id/editText1"
    27         android:layout_marginTop="22dp"
    28         android:ems="10"
    29         android:hint="请输入第二个数字:" />
    30 
    31     <EditText
    32         android:id="@+id/editText3"
    33         android:layout_width="wrap_content"
    34         android:layout_height="wrap_content"
    35         android:layout_alignLeft="@+id/editText2"
    36         android:layout_below="@+id/editText2"
    37         android:layout_marginTop="20dp"
    38         android:ems="10"
    39         android:hint="请输入运算符:" >
    40 
    41         <requestFocus />
    42     </EditText>
    43 
    44     <Button
    45         android:id="@+id/button1"
    46         android:layout_width="wrap_content"
    47         android:layout_height="wrap_content"
    48         android:layout_centerHorizontal="true"
    49         android:layout_centerVertical="true"
    50         android:text="计算"
    51         android:onClick="doClick" />
    52 
    53     <TextView
    54         android:id="@+id/textView1"
    55         android:layout_width="wrap_content"
    56         android:layout_height="wrap_content"
    57         android:layout_below="@+id/button1"
    58         android:layout_marginTop="50dp"
    59         android:layout_toLeftOf="@+id/button1"
    60         android:text="计算结果:" />
    61 
    62 </RelativeLayout>
    View Code

     在src下的MainActivity.Java下添加代码

     1 package com.example.caculater;
     2 
     3 import android.os.Bundle;
     4 import android.app.Activity;
     5 import android.view.Menu;
     6 import android.view.View;
     7 import android.widget.EditText;
     8 import android.widget.TextView;
     9 
    10 public class MainActivity extends Activity {
    11     private EditText et_num1;
    12     private EditText et_num2;
    13     private EditText et;
    14     private TextView tv_result;
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20         et_num1 = (EditText) findViewById(R.id.editText1);
    21         et_num2 = (EditText) findViewById(R.id.editText2);
    22         et = (EditText) findViewById(R.id.editText3);
    23         tv_result = (TextView) findViewById(R.id.textView1);
    24     }
    25 
    26     public void doClick(View view) {
    27         String str1 = et_num1.getText().toString();
    28         float number1 = Float.parseFloat(str1);
    29 
    30         String str2 = et_num2.getText().toString();
    31         float number2 = Float.parseFloat(str2);
    32 
    33         String str3 = et.getText().toString();
    34         float result=-1 ;
    35         if ("+".equals(str3)) {
    36             result = number1 + number2;
    37         } else if ("-".endsWith(str3)) {
    38             result = number1 - number2;
    39         } else if ("*".endsWith(str3)) {
    40             result = number1 * number2;
    41         } else if ("/".endsWith(str3)) {
    42             result = number1 / number2;
    43             if (number2 == 0) {
    44                 tv_result.setText("Error!!!");
    45             } else {
    46                 tv_result.setText(number1 + str3 + number2 + "=" + result);
    47             }
    48         } else if ("%".endsWith(str3)) {
    49             result = number1 % number2;
    50             if ( number2 == 0) {
    51                 tv_result.setText("Error!!!");
    52             } else {
    53                 tv_result.setText(number1 + str3 + number2 + "=" + result);
    54             }
    55         } else if ("²".endsWith(str3) || "p".equals(str3)) {
    56             result = (float) Math.pow(number1,2);
    57         } else if ("√".endsWith(str3) || "s".equals(str3)) {
    58             result = (float) Math.sqrt(number1);
    59         }
    60         
    61         
    62         
    63         if ("+".endsWith(str3) || "-".endsWith(str3) || "*".endsWith(str3)) {
    64             tv_result.setText(number1 + str3 + number2 + "=" + result);
    65         }else if ("²".endsWith(str3) || "p".equals(str3)) {
    66             tv_result.setText(number1 + "²" + "=" + result);
    67         } else if ("√".endsWith(str3)||"s".equals(str3) ) {
    68             tv_result.setText("√" + number1 + "=" + result);
    69         }
    70     }
    71 
    72     @Override
    73     public boolean onCreateOptionsMenu(Menu menu) {
    74         // Inflate the menu; this adds items to the action bar if it is present.
    75         getMenuInflater().inflate(R.menu.main, menu);
    76         return true;
    77     }
    78 
    79 }
    MainActivity.Java


    运行

     
  • 相关阅读:
    JAVA基础--线程
    Flutter: SliverAppBar 应用程序栏与滚动视图集成,以便它可以根据滚动偏移量在高度上变化
    Flutter: MediaQuery
    Dart: puppeteer库
    Flutter: 监听App显示,隐藏
    Dart: 解析html字符串
    Dart: 编码和解码各种存档和压缩格式
    Dart: 执行shell命令
    运行Chrome浏览器如何添加Options
    Flutter 删除AppBar的返回icon
  • 原文地址:https://www.cnblogs.com/Vibge/p/6033857.html
Copyright © 2011-2022 走看看