当方向屏幕改变时,我需要清除 EditText 的错误。我尝试在 onStart() 方法中设置 setError(null),但它不起作用。
我在新项目上试过了,但还是不行。当我点击按钮时,字段设置错误,当我改变我期望的方向时,错误清除,但它不起作用。
public class MainActivity extends ActionBarActivity {
EditText text;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.et);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
text.setError("Error");
}
});
}
@Override
public void onStart(){
super.onStart();
text.setError(null);
}
最佳答案
更新:
看来你必须在暂停而不是在恢复/开始时清除错误:
@Override
protected void onPause(){
super.onPause();
text.setError(null);
}
以上对我有用。
旧:
Android 将在方向更改时自动为您处理 EditText 及其状态。
如果您想自己处理状态,您需要实现并覆盖 onSaveInstanceState 而不是调用 super()。您不需要重写 onRestoreInstanceState 除非您需要处理其他内容的恢复。
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
//super.onSaveInstanceState(outState);
}
关于android - 如何清除方向更改时的 EditText 错误状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428848/