关闭

android中自带计算器按.格式显示错误问题

标签: android计算器
214人阅读 评论(0) 收藏 举报
分类:

在开发中遇到的关于android手机自带计算器显示格式纠正问题

此问题修改的文件路径:alps/packages/apps/Calculator/src/com/android/calculator2/CalculatorExpressionBuilder.java

replace()方法中做修改:

在switch循环后加入如下代码

        if(expr.length()>0){
        char mText=expr.charAt(expr.length()-1);
        Boolean mBoolean =false;
        if(!(mText==('0')||mText==('1')||mText==('2')||
            mText==('3')||mText==('4')||mText==('5')||
            mText==('6')||mText==('7')||mText==('8')||
            mText==('9'))){
            mBoolean=true;
        }
        android.util.Log.i("CalculatorExpressionBuilder","mBoolean="+mBoolean);
        if(appendExpr.equals(".")&&mBoolean){
            appendExpr="0.";
        }
          }else if(expr.length()==0&&appendExpr.equals(".")){
             appendExpr="0.";
          }
        }



此方法完整的代码所示

    @Override
    public SpannableStringBuilder replace(int start, int end, CharSequence tb, int tbstart,
            int tbend) {
        if (start != length() || end != length()) {
            mIsEdited = true;
            return super.replace(start, end, tb, tbstart, tbend);
        }

        String appendExpr =
                mTokenizer.getNormalizedExpression(tb.subSequence(tbstart, tbend).toString());
        if (appendExpr.length() == 1) {
            final String expr = mTokenizer.getNormalizedExpression(toString());
            // SPRD 515934 enter all numbers and operators, calculator crash
            final int len = expr.length();
            switch (appendExpr.charAt(0)) {
                case '.':
                    // don't allow two decimals in the same number
                    final int index = expr.lastIndexOf('.');
                    // SPRD: 544823 modify for StringIndexOutOfBoundsException
                    if (index != -1 && index < start && TextUtils.isDigitsOnly(expr.substring(index + 1, start))) {
                        appendExpr = "";
                    }
                    break;
                case '+':
                case '*':
                case '/':
                    // don't allow leading operator
                    if (start == 0) {
                        appendExpr = "";
                        break;
                    }
                    /*SPRD: Bug 487833 don't allow leading operator change from - to * or / @{ */
                    if (start == 1 && expr.equals("-")) {
                        appendExpr = "";
                        break;
                    }
                    /* @} */
                    /* SPRD 515934 enter all numbers and operators, calculator crash @{ */
                    Log.d("Calculator", "len: " + len + " start: " + start+" expr: "+expr);
                    if (start > len) {
                        break;
                    }
                    /* @} */
                    // don't allow multiple successive operators
                    while (start > 0 && "+-*/".indexOf(expr.charAt(start - 1)) != -1) {
                        --start;
                    }
                    // fall through
                case '-':
                    // don't allow -- or +-
                    // SPRD 515934 enter all numbers and operators,calculator crash
                    if ((start > 0) && (start <= len) && "+-".indexOf(expr.charAt(start - 1)) != -1) {
                        --start;
                    }

                    // mark as edited since operators can always be appended
                    mIsEdited = true;
                    break;
                default:
                    break;
            }
        if(expr.length()>0){
        char mText=expr.charAt(expr.length()-1);
        Boolean mBoolean =false;
        if(!(mText==('0')||mText==('1')||mText==('2')||
            mText==('3')||mText==('4')||mText==('5')||
            mText==('6')||mText==('7')||mText==('8')||
            mText==('9'))){
            mBoolean=true;
        }
        android.util.Log.i("CalculatorExpressionBuilder","mBoolean="+mBoolean);
        if(appendExpr.equals(".")&&mBoolean){
            appendExpr="0.";
        }
          }else if(expr.length()==0&&appendExpr.equals(".")){
             appendExpr="0.";
          }
        }

        // since this is the first edit replace the entire string
        if (!mIsEdited && appendExpr.length() > 0) {
            start = 0;
            mIsEdited = true;
        }

        appendExpr = mTokenizer.getLocalizedExpression(appendExpr);
        return super.replace(start, end, appendExpr, 0, appendExpr.length());
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8902次
    • 积分:261
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类