Android 应用程序:Android Studio - 公式计算器

Android app: Android Studio - formula calculator

我想制作一个简单的 Android 最小版本 2 应用程序供本地使用。问题是我从未制作过 Android 应用程序。我现在已经安装了 Android Studio 和 SDK 工具,但是在创建空白应用程序项目时出现错误。其实我是一个VB.NET程序员,我也懂C#,PHP,但是我以前从来没有做过智能手机的东西

我需要的是一个简单的计算器,它有两个文本框(例如 txt1 (a) 和 txt2 (b),它们是双精度输入)、一个计算按钮和一个结果字段(双精度)..请参见下面的示例图片。

当聚焦文本框时,必须出现数字键盘(如计算器),带有小数点按钮。当我单击结果按钮时,必须按照以下公式计算和打印输出:

任何人都可以告诉我如何制作这个吗?或者任何人都可以告诉在哪里编写代码来使这个应用程序工作?

谢谢。

因此,首先您需要了解 android 布局 XML 文件的工作原理。然后添加 4 项:2 EditText 开头,然后是 Button 和文本 "Calculate",最后还有一个 EditText。为它们全部设置一些 ID(即文本 1、文本 2、按钮、输出)。
也不要忘记将所有项目设置为在 "layout_width" 处具有 "match_parent",在 "layout_height" 处具有 "wrap_content"(以便根据需要显示)。

然后在你的主 activity class,在你的 onCreate(Bundle savedInstanceState):

final EditText text1 = (EditText) findViewById(R.id.text1);
final EditText text2 = (EditText) findViewById(R.id.text2);
final EditText output = (EditText) findViewById(R.id.output);
text1.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
text2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
output.setEditable(false);
Button calculate = (Button) findViewById(R.id.button);
calculate.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (text1.getText().isEmpty() || text2.getText().isEmpty()) return;
        double text1Value = Double.valueOf(text1.getText().toString());
        double text2Value = Double.valueOf(text2.getText().toString());
        double a, b;
        if (text1Value > text2Value) {
            a = text1Value;
            b = text2Value;
        } else {
            a = text2Value;
            b = text1Value;
        }
        double result = (a*a - b*b) / 4;
        result = Math.sqrt(result);
        output.setText(String.valueOf(result));
    }
});

希望对大家有所帮助,如有错误请告知!