【发布时间】:2020-03-19 15:52:46
【问题描述】:
当我输入 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 start, 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, 0(0B) LOS 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, 0(0B) LOS objects, 50% free, 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, 0(0B) LOS objects, 50% free, 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, 0(0B) LOS objects, 50% free, 16MB/32MB, paused 101us total 181.131ms
它冻结了应用程序,我不得不杀死它。
我理解并意识到我需要的不是那样做。因为我想保存用户在 SharedPreferences 中写入的内容以再次显示它们,这样用户就不必多次键入内容。
【问题讨论】:
标签: android android-edittext android-alertdialog