如何从Android计算器中检索值

时间:2012-11-05 20:34:39

标签: android calculator

我正在开发一个用户点击EditText的应用程序,它显示了android计算器,因此用户可以进行算术运算。

当用户按下android计算器中的相等按钮并显示它时,我想检索最终值。这可能使用标准的Android计算器吗?

打开计算器的代码如下:

Intent intent = new Intent();           
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(CALCULATOR_PACKAGE, CALCULATOR_CLASS));
startActivity(intent);

其中,

String CALCULATOR_PACKAGE ="com.android.calculator2";
String CALCULATOR_CLASS ="com.android.calculator2.Calculator";

有关如何从计算器中检索最终值的任何想法或建议? 感谢

2 个答案:

答案 0 :(得分:2)

使用标准计算器是不可能的,但该计算器是开源的,因此您可以创建自己的计算器。源包含一个名为Logic(Logic.java)的类;如果你更新方法evaluateAndShowResult()以自动将结果粘贴到剪贴板,然后在你的应用程序中粘贴,你就准备好了。

作为替代方案,如果你真的不需要计算器的用户界面,你就可以拿走逻辑并把剩下的东西丢弃。

答案 1 :(得分:-1)

您可以使用startActivityForResult启动计算器以返回结果。

这是一个例子

Intent myIntent = new Intent();
myIntent.setClassName("com.android.calculator2", "com.android.calculator2.Calculator");
startActivityForResult(myIntent, CALC_INTENT_RETURN);

   /**
     * Framework method called when return from a launched intent is available.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    if (requestCode == CALC_INTENT_RETURN) {
        if (resultCode == RESULT_OK) {
                    mCalculator.setFeatureValue(mCurrentInputField, Double.parseDouble(data.getAction()));
                    resetValue();
        }
    }
}

从中删除 http://code.google.com/p/android-labs/source/browse/trunk/BistroMath/src/com/google/android/bistromath/BistroMath.java?r=6