android应用-android下的计算器中的退格功能实现不了,麻烦各位大神啦,谢谢!!

问题描述

android下的计算器中的退格功能实现不了,麻烦各位大神啦,谢谢!!

private EditText output = null;

private EditText input = null;

private Button btn0 = null; private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; private Button btn5 = null; private Button btn6 = null; private Button btn7 = null; private Button btn8 = null; private Button btn9 = null; private Button btnadd = null; private Button btnsubtract = null; private Button btnmultiply = null; private Button btndivide = null; private Button btnclear = null; private Button btnresult = null; private Button btndot = null; private Button btnbackspace = null; private Button btnkaifang = null; private Button btnpingfang = null; private String str = "";// 保存数字 private String strold = "";// 原数字 private char act = ' ';// 记录“加减乘除等于”符号 private int count = 0;// 判断要计算的次数,如果超过一个符号,先算出来一部分 private Double result = 0.0;// 计算的输出结果 private Boolean errBoolean = false;// 有错误的时候为true,无错为false private Boolean flagBoolean = false;// 一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息 private Boolean flagDot = false; // 小数点标志位 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); output = (EditText) rootView.findViewById(R.id.output); input = (EditText) rootView.findViewById(R.id.input); btn0 = (Button) rootView.findViewById(R.id.zero); btn1 = (Button) rootView.findViewById(R.id.one); btn2 = (Button) rootView.findViewById(R.id.two); btn3 = (Button) rootView.findViewById(R.id.three); btn4 = (Button) rootView.findViewById(R.id.four); btn5 = (Button) rootView.findViewById(R.id.five); btn6 = (Button) rootView.findViewById(R.id.six); btn7 = (Button) rootView.findViewById(R.id.seven); btn8 = (Button) rootView.findViewById(R.id.eight); btn9 = (Button) rootView.findViewById(R.id.nine); btnadd = (Button) rootView.findViewById(R.id.add); btnsubtract = (Button) rootView.findViewById(R.id.subtract); btnmultiply = (Button) rootView.findViewById(R.id.multiply); btndivide = (Button) rootView.findViewById(R.id.divide); btnclear = (Button) rootView.findViewById(R.id.clear); btnresult = (Button) rootView.findViewById(R.id.result); btndot = (Button) rootView.findViewById(R.id.dot); btnbackspace = (Button) rootView.findViewById(R.id.zhengfu); btnkaifang = (Button) rootView.findViewById(R.id.kaifang); btnpingfang = (Button) rootView.findViewById(R.id.pingfang); // 设置按钮侦听事件 btn0.setOnClickListener(new BtnOnClickListener()); btn1.setOnClickListener(new BtnOnClickListener()); btn2.setOnClickListener(new BtnOnClickListener()); btn3.setOnClickListener(new BtnOnClickListener()); btn4.setOnClickListener(new BtnOnClickListener()); btn5.setOnClickListener(new BtnOnClickListener()); btn6.setOnClickListener(new BtnOnClickListener()); btn7.setOnClickListener(new BtnOnClickListener()); btn8.setOnClickListener(new BtnOnClickListener()); btn9.setOnClickListener(new BtnOnClickListener()); // 执行运算 btnadd.setOnClickListener(new BtnOnClickListener()); btnsubtract.setOnClickListener(new BtnOnClickListener()); btnmultiply.setOnClickListener(new BtnOnClickListener()); btndivide.setOnClickListener(new BtnOnClickListener()); btnclear.setOnClickListener(new BtnOnClickListener()); btnresult.setOnClickListener(new BtnOnClickListener()); btndot.setOnClickListener(new BtnOnClickListener()); btnbackspace.setOnClickListener(new BtnOnClickListener()); btnkaifang.setOnClickListener(new BtnOnClickListener()); btnpingfang.setOnClickListener(new BtnOnClickListener()); return rootView; } class BtnOnClickListener implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { // 输入数字 case R.id.zero: num(0); break; case R.id.one: num(1); break; case R.id.two: num(2); break; case R.id.three: num(3); break; case R.id.four: num(4); break; case R.id.five: num(5); break; case R.id.six: num(6); break; case R.id.seven: num(7); break; case R.id.eight: num(8); break; case R.id.nine: num(9); break; case R.id.dot: dot(); break; // 执行运算 // 退格功能 case R.id.zhengfu: backspace(); break; case R.id.kaifang: kaifang(); break; case R.id.pingfang: pingfang(); break; case R.id.add: add(); break; case R.id.subtract: sub(); break; case R.id.multiply: multiply(); break; case R.id.divide: divide(); break; case R.id.clear: clear(); break; // 计算结果 case R.id.result: result(); if (!errBoolean && flagBoolean) { output.setText(String.valueOf(result)); } default: break; } // if(strold==""&&act==' '&&str=="") // { // input.setText(""); // } // else { // input.setText(strold+act+str); // } if (act == 'k') { input.setText(strold); } else { input.setText(strold + act + str); } if (act == 'p') { input.setText(strold); } else { input.setText(strold + act + str); } output.setText(String.valueOf(result)); } private void kaifang() { // TODO Auto-generated method stub if (flagBoolean) { act = 'k'; strold = str; // str=""; result(); } } private void pingfang() { // TODO Auto-generated method stub if (flagBoolean) { act = 'p'; strold = str; // str=""; result(); } } //退格功能 private void backspace() { // TODO Auto-generated method stub String str1=input.getText().toString(); if(input.length()>0){ input.setText(str1.substring(0, str1.length()-1)); } } private void dot() { // TODO Auto-generated method stub if (!flagDot) { str = str + "."; flagBoolean = false; flagDot = true; } // str=str+i; // flagBoolean=false; } //清空 private void clear() { // TODO Auto-generated method stub str = strold = ""; count = 0; act = ' '; result = 0.0; flagBoolean = false; flagDot = false; input.setText(strold + act + str); output.setText(""); } private void divide() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '/'; flagBoolean = false; } } private void multiply() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '*'; flagBoolean = false; } } private void sub() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '-'; flagBoolean = false; } } private void add() { // TODO Auto-generated method stub if (flagBoolean) { check(); act = '+'; flagBoolean = false; } } private void check() { // TODO Auto-generated method stub if (count >= 1) { result(); str = String.valueOf(result); } strold = str; str = ""; count++; flagDot = false; } // 计算输出结果 private void result() { // TODO Auto-generated method stub if (flagBoolean) { Double a, b; // if(act=='k'||act=='c') // { // a=Double.parseDouble(strold); // } // else { // a=Double.parseDouble(strold); // b=Double.parseDouble(str); // } a = Double.parseDouble(strold); b = Double.parseDouble(str); if (b == 0 && act == '/') { clear(); output.setText("除数不能为零!"); errBoolean = true; } if (!errBoolean) { switch (act) { case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; case 'k': result = Math.sqrt(a); break; case 'p': result = Math.pow(a,2); break; default: break; } } } } private void num(int i) { // TODO Auto-generated method stub str = str + String.valueOf(i); flagBoolean = true; } }

