• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

安卓计算器小项目--减法结果为0,求指点,3Q!

public class MainActivity extends Activity implements OnClickListener{


        private Button bt_c,bt_del,bt_chu,bt_cheng,bt_jia,bt_jian,bt_0,bt_1,bt_2,bt_3,
                       bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_dengyu,bt_dian;
        private EditText et_input;
        private boolean clear_flag;
       
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        bt_c=(Button) findViewById(R.id.bt_c);
        bt_del=(Button) findViewById(R.id.bt_del);
        bt_chu=(Button) findViewById(R.id.bt_chu);
        bt_cheng=(Button) findViewById(R.id.bt_cheng);
        bt_jia=(Button) findViewById(R.id.bt_jia);
        bt_jian=(Button) findViewById(R.id.bt_jian);
        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_dengyu=(Button) findViewById(R.id.bt_dengyu);
        bt_dian=(Button) findViewById(R.id.bt_dian);
        
        et_input=(EditText) findViewById(R.id.input);
        
        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_dian.setOnClickListener(this);
        bt_dengyu.setOnClickListener(this);
        bt_c.setOnClickListener(this);
        bt_del.setOnClickListener(this);
        bt_jia.setOnClickListener(this);
        bt_jian.setOnClickListener(this);
        bt_cheng.setOnClickListener(this);
        bt_chu.setOnClickListener(this);
        
        et_input.setOnClickListener(this);
    }
        public void onClick(View v) {
                // TODO Auto-generated method stub
                String str=et_input.getText().toString();//获开屏幕显示的数据
               
                switch (v.getId()) {
                case R.id.bt_0:
                case R.id.bt_1:
                case R.id.bt_2:
                case R.id.bt_3:
                case R.id.bt_4:
                case R.id.bt_5:
                case R.id.bt_6:
                case R.id.bt_7:
                case R.id.bt_8:
                case R.id.bt_9:
                case R.id.bt_dian:
                        if(clear_flag){
                                clear_flag=false;
                                str="";
                                et_input.setText("");
                        }
                       
                        et_input.setText(str+((Button)v).getText());
                        //按那个键就会在屏幕上显示出之前str加上按得那个键值一起显示
                        break;
                case R.id.bt_jia:
                case R.id.bt_jian:
                case R.id.bt_cheng:
                case R.id.bt_chu:
                        if(clear_flag){
                                clear_flag=false;
                                str="";
                                et_input.setText("");
                        }
                        et_input.setText(str+" "+((Button)v).getText()+" ");
                        break;
                case R.id.bt_dengyu:
                        getResult();
                        break;
                case R.id.bt_del:
                        if(clear_flag){
                                clear_flag=false;
                                str="";
                                et_input.setText("");
                        }
                        else if(str!=null&&str!=""){
                                et_input.setText(str.substring(0,str.length()-1));
                                //如果str中不为空并且不是空字符串,就将str减一位显示出来
                        }
                        break;
                case R.id.bt_c:
                        clear_flag=false;
                        str="";
                        et_input.setText("");//按C键就显示空字符串
                        break;


                default:
                        break;
                }
               
        }
        private void getResult(){
               
                String exp=et_input.getText().toString();//获开屏幕显示的数据
                if(exp==null||exp.equals("")){
                        return;
                }
                if(!exp.contains(" ")){
                        return;
                }
                if(clear_flag){
                        clear_flag=false;
                        return;
                }
                clear_flag=true;
                double r=0;
                int space =exp.indexOf(' ');//用于搜索空格位置
            String s1 = exp.substring(0, space);//s1用于保存第一个运算数
            String op = exp.substring(space + 1, space + 2);//op用于保存运算符
            String s2 = exp.substring(space + 3);//s2用于保存第二个运算数
            
            if(!s1.equals("")&&!s2.equals("")){
                   
            double arg1 = Double.parseDouble(s1);//将运算数从string转换为Single
           double arg2 = Double.parseDouble(s2);
        
        if(op.equals("+")){
                r=arg1+arg2;
        }else if (op.equals("-")) {
                        r=arg1-arg2;
                }else if (op.equals("×")) {
                        r=arg1*arg2;
                }else if (op.equals("÷")) {
                        if(arg2==0){
                                r=0;
                        }
                        else{
                                r=arg1/arg2;
                        }
                }
        if(!s1.contains(".")&&!s2.contains(".")){
                int result=(int) r;
                et_input.setText(result+"");//如果都没小数点,就显示整数
        }else {
                et_input.setText(r+"");//否则,已小数点形式显示
                }
         }else if(!s1.equals("")&&s2.equals("")){
                et_input.setText(exp);
        }else if (s1.equals("")&&!s2.equals("")) {
         double arg2 = Double.parseDouble(s2);
        
        if(op.equals("+")){
                r=0+arg2;
        }else if (op.equals("-")) {
                        r=0-arg2;
                }else if (op.equals("×")) {
                        r=0;
                }else if (op.equals("÷")) {
                        r=0;
              }
        if(!s2.contains(".")){
                int result=(int) r;
                et_input.setText(result+"");//如果都没小数点,就显示整数
        }else {
                et_input.setText(r+"");//否则,已小数点形式显示
                }
         
        }else {
                et_input.setText("");
        }
        }
   
}

免责声明:本内容仅代表回答会员见解不代表天盟观点,请谨慎对待。

版权声明:作者保留权利,不代表天盟立场。

使用道具 举报

全部参与1

哥们儿,你确定有问题?我用你代码测试了下,没问题呀。

使用道具 举报

发新帖

发布任务需求已有1031167位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!