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

    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"
        tools:context="com.example.app.MainActivity" >
        <EditText 
            android:id="@+id/editText_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:editable="false"
            android:gravity="right"
            />
        <Button 
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="删除"
            android:textSize="20sp"
            />
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
         >
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center_horizontal"
              >
                <Button 
                    android:id="@+id/btn_7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="7"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="8"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="9"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/divide"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="/"
                    android:textSize="20sp"
                    />
            </LinearLayout>
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center_horizontal"
              >
                <Button 
                    android:id="@+id/btn_4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="5"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="6"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/multiply"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="*"
                    android:textSize="20sp"
                    />
            </LinearLayout>
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center_horizontal"
              >
                <Button 
                    android:id="@+id/btn_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="3"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/del"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="-"
                    android:textSize="20sp"
                    />
            </LinearLayout>
            <LinearLayout 
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center_horizontal"
              >
                <Button 
                    android:id="@+id/point"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="."
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/btn_0"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/equals"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="="
                    android:textSize="20sp"
                    />
                <Button 
                    android:id="@+id/add"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="+"
                    android:textSize="20sp"
                    />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    
    
    


    源程序:
    package
    com.example.app; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener{ private Button delete; private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Button btn_5; private Button btn_6; private Button btn_7; private Button btn_8; private Button btn_9; private Button point; private Button divide; private Button multiply; private Button add; private Button del; private EditText editText; private Button equals; private Button btn_0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText_1); delete = (Button) findViewById(R.id.delete); 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); point = (Button) findViewById(R.id.point); divide = (Button) findViewById(R.id.divide); equals = (Button) findViewById(R.id.equals); multiply = (Button) findViewById(R.id.multiply); add = (Button) findViewById(R.id.add); del = (Button) findViewById(R.id.del); 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); add.setOnClickListener(this); delete.setOnClickListener(this); del.setOnClickListener(this); multiply.setOnClickListener(this); equals.setOnClickListener(this); divide.setOnClickListener(this); point.setOnClickListener(this); } @Override public void onClick(View v) { String text = editText.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.point: editText.setText(text+((Button) v).getText()); break; case R.id.add: case R.id.del: case R.id.multiply: case R.id.divide: editText.setText(text+" "+((Button) v).getText()+" "); break; case R.id.delete: if(text!=null&&!text.equals("")) editText.setText(text.substring(0, text.length()-1)); break; case R.id.equals: getResult(); break; } } private void getResult(){ String text = editText.getText().toString(); if(text==null||text.equals(" ")){ return; } if(!text.contains(" ")){ return; } double result; String s1 = text.substring(0, text.indexOf(" ")); String oprate = text.substring(text.indexOf(" ")+1, text.indexOf(" ")+2); String s2 = text.substring(text.indexOf(" ")+3); if(!s1.equals("")&&!s2.equals("")){ double d1 = Double.parseDouble(s1); double d2 = Double.parseDouble(s2); if(d1==0){ if(oprate.equals("+")){ result = d2; if(!s1.contains(".")&&!s2.contains(".")){ editText.setText((int)result+""); } editText.setText(result+""); } if(oprate.equals("*")){ result = 0; editText.setText(result+""); } if(oprate.equals("/")){ result = 0; editText.setText(result+""); } if(oprate.equals("-")){ result = 0-d2; editText.setText(result+""); } } if(oprate.equals("+")){ result = d1+d2; if(!s1.contains(".")&&!s2.contains(".")){ editText.setText((int)result+""); } editText.setText(result+""); } if(oprate.equals("*")){ result = d1*d2; editText.setText(result+""); } if(oprate.equals("/")){ result = d1/d2; editText.setText(result+""); } if(oprate.equals("-")){ result = d1-d2; editText.setText(result+""); } } } }
  • 相关阅读:
    Oracle 基本命令
    一个完整的创建用户,创建表空间,授权建表过程
    jQueryMobile之Popup
    data-theme 几种值的样式
    jQueryMobile之listview
    jQueryMobile之弹出对话框
    android EditText内嵌图片
    css 盒子模型
    kms可用激活服务器地址|kms可用激活服务器分享
    本地配置DNS服务器(MAC版)
  • 原文地址:https://www.cnblogs.com/fangg/p/5434611.html
Copyright © 2011-2022 走看看