主类代码:

package com.leige.calc;




import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.regex.Pattern;


import android.app.Activity;
import android.location.GpsStatus.Listener;
import android.os.Bundle;
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;
import android.widget.TextView;
import android.widget.Toast;




public class MainActivity extends Activity implements OnClickListener{


private static final String Button = null;
TextView show=null;
//声明控件
Button bt_0=null;
Button bt_1=null;
Button bt_2=null;
Button bt_3=null;
Button bt_4=null;
Button bt_5=null;
Button bt_6=null;
Button bt_7=null;
Button bt_8=null;
Button bt_9=null;
Button bt_jia=null;
Button bt_jian=null;
Button bt_cheng=null;
Button bt_chu=null;
Button bt_deng=null;
Button bt_c=null;
Button bt_del=null;
Button bt_point=null;
private String exp="";
private double num1=0;
private String op="";
private double num2=0;
private boolean flag=true;//设置合法点击标志,如果不合法,就设为false
//点击完=,结果未清空,不能点击数字
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //实例化控件
        bt_0=(Button) findViewById(R.id.bt_0);
        bt_1=(Button) findViewById(R.id.bt_1);
        bt_2=(Button) findViewById(R.id.bt_2);
        bt_3=(Button) findViewById(R.id.bt_3);
        bt_4=(Button) findViewById(R.id.bt_4);
        bt_5=(Button) findViewById(R.id.bt_5);
        bt_6=(Button) findViewById(R.id.bt_6);
        bt_7=(Button) findViewById(R.id.bt_7);
        bt_8=(Button) findViewById(R.id.bt_8); 
        bt_9=(Button) findViewById(R.id.bt_9);
        bt_jia=(Button) findViewById(R.id.bt_jia);
        bt_jian=(Button) findViewById(R.id.bt_jian);
        bt_cheng=(Button) findViewById(R.id.bt_cheng);
        bt_chu=(Button) findViewById(R.id.bt_chu);
        bt_deng=(Button) findViewById(R.id.bt_deng);
        bt_point=(Button) findViewById(R.id.bt_point);
        bt_c=(Button) findViewById(R.id.bt_c);
        bt_del=(Button) findViewById(R.id.bt_del);
        show=(TextView) findViewById(R.id.show);
        //注册监听接口
        bt_0.setOnClickListener(this);
        bt_1.setOnClickListener(this);
        bt_2.setOnClickListener(this);
        bt_3.setOnClickListener(this);
        bt_4.setOnClickListener(this);
        bt_5.setOnClickListener(this);
        bt_6.setOnClickListener(this);
        bt_7.setOnClickListener(this);
        bt_8.setOnClickListener(this);
        bt_9.setOnClickListener(this);
        bt_point.setOnClickListener(this);
        OnClickListener listener=new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//点击完操作符后读取第一个操作数
num1=Double.parseDouble(exp);
//记录操作符
op=(String) ((Button)findViewById(v.getId())).getText();
exp+=op;
show.setText(exp);
if(flag==false){
//设置标志,防止用户点完=好直接点击数字如果点击操作符op在设置为合法状态即true
flag=true;
}
}
};
        bt_jia.setOnClickListener(listener);
        bt_jian.setOnClickListener(listener);
        bt_cheng.setOnClickListener(listener);
        bt_chu.setOnClickListener(listener);
        //点击等号时记录第二个操作数,计算开始
        OnClickListener deng=new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
double result;
//读取第二个操作数
num2=Double.parseDouble(
exp.substring(exp.indexOf(op)+1, exp.length()));
if(op.equals("+")){
result=num1+num2;
}else if(op.equals("-")){
result=num1-num2;
}else if(op.equals("*")){
result=num1*num2;
}else{
result=num1/num2;
}
exp=new DecimalFormat("0.0000").format(result)+"";
show.setText(exp);
//设置标志,防止用户点完=好直接点击数字如果点击操作符op在设置为合法状态即true
flag=false;

}
};
        bt_deng.setOnClickListener(deng);
  
        bt_del.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(exp==null||exp.equals("")||exp.length()<=1){
Toast.makeText(MainActivity.this, 
"亲!不要瞎点啊", 1000).show();
}else{

exp=exp.substring(0,exp.length()-1);
show.setText(exp);
}
}
});
        bt_c.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
