从 EeditText 添加数据到 double

Add data fromEeditText to double

我有一个应用程序,用户可以在其中将金额输入 EditText,但我希望能够添加或减去这个 to/from 一个双精度然后能够在另一个中使用 TextView 显示这个双精度activity.

我不确定该怎么做,希望得到一些帮助。 提前致谢!

编辑:我忘了说我也想在应用 launches/closes 之间保存这些数据。

首先您需要解析 EditText 中的数据,您可以使用

从 EditText 中获取字符串
EditText.getText().toString()

然后使用某种形式的

Double.parseDouble(String)
Integer.parseInt(String)

从字符串中获取数值,然后您可以将其用于任何需要的数学运算。计算出这个值后,您将希望通过 intent

将其发送给另一个 Activity
Intent i = new Intent(this, ActivityTwo.class);
        i.putExtra("KEY", myDouble);
        startActivity(i);

在您的下一个 activity 中接收意图

Bundle extras = getIntent().getExtras();
if (extras != null) {
    Double myDouble = extras.getDouble("KEY");
}

然后,如果您想保存该值,您需要查看 SharedPreferences

节省

SharedPreferences prefs = getSharedPreferences("KEY", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("KEY", myFloat);
editor.commit();

得到

SharedPreferences prefs = getSharedPreferences("KEY", Context.MODE_PRIVATE);
myFloat = prefs.getFloat("KEY", myFloat);

在您的 activity 中接受来自 EditText 的输入:

double value = Double.parseDouble(yourEditText.getText().toString());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (adding) {
    value = prefs.getFloat("your.float.key", 0f) + value;
} else {
    value = prefs.getFloat("your.float.key", 0f) - value;
}
SharedPreferences.Editor editor = prefs.edit();
editor.putFloat("your.float.key", value);
editor.apply();

在您的 activity 中显示值:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Double value = prefs.getFloat("your.float.key", 0f);
yourTextView.setText(value.toString());