我试图保存用户在3个editText fiels(editText1,editText2,editText3)中放置的值,以便它们在应用程序重新启动后显示在右侧字段中。 问题:重新启动后,只有editText3的最后一个值被保存在3个字段中。 什么是解决方法,以便特定字段的值得到保存和重新加载?Android:使用多个editTexts保存首选项
-2
A
回答
0
您需要使用不同的密钥保存的每个文本:
savePreferences("storedName", editText1.getText().toString());
savePreferences("storedName2", editText2.getText().toString());
savePreferences("storedName3", editText3.getText().toString());
然后,检索每个文本
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
editText1.setText(sharedPreferences.getString("storedName", "YourName"));
editText2.setText(sharedPreferences.getString("storedName2", "YourName"));
editText3.setText(sharedPreferences.getString("storedName3", "YourName"));
}
要小心,因为第二个参数( “YOURNAME” )在getString中是默认值,以防您没有使用指定的键参数保存任何文本。
0
解决了!
public class MainActivity extends AppCompatActivity {
int sum = 0;
int ects = 0;
float mark = 0;
NumberFormat numberFormat = new DecimalFormat("0.00");
Button button1;
TextView textView1;
EditText editText1;
EditText editText2;
EditText editText3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
textView1 = (TextView)findViewById(R.id.textView1);
editText1 = (EditText)findViewById(R.id.editTextGDI);
editText2 = (EditText)findViewById(R.id.editTextGDW);
editText3 = (EditText)findViewById(R.id.editTextProgrammieren1);
loadSavedPreferences();
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v){
ViewGroup group = (ViewGroup)findViewById(R.id.activity_main);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
if(view.equals(editText1) && !(editText1.getText().toString().matches(""))){ //GDI
System.out.println("edittext1");
ects += 5;
try{ sum += Integer.parseInt(editText1.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText2)&& !(editText2.getText().toString().matches(""))){ //GDW
System.out.println("edittext2");
ects += 5;
try{ sum += Integer.parseInt(editText2.getText().toString()) *5; } catch(NumberFormatException n){}
}
else if(view.equals(editText3)&& !(editText3.getText().toString().matches(""))){
System.out.println("edittext3");
ects += 7;
try{ sum += Integer.parseInt(editText3.getText().toString()) *7; } catch(NumberFormatException n){}
}
}
}
mark = (float)sum/(float)ects;
textView1.setText(String.valueOf(numberFormat.format(mark)));
savePreferences("1", editText1.getText().toString());
savePreferences("2", editText2.getText().toString());
savePreferences("3", editText3.getText().toString());
sum = 0;
ects = 0;
mark = 0;
}
}
);
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
editText1.setText(sharedPreferences.getString("1", "YourName"));
editText2.setText(sharedPreferences.getString("2", "YourName"));
editText3.setText(sharedPreferences.getString("3", "YourName"));
}
private void savePreferences(String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
}
相关问题
- 1. 保存Android的首选项
- 2. Android列表首选项,如何保存个人首选项
- 3. 保存登录信息(首选项)android
- 4. 如何使用共享首选项保存位置Android?
- 5. Android试图使用首选项来保存数据
- 6. iPhone滑块保存用户首选项
- 7. 使用多个ClassLoaders的首选项API?
- 8. 无法使用统一首选项保留我的首选项
- 9. 铃声首选项不保存选择
- 10. Android:在首选项xml中多次使用相同的首选项
- 11. 如何使用共享首选项将多个图像保存到ImageView
- 12. Android将Edittexts的值保存在SharedPreference中
- 13. 使用共享首选项在Android中设置首选项
- 14. Android应用首选项中的值是否自动保存?
- 15. 在共享首选项中保存用户ID Android
- 16. 复选框值在android中保存共享首选项?
- 17. 如何存储和检索(键,值)使用保存的首选项的Android
- 18. 密码保护Android中的首选项
- 19. 保存多个复选框选项
- 20. 已保存的首选项未获取
- 21. 保存int的共享首选项?
- 22. 在首选项中保存seekbar进度
- 23. 在首选项中保存Firefox扩展
- 24. 共享首选项保存Textview背景
- 25. 保存和恢复一组首选项
- 26. Libgdx:如何保存首选项?
- 27. 首选项不保存在DialogPreference中
- 28. 保存意向共享首选项
- 29. 保存excel 2003的打印首选项
- 30. Android保存并使用自定义DialogPreference显示用户首选项
通过将[MCVE] – t0mm13b