Layout布局
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<TextView
android:id="@+id/led"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="70sp"
android:layout_marginTop="10dp"
android:gravity="right"
android:background="#eee"
android:text="0"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/clean"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="10pt"
android:text="Ac"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/num7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="10pt"
android:text="7"/>
<Button
android:id="@+id/num8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="10pt"
android:text="8"/>
<Button
android:id="@+id/num9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="10pt"
android:text="9"/>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="+"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/num4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="10pt"
android:text="4"/>
<Button
android:id="@+id/num5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="10pt"
android:text="5"/>
<Button
android:id="@+id/num6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="10pt"
android:text="6"/>
<Button
android:id="@+id/mine"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="-"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/num1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="10pt" />
<Button
android:id="@+id/num2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="10pt" />
<Button
android:id="@+id/num3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:textSize="10pt" />
<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="x"
android:textSize="10pt" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/num0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"
android:textSize="10pt"/>
<Button
android:id="@+id/point"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="."
android:textSize="10pt" />
<Button
android:id="@+id/equal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="="
android:textSize="10pt" />
<Button
android:id="@+id/dev"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="/"
android:textSize="10pt" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Java代码
package com.example.calculater;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
Button bt0,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,btPoint,btEqual,btAdd,btMine,btMul,btDev,btClean;
String strNumber1="",strNumber2="",strNumber3="";
double num3=0;
int mark=0,emark=0;
String sign="";
boolean flat=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bt0.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"0";
else if(emark==1){
strNumber1="0";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"0";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"1";
else{
strNumber1="1";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"1";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"2";
else{
strNumber1="2";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"2";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt3.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"3";
else{
strNumber1="3";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"3";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt4.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"4";
else{
strNumber1="4";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"4";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt5.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"5";
else{
strNumber1="5";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"5";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt6.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"6";
else{
strNumber1="6";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"6";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt7.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"7";
else{
strNumber1="7";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"7";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt8.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"8";
else{
strNumber1="8";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"8";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
bt9.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(emark==0)
strNumber1=strNumber1+"9";
else{
strNumber1="9";
emark=0;
}
tv.setText(strNumber1);
}
else if(mark==1){
strNumber2=strNumber2+"9";
tv.setText(strNumber1+sign+strNumber2);
}
flat=true;
};
});
btAdd.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(flat){
sign="+";
mark=1;
flat=false;
tv.setText(strNumber1+sign);
}
}
});
btMine.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(flat){
sign="-";
mark=1;
flat=false;
tv.setText(strNumber1+sign);
}
}
});
btMul.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(flat){
sign="x";
mark=1;
flat=false;
tv.setText(strNumber1+sign);
}
}
});
btDev.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(flat){
sign="/";
mark=1;
flat=false;
tv.setText(strNumber1+sign);
}
}
});
btPoint.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(mark==0){
if(strNumber1==""||strNumber1=="0"){
strNumber1="0.";
}
else
strNumber1=strNumber1+".";
tv.setText(strNumber1);
}
else{
if(strNumber2==""||strNumber2=="0")
strNumber2="0.";
else
strNumber2=strNumber2+".";
tv.setText(strNumber1+sign+strNumber2);
}
flat=false;
emark=0;
}
});
btEqual.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(flat){
if(sign.equals("+")){
Double x=Double.parseDouble(strNumber1);
Double y=Double.parseDouble(strNumber2);
num3=x+y;
iszhengxing(num3);
tv.setText(strNumber3);
}
else if(sign.equals("-")){
Double x=Double.parseDouble(strNumber1);
Double y=Double.parseDouble(strNumber2);
num3=x-y;
iszhengxing(num3);
tv.setText(strNumber3);
}
else if(sign.equals("x")){
Double x=Double.parseDouble(strNumber1);
Double y=Double.parseDouble(strNumber2);
num3=x*y;
iszhengxing(num3);
tv.setText(strNumber3);
}
else if(sign.equals("/")){
double x=Double.parseDouble(strNumber1);
double y=Double.parseDouble(strNumber2);
num3=x/y;
iszhengxing(num3);
tv.setText(strNumber3);
}
else if(sign==""){
double x=Double.parseDouble(strNumber1);
num3=x;
iszhengxing(num3);
tv.setText(strNumber3);
}
strNumber1=strNumber3;
strNumber2="";
strNumber3="";
sign="";
flat=true;
num3=0;
mark=0;
emark=1;
}
}
});
btClean.setOnClickListener(new OnClickListener(){
public void onClick(View v){
strNumber1="";
strNumber2="";
strNumber3="";
mark=0;
emark=1;
num3=0;
sign="";
flat=false;
tv.setText("0");
}
});
}
private void init(){
bt0=(Button)findViewById(R.id.num0);
bt1=(Button)findViewById(R.id.num1);
bt2=(Button)findViewById(R.id.num2);
bt3=(Button)findViewById(R.id.num3);
bt4=(Button)findViewById(R.id.num4);
bt5=(Button)findViewById(R.id.num5);
bt6=(Button)findViewById(R.id.num6);
bt7=(Button)findViewById(R.id.num7);
bt8=(Button)findViewById(R.id.num8);
bt9=(Button)findViewById(R.id.num9);
btPoint=(Button)findViewById(R.id.point);
btEqual=(Button)findViewById(R.id.equal);
btAdd=(Button)findViewById(R.id.add);
btMine=(Button)findViewById(R.id.mine);
btMul=(Button)findViewById(R.id.mul);
btDev=(Button)findViewById(R.id.dev);
btClean=(Button)findViewById(R.id.clean);
tv=(TextView)findViewById(R.id.led);
}
public void iszhengxing(double num){
int number=(int)num;
double y=num-number;
if(y==0){
int num33=(int)num3;
strNumber3=String.valueOf(num33);
}
else strNumber3=String.valueOf(num3);
}
}
效果图