设置旋转器 onResume 不起作用
我想在暂停时保存 2 个编辑文本(et1,et2
)中的文本和 3 个微调器(spinnerm、spinner 和 spinner2)中的选择并将它们设置回恢复时。 edittexts 中的文本已正确保存,只有微调器选择无法按预期工作。 我的代码:
public void onResume(){
super.onResume();
Log.d("REZUM","notr smo");
SharedPreferences seti = getSharedPreferences( "pavzica", MODE_PRIVATE);
spinnerm.setSelection(seti.getInt("m", 0));
spinner.setSelection(seti.getInt("k", 0));
spinner2.setSelection(seti.getInt("p", 0));
et1.setText(seti.getString("zade", ""));
et2.setText(seti.getString("Vseb", ""));
}
public void onPause() {
shraniPolja();
super.onPause();
}
public void shraniPolja() {
SharedPreferences seti = getSharedPreferences( "pavzica", MODE_PRIVATE);
SharedPreferences.Editor edito = seti.edit();
edito.putString("zade", et1.getText().toString());
edito.putString("Vseb", et2.getText().toString());
edito.putInt("m", spinnerm.getSelectedItemPosition());
edito.putInt("k", spinner.getSelectedItemPosition());
edito.putInt("p", spinner2.getSelectedItemPosition());
edito.putBoolean("b", true);
edito.commit();
}
我做错了什么?
I want to save text from 2 edittexts(et1,et2
) and selection from 3 spinners(spinnerm,spinner and spinner2) onPause and setting them back onResume.
Text from edittexts is saved correctly, only spinner selection don't work as desired.
My code:
public void onResume(){
super.onResume();
Log.d("REZUM","notr smo");
SharedPreferences seti = getSharedPreferences( "pavzica", MODE_PRIVATE);
spinnerm.setSelection(seti.getInt("m", 0));
spinner.setSelection(seti.getInt("k", 0));
spinner2.setSelection(seti.getInt("p", 0));
et1.setText(seti.getString("zade", ""));
et2.setText(seti.getString("Vseb", ""));
}
public void onPause() {
shraniPolja();
super.onPause();
}
public void shraniPolja() {
SharedPreferences seti = getSharedPreferences( "pavzica", MODE_PRIVATE);
SharedPreferences.Editor edito = seti.edit();
edito.putString("zade", et1.getText().toString());
edito.putString("Vseb", et2.getText().toString());
edito.putInt("m", spinnerm.getSelectedItemPosition());
edito.putInt("k", spinner.getSelectedItemPosition());
edito.putInt("p", spinner2.getSelectedItemPosition());
edito.putBoolean("b", true);
edito.commit();
}
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在调用 setSelection / setText 后使视图无效。
您可以使用 mYourView.invalidate() 使整个视图无效;或者您可以仅使视图或特定可绘制对象的一部分无效。 请参阅 View 类文档。
如果您希望微调器动画在微调器达到恢复时的值时运行,请使用具有第二个布尔参数的 setSelection 形式并传入 true。
you need to invalidate the view after calling setSelection / setText.
you can invalidate the entire view with mYourView.invalidate(); or you can invalidate just part of a view or a particular drawable. See the View class documentation.
If you want the spinner animation to run as the spinner gets to the value on resume, use the form of setSelection that has a second boolean parameter and pass true in.