android的简单计算器的实现

时间:2022-12-29 14:53:12

终于放假了,不过是没有暑假的暑假了。

好久以前就就有接触过安卓,过了这么久还是没有相当于没有入门状态。昨天有空又做了一个简单的计算器练一下手,如有大神看到请绕行了,因为代码实在写得比较烂。嘿嘿。废话不说了,先上图:

    android的简单计算器的实现      android的简单计算器的实现      android的简单计算器的实现      android的简单计算器的实现

首先看一下这个计算器的结构很简单,包含两各Activity:一个是标准类型(标准Activity),只包含简单的加减乘除,另一个加上了更多的功能(科学Activity),如sin,cos等。

看一下标准Activity的布局:

<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=".MainActivity" >

<TextView
android:id="@+id/textview"
android:layout_width="337dp"
android:layout_height="50dp"
android:gravity="right|bottom"
android:textSize="30dp" />

<EditText
android:id="@+id/edittext1"
android:layout_width="355dp"
android:layout_height="70dp"
android:inputType="textMultiLine"
android:gravity="right|bottom"
android:minLines="2"
android:textSize="40dp"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="90dp" >

<Button
android:id="@+id/qiehuan"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:text="标准"
android:textSize="15dp"
android:background="@drawable/btneffect" />

</RelativeLayout>

<RelativeLayout
android:layout_width="360dp"
android:layout_height="80dp" >

<Button
android:id="@+id/del"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="93dp"
android:background="@drawable/btneffect2"
android:text="DEL" />

<Button
android:id="@+id/cheng"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="182dp"
android:background="@drawable/btneffect2"
android:text="×" />

<Button
android:id="@+id/clear"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="4dp"
android:background="@drawable/btneffect1"
android:text="C" />

<Button
android:id="@+id/chu"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/cheng"
android:layout_marginLeft="3dp"
android:background="@drawable/btneffect2"
android:text="÷" />

</RelativeLayout>

<RelativeLayout
android:layout_width="360dp"
android:layout_height="80dp"
android:orientation="horizontal" >

<Button
android:id="@+id/btn7"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="4dp"
android:background="@drawable/btneffect"
android:text="7" />

<Button
android:id="@+id/btn9"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="182dp"
android:background="@drawable/btneffect"
android:text="9" />

<Button
android:id="@+id/btn8"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="93dp"
android:background="@drawable/btneffect"
android:text="8" />

<Button
android:id="@+id/jian"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_toRightOf="@+id/btn9"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:background="@drawable/btneffect2"
android:text="-" />

</RelativeLayout>

<RelativeLayout
android:layout_width="354dp"
android:layout_height="80dp"
android:orientation="horizontal" >

<Button
android:id="@+id/btn4"
android:layout_width="85dp"
android:layout_height="75dp"
android:text="4"
android:layout_marginLeft="4dp"
android:background="@drawable/btneffect"/>

<Button
android:id="@+id/btn6"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="182dp"
android:background="@drawable/btneffect"
android:text="6" />

<Button
android:id="@+id/btn5"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="93dp"
android:background="@drawable/btneffect"
android:text="5" />

<Button
android:id="@+id/jia"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_toRightOf="@+id/btn6"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:background="@drawable/btneffect2"
android:text="+" />

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:id="@+id/btn1"
android:layout_width="85dp"
android:layout_height="75dp"
android:text="1"
android:layout_marginLeft="3dp"
android:background="@drawable/btneffect" />

<Button
android:id="@+id/deng"
android:layout_width="85dp"
android:layout_height="155dp"
android:layout_toRightOf="@+id/btn3"
android:layout_alignParentTop="true"
android:layout_marginLeft="3dp"
android:text="="
android:background="@drawable/btneffect1"/>

<Button
android:id="@+id/btn3"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_marginLeft="182dp"
android:text="3"
android:background="@drawable/btneffect" />

<Button
android:id="@+id/btn2"
android:layout_width="85dp"
android:layout_height="75dp"
android:text="2"
android:layout_marginLeft="93dp"
android:background="@drawable/btneffect"/>

<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/btn2"
android:layout_below="@+id/btn1"
android:text="0"
android:layout_marginLeft="3dp"
android:layout_marginTop="5dp"
android:background="@drawable/btneffect" />

<Button
android:id="@+id/dian"
android:layout_width="85dp"
android:layout_height="75dp"
android:layout_alignLeft="@+id/btn3"

android:layout_alignTop="@+id/btn0"
android:text="."
android:background="@drawable/btneffect" />

</RelativeLayout>
</LinearLayout>

MainActivity.java如下:

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private long exitTime = 0;
protected static final float NaN = 0;
private Button[] btnNum = new Button[11];// 数值按钮
private Button clear,del,cheng,chu,jian,jia,deng,qiehuan;
private EditText edittext;
private TextView textview;
int flag;
String num1,nan="NaN",max="∞",zero="0";
float result,zo=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

