首页 > 代码库 > Android简易计算器

Android简易计算器

这是我的第一个andriod小程序,第一次写用了半个月,第二次修改用了一天,第三次修改用了两个小时,现在终于比较满意了。现在我就直接分享一下我的源代码,由于思路比较简单,注释加的不多。采用的是相对布局,但是此布局只适合480*800的分辨率,更高的分辨率界面会显示不正常,但是功能没有任何问题,你可以尝试用其他布局来实现界面,总之核心功能实现是不变的。

activity_main.xml文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.test.MainActivity"    tools:ignore="MergeRootFrame"    android:padding="15dp"    android:layout_gravity="center"     android:background="#111"    >     <Button         android:id="@+id/button4"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button13"         android:layout_alignBottom="@+id/button13"         android:layout_toLeftOf="@+id/button2"         android:background="#a10b39"         android:padding="10dp"         android:text="="         android:textColor="#fff"         android:textSize="28dp"          android:layout_marginRight="3dp"/>     <Button         android:id="@+id/button9"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button10"         android:layout_alignBottom="@+id/button10"         android:layout_alignLeft="@+id/button6"         android:background="#666"         android:padding="10dp"         android:text="7"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"        android:layout_width="70dp"        android:layout_marginBottom="3dp"          />     <Button         android:id="@+id/button11"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button10"         android:layout_alignBottom="@+id/button10"         android:layout_toRightOf="@+id/button13"         android:background="#666"         android:padding="10dp"         android:text="9"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"        android:layout_width="70dp"         android:layout_marginBottom="3dp"/>     <Button         android:id="@+id/button10"         android:layout_height="wrap_content"         android:layout_alignLeft="@+id/button18"         android:layout_below="@+id/button17"         android:background="#666"         android:padding="10dp"         android:text="8"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"        android:layout_width="70dp"        android:layout_marginBottom="3dp"          />     <Button         android:id="@+id/button20"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_above="@+id/button14"         android:layout_alignParentRight="true"         android:background="#a10b39"         android:padding="10dp"         android:text="←"         android:textColor="#fff"         android:textSize="28dp"          android:layout_marginBottom="3dp"/>     <Button         android:id="@+id/button19"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button20"         android:layout_alignBottom="@+id/button20"         android:layout_toLeftOf="@+id/button20"         android:background="#a10b39"         android:padding="10dp"         android:text="CE"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"         android:layout_marginBottom="3dp"          />     <Button         android:id="@+id/button18"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_above="@+id/button10"         android:layout_toLeftOf="@+id/button11"         android:background="#a10b39"         android:padding="10dp"         android:text="±"         android:textColor="#fff"         android:textSize="28dp"               android:layout_marginRight="3dp"        android:layout_marginBottom="3dp"         />     <Button         android:id="@+id/button17"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_below="@+id/editText1"         android:layout_marginTop="5dp"         android:background="#a10b39"         android:padding="10dp"         android:text="√"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"         android:layout_marginBottom="3dp" />     <Button         android:id="@+id/button14"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignParentRight="true"         android:layout_below="@+id/button18"         android:background="#d89218"         android:padding="10dp"         android:text="÷"         android:textColor="#fff"         android:textSize="28dp"        android:layout_marginBottom="3dp" />     <Button         android:id="@+id/button7"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_below="@+id/button10"         android:layout_toRightOf="@+id/button6"         android:background="#666"         android:padding="10dp"         android:text="5"         android:textColor="#fff"         android:textSize="28dp"          android:layout_marginBottom="3dp"         android:layout_marginRight="3dp"/>     <Button         android:id="@+id/button6"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button7"         android:layout_alignBottom="@+id/button7"         android:layout_alignParentLeft="true"         android:background="#666"         android:padding="10dp"         android:text="4"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"        android:layout_marginBottom="3dp" />     <Button         android:id="@+id/button15"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button7"         android:layout_alignBottom="@+id/button7"         android:layout_alignParentRight="true"         android:background="#d89218"         android:padding="10dp"         android:text="×"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginBottom="3dp" />     <Button         android:id="@+id/button8"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button7"         android:layout_alignBottom="@+id/button7"         android:layout_toRightOf="@+id/button10"         android:background="#666"         android:padding="10dp"         android:text="6"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginBottom="3dp"         android:layout_marginRight="3dp" />     <Button         android:id="@+id/button3"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_below="@+id/button7"         android:layout_toRightOf="@+id/button1"         android:background="#666"         android:padding="10dp"         android:text="2"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginBottom="3dp"         android:layout_marginRight="3dp" />     <Button         android:id="@+id/button1"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button3"         android:layout_alignBottom="@+id/button3"         android:layout_alignParentLeft="true"         android:background="#666"         android:padding="10dp"         android:text="1"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginBottom="3dp"         android:layout_marginRight="3dp" />     <Button         android:id="@+id/button5"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button3"         android:layout_alignBottom="@+id/button3"         android:layout_toRightOf="@+id/button7"         android:background="#666"         android:padding="10dp"         android:text="3"         android:textColor="#fff"         android:textSize="28dp"          android:layout_marginBottom="3dp"         android:layout_marginRight="3dp"/>     <Button         android:id="@+id/button16"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button5"         android:layout_alignBottom="@+id/button5"         android:layout_alignLeft="@+id/button15"         android:background="#d89218"         android:padding="10dp"         android:text="-"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginBottom="3dp" />     <Button         android:id="@+id/button2"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button4"         android:layout_alignBottom="@+id/button4"         android:layout_alignParentRight="true"         android:background="#d89218"         android:padding="10dp"         android:text="+"         android:textColor="#fff"         android:textSize="28dp" />     <Button         android:id="@+id/button13"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_below="@+id/button3"         android:layout_toLeftOf="@+id/button4"         android:padding="10dp"         android:text="."         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp"         android:background="#d89218"  />     <Button         android:id="@+id/button12"         android:layout_width="70dp"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/button13"         android:layout_alignBottom="@+id/button13"         android:layout_alignParentLeft="true"         android:background="#666"         android:padding="10dp"         android:text="0"         android:textColor="#fff"         android:textSize="28dp"         android:layout_marginRight="3dp" />     <TextView         android:id="@+id/textView1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignLeft="@+id/editText1"         android:layout_alignParentTop="true"         android:layout_alignRight="@+id/editText1"         android:background="#666"         android:text=" "         android:textColor="#fff"         android:textSize="15dp"         android:textAppearance="?android:attr/textAppearanceLarge" />     <EditText         android:id="@+id/editText1"         android:layout_width="wrap_content"         android:layout_height="40dp"         android:layout_alignLeft="@+id/button17"         android:layout_below="@+id/textView1"         android:background="#666"         android:ems="10"         android:singleLine="true"         android:textColor="#000"         android:textSize="28dp" /></RelativeLayout>

 

