字符串stored
中包含“1.0
”。每次按下按钮,我想将其值增加0.5
。但相反,我的输出变为“1.00.5
”。我该如何解决这个问题?
String stored = userspeed.getText().toString();
String speedplus = stored + 0.5;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_speed", speedplus.toString());
editor.apply();
更新
public class ProgramActivity extends AppCompatActivity {
EditText userspeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program);
userspeed = (EditText) findViewById(R.id.userspeed);
//load values
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String stored = preferences.getString("user_speed", "1.0");//default
userspeed.setText(stored, TextView.BufferType.EDITABLE);
}
public void adduserspeed(View view) {
String stored = userspeed.getText().toString();
double storedValue = Double.parseDouble(stored);
String speedplus = String.valueOf(storedValue +0.5f);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_speed", speedplus);
editor.apply();
}
}
答案 0 :(得分:0)
String stored = userspeed.getText().toString();
float speedplus = Float.parseFloat(stored) + 0.5;
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putFloat("user_speed", speedplus);
editor.apply();
将字符串值解析为int,然后进行数学运算。否则将发生字符串连接。
答案 1 :(得分:0)
尝试解析存储的,即1.0为Double,然后添加0.5d
String speedplus = String.valueOf((Double.parseDouble(stored) + 0.5d));
最终代码
String stored = userspeed.getText().toString();
String speedplus = String.valueOf((Double.parseDouble(stored) + 0.5d));
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_speed", speedplus.toString());
editor.apply();
答案 2 :(得分:0)
最快的黑客将是:
String speedplus = String.valueOf(Float.parseFloat(stored) + 0.5F);
但是,您的下一个问题无疑将是“为什么我的结果为1.499999999
?”,即answered here in detail。
答案 3 :(得分:0)
你也可以使用double:
String speedplus = (Double.parseDouble(stored)+0.5).toString();
答案 4 :(得分:0)
首先,您需要将字符串值转换为浮点值,然后执行添加操作,最后将结果转换为字符串,以便您可以在共享首选项中存储为字符串。
final String stored = userspeed.getText().toString();
final float storedValue = Float.parseFloat(stored);
final String speedplus = String.valueOf(storedValue + 0.5f);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_speed", speedplus);
editor.apply();
如果要使用double值,只需将第一行替换为以下内容:
final String stored = "1.0";
final double storedValue = Double.parseDouble(stored);
final String speedplus = String.valueOf(storedValue + 0.5d);
<强>更新强>
您的问题是在adduserspeed(View view)
方法中,您没有从首选项中检索存储的值,您正在从EditText中检索,并且您没有更新该EditText值,因此每次执行adduserspeed(View view)
方法时,您在首选项上存储的值是1.0 + 0.5,因为您重新打开应用程序之前EditText值为1.0。当您重新打开应用程序时,您的EditText值为1.5,依此类推......
我已经改进了你的代码,现在效果很好,不管怎么说,不要只是复制我的代码,尝试理解它,这样你就可以学习。
public class MainActivity extends AppCompatActivity {
private EditText userSpeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.userSpeed = (EditText) findViewById(R.id.userspeed);
setUserSpeedText();
}
public void adduserspeed(View view) {
final String stored = getUserSpeedValue();
final double storedValue = Double.parseDouble(stored);
final String speedPlus = String.valueOf(storedValue + 0.5f);
setUserSpeedValue(speedPlus);
setUserSpeedText();
}
private String getUserSpeedValue() {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
return preferences.getString("user_speed", "1.0");
}
private void setUserSpeedValue(final String newSpeedValue) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
editor.putString("user_speed", newSpeedValue);
editor.apply();
}
private void setUserSpeedText() {
if (null != this.userSpeed) {
this.userSpeed.setText(getUserSpeedValue());
}
}
}