btnNum[0] = (Button) findViewById(R.id.btn0);
btnNum[1] = (Button) findViewById(R.id.btn1);
btnNum[2] = (Button) findViewById(R.id.btn2);
btnNum[3] = (Button) findViewById(R.id.btn3);
btnNum[4] = (Button) findViewById(R.id.btn4);
btnNum[5] = (Button) findViewById(R.id.btn5);
btnNum[6] = (Button) findViewById(R.id.btn6);
btnNum[7] = (Button) findViewById(R.id.btn7);
btnNum[8] = (Button) findViewById(R.id.btn8);
btnNum[9] = (Button) findViewById(R.id.btn9);
btnNum[10] = (Button) findViewById(R.id.dian);
qiehuan = (Button) findViewById(R.id.qiehuan);
edittext = (EditText) findViewById(R.id.edittext1);
clear = (Button) findViewById(R.id.clear);
del = (Button) findViewById(R.id.del);
cheng = (Button) findViewById(R.id.cheng);
chu = (Button) findViewById(R.id.chu);
jian = (Button) findViewById(R.id.jian);
jia = (Button) findViewById(R.id.jia);
deng = (Button) findViewById(R.id.deng);
textview = (TextView) findViewById(R.id.textview);

edittext.setCursorVisible(false); //隐藏光标
edittext.setInputType(InputType.TYPE_NULL); // 关闭软键盘
edittext.setText("0"); //初始值

for(int i=0;i<=10;i++){//数字输入显示
btnNum[i].setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button btn = (Button) v;
String input = btn.getText().toString();
String edittext_Value = edittext.getText().toString();
String textview_Value = textview.getText().toString();
if( edittext_Value.equals(nan) || edittext_Value.equals(max) || edittext_Value.equals(zero)){
edittext.setText("");
}
edittext.setText(edittext.getText().toString() + input);// 设置显示区域的值
}
});
}

clear.setOnClickListener(new OnClickListener() { //清除键

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edittext.setText("0");
textview.setText("");
}
});

del.setOnClickListener(new OnClickListener() {//删除键

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String show = edittext.getText().toString();
int L = show.length();
if(L == 0){}
else if(L != 0){
edittext.setText(show.substring(0, L-1));
}

}
});

jia.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
flag=1;
num1 = edittext.getText().toString();
edittext.setText(edittext.getText().toString() + "+");
}
});

jian.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
flag=2;
num1 = edittext.getText().toString();
edittext.setText(edittext.getText().toString() + "-");
}
});

cheng.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
flag=3;
num1 = edittext.getText().toString();
edittext.setText(edittext.getText().toString() + "×");
}
});

chu.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
flag=4;
num1 = edittext.getText().toString();
edittext.setText(edittext.getText().toString() + "÷");
}
});

/*public void dengdown(){
String num = edittext.getText().toString();
if (num.indexOf("+") <= 0 && num.indexOf("-") <= 0 && num.indexOf("×") <= 0 && num.indexOf("÷") <= 0)
{
flag=5;
}

}*/

deng.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String num = edittext.getText().toString();
String num2 = num.substring(num1.length()+1,num.length());
float numa = Float.parseFloat(num1);
float numb = Float.parseFloat(num2);
if(num.length()>0) {System.out.println("11111111");}

switch (flag) {
case 1:result = numa+numb;
break;
case 2:result = numa-numb;
break;
case 3:result = numa*numb;
break;
case 4:if(numa == 0 && numb == 0){
edittext.setText(nan);
}else if (numb == 0){
edittext.setText(max);
}
else result = numa/numb;
break;
/*case 5:*/
default://dengdown();
break;
}
textview.setText(num+"=");
int re = (int)result;
float xiaoshu = result-re;
if(xiaoshu == zo){
edittext.setText(String.valueOf(re));
}else
edittext.setText(String.valueOf(result));

}
});

qiehuan.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent it = new Intent(MainActivity.this,ScienceActivity.class);
startActivity(it);
}
});


}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
if((System.currentTimeMillis()-exitTime) > 2000){
Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}

@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;
}

}


 

为了给Button加入以下效果使界面更美观,又写了三个XML文件:

android的简单计算器的实现

其中btneffect.xml如下,其他两个类似,如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false">
<shape >
<gradient android:startColor="#FAF0E6" android:endColor="#FAF0E6"
android:angle="270" />
<stroke android:width="1dip" android:color="#696969" />
<corners android:radius="10dp" />
<padding android:left="1dp" android:top="1dp"
android:right="1dp" android:bottom="1dp" />
</shape>
</item>

<item android:state_pressed="true">
<shape >
<gradient android:startColor="#DCDCDC" android:endColor="#DCDCDC"
android:angle="270" />
<stroke android:width="1dip" android:color="#000000" />
<corners android:radius="10dp" />
<padding android:left="1dp" android:top="1dp"
android:right="1dp" android:bottom="1dp" />
</shape>
</item>
</selector>


这就是简单计算器基本实现了,关于另外一个Activity的实现和这个基本类似,就不贴代码了。如有哪里写得不好或者有更好的想法,希望大家指正,本文仅作为新手们的交流。android的简单计算器的实现android的简单计算器的实现