在学习安卓,做个计算器练手。
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">计算器</string>
<string name="app_name">计算器</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="zero">0</string>
<string name="jia">+</string>
<string name="jian">-</string>
<string name="cheng">x</string>
<string name="chu">÷</string>
<string name="dengyu">=</string>
<string name="zhengfu">+/-</string>
<string name="dihao">.</string>
<string name="ping">x²</string>
<string name="kai">√x</string>
<string name="shanchu">清除</string>
<string name="shan">归零</string>
</resources>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="#ff808080"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center_vertical" android:id="@+id/zhengfu">
<EditText
android:text="TextView"
android:id="@+id/textView1"
android:textSize="25sp"
android:textColor="#000000"
android:layout_gravity="center"
android:gravity="left"
android:cursorVisible="false"
android:editable = "false"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:layout_marginTop="4dip" android:layout_weight="1.5">
</EditText>
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dip" android:layout_weight="10">
<LinearLayout
android:id="@+id/linearlayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/button_c"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/shan"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/button_ce"
android:layout_width="50dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/shanchu"
android:textSize="25sp" >
</Button>
</LinearLayout>
<TableRow
android:id="@+id/row0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/one" android:id="@+id/button1">
</Button>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/two" android:id="@+id/button2">
</Button>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/three" android:id="@+id/button3">
</Button>
<Button
android:id="@+id/button_add"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/jia"
android:textSize="25sp" >
</Button>
</TableRow>
<TableRow
android:id="@+id/row1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/four" android:id="@+id/button4">
</Button>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/five" android:id="@+id/button5">
</Button>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/six" android:id="@+id/button6">
</Button>
<Button
android:id="@+id/button_minus"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/jian"
android:textSize="25sp" >
</Button>
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/seven" android:id="@+id/button7">
</Button>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/eight" android:id="@+id/button8">
</Button>
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/nine" android:id="@+id/button9">
</Button>
<Button
android:id="@+id/button_multiply"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/cheng"
android:textSize="25sp" >
</Button>
</TableRow>
<TableRow
android:id="@+id/row3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:textSize="25sp" android:text="@string/zero" android:id="@+id/button0">
</Button>
<Button
android:id="@+id/button_dot"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/dihao"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/button_equals"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/dengyu"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/button_divide"
android:layout_width="40dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/chu"
android:textSize="25sp" >
</Button>
</TableRow>
<LinearLayout
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1">
<Button
android:id="@+id/button_zhengfu"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/zhengfu"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/button_pingfang"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/ping"
android:textSize="25sp" >
</Button>
<Button
android:id="@+id/button_kaifang"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="@string/kai"
android:textSize="25sp" >
</Button>
</LinearLayout>
</TableLayout>
</LinearLayout>
CalculaterActivity.java
package com.fuxing;
import fuxing.calculater.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class CalculaterActivity extends Activity {
String str="";
EditText et;
char ch=' ';//计算符号,+,-,*,/
int flag=0;
double b=0.0,g=0.0,f=0.0;
View vi;
//菜单项退出
public boolean onCreateOptionsMenu(Menu menu){
menu.add(0, 1, 1, "退出");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==1){finish();}
return super.onOptionsItemSelected(item);
}
//计算
public double calculater(){
switch(ch){
case ' ':f=g;break;
case '+':f=b+g;break;
case '-':f=b-g;break;
case '*':f=b*g;break;
case '/':f=b/g;break;
}
b=f;
ch=' ';
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得按键
final Button number[]=new Button[10];
final Button button_add=(Button)findViewById(R.id.button_add);//+
final Button button_minus=(Button)findViewById(R.id.button_minus);//-
final Button button_multiply=(Button)findViewById(R.id.button_multiply);//*
final Button button_divide=(Button)findViewById(R.id.button_divide);//除
final Button button_equals=(Button)findViewById(R.id.button_equals);//等于
final Button button_dot=(Button)findViewById(R.id.button_dot);//点
final Button button_ce=(Button)findViewById(R.id.button_ce);//清除
final Button button_c=(Button)findViewById(R.id.button_c);//归零
final Button button_zhengfu=(Button)findViewById(R.id.button_zhengfu);//正负号
final Button button_kaifang=(Button)findViewById(R.id.button_kaifang);//开方
final Button button_pingfang=(Button)findViewById(R.id.button_pingfang);//平方
//数字键0-9
number[0]=(Button)findViewById(R.id.button0);
number[1]=(Button)findViewById(R.id.button1);
number[2]=(Button)findViewById(R.id.button2);
number[3]=(Button)findViewById(R.id.button3);
number[4]=(Button)findViewById(R.id.button4);
number[5]=(Button)findViewById(R.id.button5);
number[6]=(Button)findViewById(R.id.button6);
number[7]=(Button)findViewById(R.id.button7);
number[8]=(Button)findViewById(R.id.button8);
number[9]=(Button)findViewById(R.id.button9);
et=(EditText) findViewById(R.id.textView1);
et.setText(str);
//数字按键0-9
number[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=0;
et.setText(str);
flag=0;
}
else{
char ch1[];
ch1=str.toCharArray();
if(!(ch1.length==1&&ch1[0]=='0'))
{str+=0;
et.setText(str);}
}
vi=v;
}
});
number[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=1;
et.setText(str);
flag=0;
}
else{
str+=1;
et.setText(str);
}
vi=v;
}
});
number[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=2;
et.setText(str);
flag=0;
}
else{
str+=2;
et.setText(str);
}
vi=v;
}
});
number[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=3;
et.setText(str);
flag=0;
}
else{
str+=3;
et.setText(str);
}
vi=v;
}
});
number[4].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=4;
et.setText(str);
flag=0;
}
else{
str+=4;
et.setText(str);
}
vi=v;
}
});
number[5].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=5;
et.setText(str);
flag=0;
}
else{
str+=5;
et.setText(str);
}
vi=v;
}
});
number[6].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=6;
et.setText(str);
flag=0;
}
else{
str+=6;
et.setText(str);
}
vi=v;
}
});
number[7].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=7;
et.setText(str);
flag=0;
}
else{
str+=7;
et.setText(str);
}
vi=v;
}
});
number[8].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=8;
et.setText(str);
flag=0;
}
else{
str+=8;
et.setText(str);
}
vi=v;
}
});
number[9].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag==1){
str="";
str+=9;
et.setText(str);
flag=0;
}
else{
str+=9;
et.setText(str);
}
vi=v;
}
});
//设定符号键
//加
button_add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==button_add||vi==button_minus||vi==button_multiply||vi==button_divide){
ch='+';
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
ch='+';
flag=1;
vi=v;
}}
}
});
//减
button_minus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==button_add||vi==button_minus||vi==button_multiply||vi==button_divide){
ch='-';
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
ch='-';
flag=1;
vi=v;
}}
}
});
//乘
button_multiply.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==button_add||vi==button_minus||vi==button_multiply||vi==button_divide){
ch='*';
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
ch='*';
flag=1;
vi=v;
}}
}
});
//除
button_divide.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
if(vi==button_add||vi==button_minus||vi==button_multiply||vi==button_divide){
ch='/';
}
else{
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
ch='/';
flag=1;
vi=v;
}}
}
});
//等号
button_equals.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""&&vi!=button_add&&vi!=button_minus&&vi!=button_multiply&&vi!=button_divide){
g=Double.parseDouble(str);
calculater();
str=""+f;
et.setText(str);
flag=1;
vi=v;
}
}
});
//小数点
button_dot.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str==""){
str+="";
et.setText(str);
}
else{
char ch1[];int x=0;
ch1=str.toCharArray();
for(int i=0;i<ch1.length;i++)
if(ch1[i]=='.')
x++;
if(x==0){
str+=".";
et.setText(str);
}
}
}
});
//清除
button_ce.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
str="";
et.setText(str);
vi=v;
}
});
//归零
button_c.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
b=0.0;ch=' ';g=0.0;
str="";
et.setText(str);
}
});
//正负号处理
button_zhengfu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(vi!=button_dot&&str!=""){
char ch=str.charAt(0);
if(ch=='-')
str=str.replace("-","");
else
str="-"+str;
et.setText(str);
}
}
});
//开方
button_kaifang.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
double a=Double.parseDouble(str);
str=Math.sqrt(a)+"";
et.setText(str);
}}
});
//平方
button_pingfang.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(str!=""){
double a=Double.parseDouble(str);
str=""+a*a;
et.setText(str);
}}
});
}
}