MainActivity.java代码如下:

package com.example.mycalculator;import android.app.Activity;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {    private Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17,btn18,btn19,btn20;    private EditText editText;//显示输入的数字    private String opt = "+";//操作符    private double n1 = 0.0, n2 = 0.0;//两个操作数    private TextView textView;//显示算式        //跟据被选择按钮的id设置监听器    private OnClickListener lisenter = new OnClickListener() {                @Override        public void onClick(View v) {            // TODO Auto-generated method stub                        editText = (EditText)findViewById(R.id.editText1);            textView = (TextView) findViewById(R.id.textView1);            String s = editText.getText().toString();//获取字符串            Button btn =(Button)v;            try{                switch(btn.getId())            {                case R.id.button1://1                {                                                        String str = editText.getText().toString();                    editText.setText(str + 1);                    str = editText.getText().toString();                    textView.setText(str);                                    break;                }                case R.id.button2://+                {                    String str = editText.getText().toString();                    n1 = Double.parseDouble(str);                    opt = "+";                                        textView.setText(n1 + opt);                    editText.setText("");                    break;                }                case R.id.button3://2                {                                        String str = editText.getText().toString();                    editText.setText(str + 2);                        str = editText.getText().toString();                    textView.setText(str);                                    break;                }                case R.id.button4://操作符=                {                                        if(opt == "+")                    {                                                String str = editText.getText().toString();                        n2 = Double.parseDouble(str);                        textView.setText(n1 + opt + n2 + "=");                        editText.setText((n1 + n2) + "");                    }                    else if(opt == "-")                    {                        String str = editText.getText().toString();                        n2 = Double.parseDouble(str);                        textView.setText(n1 + opt + n2 + "=");                        editText.setText((n1 - n2) + "");                    }                    else if(opt == "*")                    {                        String str = editText.getText().toString();                        n2 = Double.parseDouble(str);                        textView.setText(n1 + opt + n2 + "=");                        editText.setText((n1 * n2) + "");                    }                    else if(opt == "/")                    {                        String str = editText.getText().toString();                        n2 = Double.parseDouble(str);                        if(n2 == 0)                        {                            editText.setText("");                            textView.setText("除数不能为0");                            break;                        }                                                else                        {                            textView.setText(n1 + opt + n2 + "=");                            editText.setText((n1 / n2) + "");                        }                    }                        break;                }                case R.id.button5://3                {                                        editText.setText(editText.getText().toString() + 3);                    String str = editText.getText().toString();                    textView.setText(str);                        break;                }                case R.id.button6://4                {                    editText.setText(editText.getText().toString() + 4);                    String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button7://5                {                    editText.setText(editText.getText().toString() + 5);                    String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button8://6                {                    editText.setText(editText.getText().toString() + 6);                    String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button9://7                {                    editText.setText(editText.getText().toString() + 7);                    String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button10://8                {                    editText.setText(editText.getText().toString() + 8);                        String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button11://9                {                    editText.setText(editText.getText().toString() + 9);                    String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button12://0                {                                        textView.setText(n1 + opt + 10);                    editText.setText(editText.getText().toString() + 0);                    String str = editText.getText().toString();                    textView.setText(str);                    break;                }                case R.id.button13://.                {                    String str = editText.getText().toString();                    if(str.indexOf(".") != -1) //判断字符串中是否已经包含了小数点,如果有就什么也不做                    {                                            }                     else //如果没有小数点                     {                                if(str.equals("0"))//如果开始为0,                            editText.setText(("0" + ".").toString());                        else if(str.equals(""))//如果初时显示为空,就什么也不做                        {                                                        }                        else                            editText.setText(str + ".");                    }                    break;                }                case R.id.button14://操作符 /                {                                    String str = editText.getText().toString();                    n1 = Double.parseDouble(str);                    opt = "/";                        editText.setText("");                    textView.setText(n1 + opt);                    break;                }                case R.id.button15://操作符*                {                            String str = editText.getText().toString();                    n1 = Double.parseDouble(str);                    opt = "*";                        editText.setText("");                    textView.setText(n1 + opt);                    break;                }                case R.id.button16://操作符-                {                                        String str = editText.getText().toString();                    n1 = Double.parseDouble(str);                    opt = "-";                        editText.setText("");                        textView.setText(n1 + opt);                    break;                }                case R.id.button17://genhao                {                    String str = editText.getText().toString();                    n1 = Double.parseDouble(str);                    editText.setText(Math.sqrt(n1) + "");                    textView.setText(n1 + "的平方根是");                    break;                }                case R.id.button18://+/-                {                    String str =editText.getText().toString();                    n1 = Double.parseDouble(str);                    if(str.length() > 0)                        editText.setText(-n1 + "");                    textView.setText(-n1 + "");                    break;                }                case R.id.button19://CE                {                    String str =editText.getText().toString();                    if(str.length() > 0)                        editText.setText("");                    break;                }                case R.id.button20://<-                    {                    String str =editText.getText().toString();                    if(str.length() > 0)                        editText.setText(str.substring(0, str.length() - 1));                    break;                }                                }            }catch(Exception e){}            }        };         @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (savedInstanceState == null) {            getFragmentManager().beginTransaction()                    .add(R.id.container, new PlaceholderFragment()).commit();        }        //获取按钮的id        btn1 = (Button) findViewById(R.id.button1);        btn2 = (Button) findViewById(R.id.button2);        btn3 = (Button) findViewById(R.id.button3);        btn4 = (Button) findViewById(R.id.button4);        btn5 = (Button) findViewById(R.id.button5);        btn6= (Button) findViewById(R.id.button6);        btn7 = (Button) findViewById(R.id.button7);        btn8 = (Button) findViewById(R.id.button8);        btn9 = (Button) findViewById(R.id.button9);        btn10 = (Button) findViewById(R.id.button10);        btn11 = (Button) findViewById(R.id.button11);        btn12 = (Button) findViewById(R.id.button12);        btn13 = (Button) findViewById(R.id.button13);        btn14 = (Button) findViewById(R.id.button14);        btn15 = (Button) findViewById(R.id.button15);        btn16 = (Button) findViewById(R.id.button16);        btn17 = (Button) findViewById(R.id.button17);        btn18 = (Button) findViewById(R.id.button18);        btn19 = (Button) findViewById(R.id.button19);        btn20 = (Button) findViewById(R.id.button20);    
     //为按钮添加监听器 btn1.setOnClickListener(lisenter); btn2.setOnClickListener(lisenter); btn3.setOnClickListener(lisenter); btn4.setOnClickListener(lisenter); btn5.setOnClickListener(lisenter); btn6.setOnClickListener(lisenter); btn7.setOnClickListener(lisenter); btn8.setOnClickListener(lisenter); btn9.setOnClickListener(lisenter); btn10.setOnClickListener(lisenter); btn11.setOnClickListener(lisenter); btn12.setOnClickListener(lisenter); btn13.setOnClickListener(lisenter); btn14.setOnClickListener(lisenter); btn15.setOnClickListener(lisenter); btn16.setOnClickListener(lisenter); btn17.setOnClickListener(lisenter); btn18.setOnClickListener(lisenter); btn19.setOnClickListener(lisenter); btn20.setOnClickListener(lisenter); } @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } }}

 

 

运行的效果展示:

 

怎么样,效果还是不错吧,有兴趣的话还可以扩展为更加高级的科学计算器。

 

Android简易计算器