exp="";
show.setText(exp);

}
});


 
    }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(flag){
exp+=((Button)findViewById(v.getId())).getText();
show.setText(exp);
}else{

//flag位设置合法点击标志,如果不合法,就设为false
//点击完=,结果未清空,不能点击数字,如果直接点击显示错误信息,清空表达式
Toast.makeText(MainActivity.this, 
"亲!要先清空哦!亲", 1000).show();
//重新开始
exp="";
exp+=((Button)findViewById(v.getId())).getText();
show.setText(exp);
flag=true;
}
}


    


}


string.xml

<?xml version="1.0" encoding="utf-8"?>



    Calc
    Hello world!
    Settings
    #ffffff
    #ff0000
    #00ff00
    #0000ff
    #f0f0f0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
     +
    *
    -
    /


    C
    DEL
    .
    0
    =
    磊哥定制版!!威武



布局文件

布局比较挫,没有好好整

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


            android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:enabled="false"
        android:ems="10"
        android:textColor="@color/green"
        android:background="@drawable/calc_show" >


       
   


            
        android:stretchColumns="*"
        android:shrinkColumns="3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        
            >
                      android:id="@+id/tr0"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >


                            android:id="@+id/bt_c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_c" 
                
                />


                            android:id="@+id/bt_del"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_del" />


                            android:id="@+id/bt_jia"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_jia" />


                            android:id="@+id/bt_jian"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_jian" />


       
                    android:id="@+id/tr1"
              android:layout_width="match_parent"
            android:layout_height="match_parent" >


                            android:id="@+id/bt_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_1" />


                            android:id="@+id/bt_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_2" />


                            android:id="@+id/bt_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_3" />


                            android:id="@+id/bt_cheng"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_cheng" 
                />


       


        
        
                          android:id="@+id/tr_2"
             android:layout_width="match_parent"
            android:layout_height="match_parent" >
             >


                            android:id="@+id/bt_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_4" />


                            android:id="@+id/bt_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_5" />


                            android:id="@+id/bt_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_6" />


                            android:id="@+id/bt_chu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_chu" />


       
                                  android:id="@+id/tr_3"
              android:layout_width="match_parent"
            android:layout_height="match_parent" >
                            android:id="@+id/bt_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_7" />


                            android:id="@+id/bt_8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_8" />


                            android:id="@+id/bt_9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:text="@string/bt_9" />


            


       
   
                                  android:id="@+id/tr_4"
     
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
                              android:id="@+id/bt_0"
                android:layout_span="2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_0" />
                            android:id="@+id/bt_point"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_point" />


                    android:id="@+id/bt_deng"


                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/bt_deng" />


       


                                               android:id="@+id/tableRow1"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content" >


                                                       android:id="@+id/imageView1"
                             android:layout_width="180dp"
                             android:layout_height="180dp"
                             android:src="@drawable/lei" />


                                                       android:id="@+id/textView1"
                             android:layout_width="80dp"
                             android:layout_height="80dp"
                             android:textColor="@color/blue"
                             android:text="@string/calc" />


                     
        
   



实验截图

android 计算器,纯手打,功能实现了,bug可能还有点_第1张图片

更多相关文章

  1. Android 对UI操作的工具类UIUtils
  2. android wifi 操作
  3. Android sdcard文件读写操作
  4. Android-SQLite3基本操作指令集合
  5. android按钮的操作例子,简单大家看明白_基础篇
  6. Android 使用ORMLite 操作数据库
  7. 我的android 第15天 -使用SQLiteOpenHelper获取用于操作数据库的
  8. android 创建数字签名应用程序

随机推荐

  1. android textView加载html 解决a标签链接
  2. GPS开发常用方法 和用Criteria确定androi
  3. android Json解析全
  4. android中创建有自身特色的字符串
  5. ButterKnife基本使用
  6. android 调用分享
  7. ssssss
  8. Android Camera怎样打开关闭闪光灯
  9. android listview 与 一般多线程配合产生
  10. Android Audio代码分析2 - 函数getMinBuf