当我键入EditText并点击屏幕另一部分时,EditText中的文本消失(就像键盘在关闭/隐藏时一样)。
EditText是密码输入,当前显示在AlertDialog.Builder
中:
final AlertDialog.Builder passwordInputDialog = new AlertDialog.Builder(Mainactivity.this,R.style.CustomAlertDialog);
TextInputLayout passwordInputLayout = new TextInputLayout(Mainactivity.this);
passwordInputInflater = LayoutInflater.from(Mainactivity.this).inflate(R.layout.password_input,null);
passwordInputDialog
.setCustomTitle(passwordInputTextView)
.setMessage(message)
.setView(passwordInputLayout)
.setPositiveButton("OK",onPositiveAlertDialogClick())
.setNegativeButton("Cancel",onNegativeAlertDialogClick());
final AlertDialog passwordInputShownDialog = passwordInputDialog.show();
final EditText enteredPassword = passwordInputInflater.findViewById(R.id.etPassword);
EditText附加了一个addTextChangedListener
事件,该事件可以很好地执行任何其他操作:
enteredPassword.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s,int start,int count,int after) { }
@Override
public void onTextChanged(CharSequence s,int before,int count) { }
@Override
public void afterTextChanged(Editable enteredText) {
...
enteredPassword.removeTextChangedListener(this);
enteredPassword.setText(enteredText);
enteredPassword.setSelection(enteredText.length());
enteredPassword.addTextChangedListener(this);
}
});
问题是,如果我做错了什么,或者是“退出输入时”对话框中EditText的默认行为。
如何在点击此按钮后立即保留在EditText中输入的文本?
这是我尝试在afterTextChanged函数上设置EditText的文本时得到的:
2019-11-24 20:20:46.795 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 135477(4MB) AllocSpace objects,0(0B) LOS objects,50% free,9MB/18MB,paused 79us total 113.470ms
2019-11-24 20:20:54.553 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 136023(4MB) AllocSpace objects,49% free,10MB/21MB,paused 89us total 128.803ms
2019-11-24 20:21:03.751 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 137630(4MB) AllocSpace objects,12MB/25MB,paused 108us total 162.638ms
2019-11-24 20:21:13.794 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 137787(4MB) AllocSpace objects,14MB/28MB,paused 110us total 153.423ms
2019-11-24 20:21:24.617 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 137118(4MB) AllocSpace objects,16MB/32MB,paused 101us total 181.131ms
它冻结了应用程序,我不得不将其杀死。
我了解并意识到,我不需要这样做。我想保存用户在SharedPreferences中写的内容以再次显示它们,因此用户不必多次键入内容。