解决方案

Android实现计算器功能
Android计算器功能的实现
Android实现简单计算器功能(Button控件实现)

解决方案二:

无法直视,这代码黏贴的,怎么看怎么晕

解决方案三:

```package com.example.t;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    private EditText output = null;
    private EditText input = null;
    private Button btn0 = null;
    private Button btn1 = null;
    private Button btn2 = null;
    private Button btn3 = null;
    private Button btn4 = null;
    private Button btn5 = null;
    private Button btn6 = null;
    private Button btn7 = null;
    private Button btn8 = null;
    private Button btn9 = null;
    private Button btnadd = null;
    private Button btnsubtract = null;
    private Button btnmultiply = null;
    private Button btndivide = null;
    private Button btnclear = null;
    private Button btnresult = null;
    private Button btndot = null;
    private Button btnbackspace = null;
    private Button btnkaifang = null;
    private Button btnpingfang = null;

    private String str = "";// 保存数字
    private String strold = "";// 原数字
    private char act = ' ';// 记录“加减乘除等于”符号
    private int count = 0;// 判断要计算的次数,如果超过一个符号,先算出来一部分
    private Double result = 0.0;// 计算的输出结果
    private Boolean errBoolean = false;// 有错误的时候为true,无错为false
    private Boolean flagBoolean = false;// 一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息
    private Boolean flagDot = false; // 小数点标志位

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        output = (EditText) rootView.findViewById(R.id.output);
        input = (EditText) rootView.findViewById(R.id.input);
        btn0 = (Button) rootView.findViewById(R.id.zero);
        btn1 = (Button) rootView.findViewById(R.id.one);
        btn2 = (Button) rootView.findViewById(R.id.two);
        btn3 = (Button) rootView.findViewById(R.id.three);
        btn4 = (Button) rootView.findViewById(R.id.four);
        btn5 = (Button) rootView.findViewById(R.id.five);
        btn6 = (Button) rootView.findViewById(R.id.six);
        btn7 = (Button) rootView.findViewById(R.id.seven);
        btn8 = (Button) rootView.findViewById(R.id.eight);
        btn9 = (Button) rootView.findViewById(R.id.nine);
        btnadd = (Button) rootView.findViewById(R.id.add);
        btnsubtract = (Button) rootView.findViewById(R.id.subtract);
        btnmultiply = (Button) rootView.findViewById(R.id.multiply);
        btndivide = (Button) rootView.findViewById(R.id.divide);
        btnclear = (Button) rootView.findViewById(R.id.clear);
        btnresult = (Button) rootView.findViewById(R.id.result);
        btndot = (Button) rootView.findViewById(R.id.dot);
        btnbackspace = (Button) rootView.findViewById(R.id.zhengfu);
        btnkaifang = (Button) rootView.findViewById(R.id.kaifang);
        btnpingfang = (Button) rootView.findViewById(R.id.pingfang);
        // 设置按钮侦听事件
        btn0.setOnClickListener(new BtnOnClickListener());
        btn1.setOnClickListener(new BtnOnClickListener());
        btn2.setOnClickListener(new BtnOnClickListener());
        btn3.setOnClickListener(new BtnOnClickListener());
        btn4.setOnClickListener(new BtnOnClickListener());
        btn5.setOnClickListener(new BtnOnClickListener());
        btn6.setOnClickListener(new BtnOnClickListener());
        btn7.setOnClickListener(new BtnOnClickListener());
        btn8.setOnClickListener(new BtnOnClickListener());
        btn9.setOnClickListener(new BtnOnClickListener());
        // 执行运算
        btnadd.setOnClickListener(new BtnOnClickListener());
        btnsubtract.setOnClickListener(new BtnOnClickListener());
        btnmultiply.setOnClickListener(new BtnOnClickListener());
        btndivide.setOnClickListener(new BtnOnClickListener());
        btnclear.setOnClickListener(new BtnOnClickListener());
        btnresult.setOnClickListener(new BtnOnClickListener());

        btndot.setOnClickListener(new BtnOnClickListener());
        btnbackspace.setOnClickListener(new BtnOnClickListener());
        btnkaifang.setOnClickListener(new BtnOnClickListener());
        btnpingfang.setOnClickListener(new BtnOnClickListener());

        return rootView;
    }

    class BtnOnClickListener implements OnClickListener {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            // 输入数字
            case R.id.zero:
                num(0);
                break;
            case R.id.one:
                num(1);
                break;
            case R.id.two:
                num(2);
                break;
            case R.id.three:
                num(3);
                break;
            case R.id.four:
                num(4);
                break;
            case R.id.five:
                num(5);
                break;
            case R.id.six:
                num(6);
                break;
            case R.id.seven:
                num(7);
                break;
            case R.id.eight:
                num(8);
                break;
            case R.id.nine:
                num(9);
                break;

            case R.id.dot:
                dot();
                break;
            // 执行运算

            //  退格功能
            case R.id.zhengfu:
                backspace();
                break;

            case R.id.kaifang:
                kaifang();
                break;
            case R.id.pingfang:
                pingfang();
                break;
            case R.id.add:
                add();
                break;
            case R.id.subtract:
                sub();
                break;
            case R.id.multiply:
                multiply();
                break;
            case R.id.divide:
                divide();
                break;
            case R.id.clear:
                clear();
                break;
            // 计算结果
            case R.id.result:
                result();
                if (!errBoolean && flagBoolean) {
                    output.setText(String.valueOf(result));
                }

            default:
                break;

            }
            // if(strold==""&&act==' '&&str=="")
            // {
            // input.setText("");
            // }
            // else {
            // input.setText(strold+act+str);
            // }
            if (act == 'k') {
                input.setText(strold);

            } else {
                input.setText(strold + act + str);
            }
            if (act == 'p') {
                input.setText(strold);

            } else {
                input.setText(strold + act + str);
            }
            output.setText(String.valueOf(result));
        }

        private void kaifang() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                act = 'k';
                strold = str;
                // str="";
                result();
            }
        }
        private void pingfang() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                act = 'p';
                strold = str;
                // str="";
                result();
            }
        }

        //退格功能
        private void backspace() {
            // TODO Auto-generated method stub
            String str1=input.getText().toString();
              if(input.length()>0){
                  input.setText(str1.substring(0, str1.length()-1));
              }
        }

        private void dot() {
            // TODO Auto-generated method stub

            if (!flagDot) {
                str = str + ".";
                flagBoolean = false;
                flagDot = true;
            }
            // str=str+i;
            // flagBoolean=false;
        }

        //清空
        private void clear() {
            // TODO Auto-generated method stub
            str = strold = "";
            count = 0;
            act = ' ';
            result = 0.0;
            flagBoolean = false;
            flagDot = false;
            input.setText(strold + act + str);
            output.setText("");
        }

        private void divide() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                check();
                act = '/';
                flagBoolean = false;
            }
        }

        private void multiply() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                check();
                act = '*';
                flagBoolean = false;
            }
        }

        private void sub() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                check();
                act = '-';
                flagBoolean = false;
            }
        }

        private void add() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                check();
                act = '+';
                flagBoolean = false;
            }
        }

        private void check() {
            // TODO Auto-generated method stub
            if (count >= 1) {
                result();
                str = String.valueOf(result);
            }
            strold = str;
            str = "";
            count++;
            flagDot = false;
        }

        // 计算输出结果
        private void result() {
            // TODO Auto-generated method stub
            if (flagBoolean) {
                Double a, b;
                // if(act=='k'||act=='c')
                // {
                // a=Double.parseDouble(strold);
                // }
                // else {
                // a=Double.parseDouble(strold);
                // b=Double.parseDouble(str);
                // }
                a = Double.parseDouble(strold);
                b = Double.parseDouble(str);

                if (b == 0 && act == '/') {
                    clear();
                    output.setText("除数不能为零!");
                    errBoolean = true;
                }
                if (!errBoolean) {
                    switch (act) {
                    case '+':
                        result = a + b;
                        break;
                    case '-':
                        result = a - b;
                        break;
                    case '*':
                        result = a * b;
                        break;
                    case '/':
                        result = a / b;
                        break;
                    case 'k':
                        result = Math.sqrt(a);
                        break;
                    case 'p':
                        result = Math.pow(a,2);
                        break;
                    default:
                        break;
                    }
                }

            }
        }

        private void num(int i) {
            // TODO Auto-generated method stub
            str = str + String.valueOf(i);
            flagBoolean = true;
        }
    }
}

}


解决方案四:

android下的计算器中的退格功能实现不了 (为了方便大家找出错误,我重新整理了下代码) ,提问者和我是同一人,谢谢

 package com.example.t;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        private EditText output = null;
        private EditText input = null;
        private Button btn0 = null;
        private Button btn1 = null;
        private Button btn2 = null;
        private Button btn3 = null;
        private Button btn4 = null;
        private Button btn5 = null;
        private Button btn6 = null;
        private Button btn7 = null;
        private Button btn8 = null;
        private Button btn9 = null;
        private Button btnadd = null;
        private Button btnsubtract = null;
        private Button btnmultiply = null;
        private Button btndivide = null;
        private Button btnclear = null;
        private Button btnresult = null;
        private Button btndot = null;
        private Button btnbackspace = null;
        private Button btnkaifang = null;
        private Button btnpingfang = null;

        private String str = "";// 保存数字
        private String strold = "";// 原数字
        private char act = ' ';// 记录“加减乘除等于”符号
        private int count = 0;// 判断要计算的次数,如果超过一个符号,先算出来一部分
        private Double result = 0.0;// 计算的输出结果
        private Boolean errBoolean = false;// 有错误的时候为true,无错为false
        private Boolean flagBoolean = false;// 一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息
        private Boolean flagDot = false; // 小数点标志位

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            output = (EditText) rootView.findViewById(R.id.output);
            input = (EditText) rootView.findViewById(R.id.input);
            btn0 = (Button) rootView.findViewById(R.id.zero);
            btn1 = (Button) rootView.findViewById(R.id.one);
            btn2 = (Button) rootView.findViewById(R.id.two);
            btn3 = (Button) rootView.findViewById(R.id.three);
            btn4 = (Button) rootView.findViewById(R.id.four);
            btn5 = (Button) rootView.findViewById(R.id.five);
            btn6 = (Button) rootView.findViewById(R.id.six);
            btn7 = (Button) rootView.findViewById(R.id.seven);
            btn8 = (Button) rootView.findViewById(R.id.eight);
            btn9 = (Button) rootView.findViewById(R.id.nine);
            btnadd = (Button) rootView.findViewById(R.id.add);
            btnsubtract = (Button) rootView.findViewById(R.id.subtract);
            btnmultiply = (Button) rootView.findViewById(R.id.multiply);
            btndivide = (Button) rootView.findViewById(R.id.divide);
            btnclear = (Button) rootView.findViewById(R.id.clear);
            btnresult = (Button) rootView.findViewById(R.id.result);
            btndot = (Button) rootView.findViewById(R.id.dot);
            btnbackspace = (Button) rootView.findViewById(R.id.zhengfu);
            btnkaifang = (Button) rootView.findViewById(R.id.kaifang);
            btnpingfang = (Button) rootView.findViewById(R.id.pingfang);
            // 设置按钮侦听事件
            btn0.setOnClickListener(new BtnOnClickListener());
            btn1.setOnClickListener(new BtnOnClickListener());
            btn2.setOnClickListener(new BtnOnClickListener());
            btn3.setOnClickListener(new BtnOnClickListener());
            btn4.setOnClickListener(new BtnOnClickListener());
            btn5.setOnClickListener(new BtnOnClickListener());
            btn6.setOnClickListener(new BtnOnClickListener());
            btn7.setOnClickListener(new BtnOnClickListener());
            btn8.setOnClickListener(new BtnOnClickListener());
            btn9.setOnClickListener(new BtnOnClickListener());
            // 执行运算
            btnadd.setOnClickListener(new BtnOnClickListener());
            btnsubtract.setOnClickListener(new BtnOnClickListener());
            btnmultiply.setOnClickListener(new BtnOnClickListener());
            btndivide.setOnClickListener(new BtnOnClickListener());
            btnclear.setOnClickListener(new BtnOnClickListener());
            btnresult.setOnClickListener(new BtnOnClickListener());

            btndot.setOnClickListener(new BtnOnClickListener());
            btnbackspace.setOnClickListener(new BtnOnClickListener());
            btnkaifang.setOnClickListener(new BtnOnClickListener());
            btnpingfang.setOnClickListener(new BtnOnClickListener());

            return rootView;
        }

        class BtnOnClickListener implements OnClickListener {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                // 输入数字
                case R.id.zero:
                    num(0);
                    break;
                case R.id.one:
                    num(1);
                    break;
                case R.id.two:
                    num(2);
                    break;
                case R.id.three:
                    num(3);
                    break;
                case R.id.four:
                    num(4);
                    break;
                case R.id.five:
                    num(5);
                    break;
                case R.id.six:
                    num(6);
                    break;
                case R.id.seven:
                    num(7);
                    break;
                case R.id.eight:
                    num(8);
                    break;
                case R.id.nine:
                    num(9);
                    break;

                case R.id.dot:
                    dot();
                    break;
                // 执行运算

                //  退格功能
                case R.id.zhengfu:
                    backspace();
                    break;

                case R.id.kaifang:
                    kaifang();
                    break;
                case R.id.pingfang:
                    pingfang();
                    break;
                case R.id.add:
                    add();
                    break;
                case R.id.subtract:
                    sub();
                    break;
                case R.id.multiply:
                    multiply();
                    break;
                case R.id.divide:
                    divide();
                    break;
                case R.id.clear:
                    clear();
                    break;
                // 计算结果
                case R.id.result:
                    result();
                    if (!errBoolean && flagBoolean) {
                        output.setText(String.valueOf(result));
                    }

                default:
                    break;

                }
                // if(strold==""&&act==' '&&str=="")
                // {
                // input.setText("");
                // }
                // else {
                // input.setText(strold+act+str);
                // }
                if (act == 'k') {
                    input.setText(strold);

                } else {
                    input.setText(strold + act + str);
                }
                if (act == 'p') {
                    input.setText(strold);

                } else {
                    input.setText(strold + act + str);
                }
                output.setText(String.valueOf(result));
            }

            private void kaifang() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    act = 'k';
                    strold = str;
                    // str="";
                    result();
                }
            }
            private void pingfang() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    act = 'p';
                    strold = str;
                    // str="";
                    result();
                }
            }

            //退格功能
            private void backspace() {
                // TODO Auto-generated method stub
                String str1=input.getText().toString();
                  if(input.length()>0){
                      input.setText(str1.substring(0, str1.length()-1));
                  }
            }

            private void dot() {
                // TODO Auto-generated method stub

                if (!flagDot) {
                    str = str + ".";
                    flagBoolean = false;
                    flagDot = true;
                }
                // str=str+i;
                // flagBoolean=false;
            }

            //清空
            private void clear() {
                // TODO Auto-generated method stub
                str = strold = "";
                count = 0;
                act = ' ';
                result = 0.0;
                flagBoolean = false;
                flagDot = false;
                input.setText(strold + act + str);
                output.setText("");
            }

            private void divide() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    check();
                    act = '/';
                    flagBoolean = false;
                }
            }

            private void multiply() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    check();
                    act = '*';
                    flagBoolean = false;
                }
            }

            private void sub() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    check();
                    act = '-';
                    flagBoolean = false;
                }
            }

            private void add() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    check();
                    act = '+';
                    flagBoolean = false;
                }
            }

            private void check() {
                // TODO Auto-generated method stub
                if (count >= 1) {
                    result();
                    str = String.valueOf(result);
                }
                strold = str;
                str = "";
                count++;
                flagDot = false;
            }

            // 计算输出结果
            private void result() {
                // TODO Auto-generated method stub
                if (flagBoolean) {
                    Double a, b;
                    // if(act=='k'||act=='c')
                    // {
                    // a=Double.parseDouble(strold);
                    // }
                    // else {
                    // a=Double.parseDouble(strold);
                    // b=Double.parseDouble(str);
                    // }
                    a = Double.parseDouble(strold);
                    b = Double.parseDouble(str);

                    if (b == 0 && act == '/') {
                        clear();
                        output.setText("除数不能为零!");
                        errBoolean = true;
                    }
                    if (!errBoolean) {
                        switch (act) {
                        case '+':
                            result = a + b;
                            break;
                        case '-':
                            result = a - b;
                            break;
                        case '*':
                            result = a * b;
                            break;
                        case '/':
                            result = a / b;
                            break;
                        case 'k':
                            result = Math.sqrt(a);
                            break;
                        case 'p':
                            result = Math.pow(a,2);
                            break;
                        default:
                            break;
                        }
                    }

                }
            }

            private void num(int i) {
                // TODO Auto-generated method stub
                str = str + String.valueOf(i);
                flagBoolean = true;
            }
        }
    }
}

时间: 2024-12-31 17:57:01

android应用-android下的计算器中的退格功能实现不了,麻烦各位大神啦,谢谢!!的相关文章

string-Android下的计算器的退格功能实现不了,还没有解决,请教各位大神,谢谢啦!

问题描述 Android下的计算器的退格功能实现不了,还没有解决,请教各位大神,谢谢啦! package com.example.t; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import

win7系统下开始菜单中的剪切板功能不见了怎么办

  很多用户在win7系统使用的过程中发现,自己的开始菜单里面,常用的剪切板功能莫名其妙的消失不见了,那到底是怎么回事呢?下面就让小编来个大家介绍下win7系统下开始菜单中的剪切板功能不见了怎么办. 步骤一:如果身边有一台同样是ghost win7的电脑或者xp系统的电脑,那我们就可以打开系统盘的windows文件夹里面的system32,找到clipbrd的文件,直接拷贝过去就可以恢复win7系统开始菜单中的剪切板功能使用了. 步骤二:如果身边没有其他电脑的话,那么就使用Win键+R键打开wi

Win7下wps表格中禁止单元格输入重复数值的设置方法

  Wps是一款办公软件,一些用户需要经常使用wps制作一些数据资料,比如学号.员工编号等,而这些数据都有着不能重复的特点,那么要如何禁止在wps的表格中输入重复数值呢?其实只要简单设置一下就可以了,下面跟大家带来Win7下在wps表格中禁止单元格输入重复数值的设置方法如下. 1.首先我们打开win7系统下WPS软件表格软件,并新建一个表格.   2.选中需要输入数值的行或列,在此小编以选择B列为例进行讲解.大家可以根据自己的表格输入的需要进行选择.   3.选择后点击"重复项";  

自动-VS2008中创建了个list contrl控件,读取excel中某单元格中有换行的问题,求大神解决

问题描述 VS2008中创建了个list contrl控件,读取excel中某单元格中有换行的问题,求大神解决 当单元格中有换行时,list contrl控件中的内容就不能显示出来? 能否进行自动换行? 解决方案 listctrl功能没这么强大,对excel格式的换行支持不了. 这个要你自己实现,你读取的时候,读到n换行时.自己把后面的插入下一行

远程控制-linux中按退格键显示 ^H问题

问题描述 linux中按退格键显示 ^H问题 程序代码说明: 通过read函数读入一段字符 到 buf中 并通过printf函数显示出来 为了按下回车键实现擦除功能,而不显示^H字符,所以修改termios状态 程序代码如下 int main() { char buf[256] = {0}; struct termios term; struct termios oldterm; if(tcgetattr(STDIN_FILENO, &term) == -1)//得到系统termion的设置 {

如何解决Linux中sqlplus退格和上翻键乱码问题

在Linux下使用sqlplus你会发现:退格键无法正常使用(乱码),上翻键也无法正常显示历史功能, 非常讨厌! 为了让退格键和上翻键在sqlplus里正常发挥它的作用,我们必须安装一个软件 - rlwrap 1. 首先,从http://utopia.knoware.nl/~hlub/uck/rlwrap/下载安装包,上传到linux服务器上 2. 以root用户解压缩安装 tar -zxvf rlwrap-0.37.tar.gz cd rlwrap-0.37 ./configure make

Android开发简单的乘法计算器,并添加退出菜单

1.目标: 写一个简单的乘法计算器,并添加退出菜单 2.即将需要或者学习到的知识: ①.TextView显示文本 ②.EditText编辑框 ③.Button按钮     ④.Menu菜单 3基础知识: ① setText函数中,连接符两端,有一个端是字符串,则另外一端也会被转为字符串. [java] private TextView resultView;  private int result;  ...  resultView.setText(result + "");  pri

android 邮件-android通过代码获取当前系统中安装的所有邮件应用列表

问题描述 android通过代码获取当前系统中安装的所有邮件应用列表 想通过代码得到当前系统所有可以收发邮件的应用,通过列表显示出来,求大神指导,有知道的朋友请知道下获取的方法,目前我能想到的就是获取当前所有安装的应用通过关键字过滤,不过感觉这种方法不是很好,不能保证所有过滤出来的都是邮件应用.

Android自定义控件实战——下拉刷新控件终结者:PullToRefreshLayout

转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38340701            说到下拉刷新控件,网上版本有很多,很多软件也都有下拉刷新功能.有一个叫XListView的,我看别人用过,没看过是咋实现的,看这名字估计是继承自ListView修改的,不过效果看起来挺丑的,也没什么扩展性,太单调了.看了QQ2014的列表下拉刷新,发现挺好看的,我喜欢,贴一下图看一下qq的下拉刷新效果: