Android如何在外部点击时将文本保留在EditText中?

问题描述 投票:0回答:2

[当我键入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
2个回答
2
投票

通过关闭alertDialog,内部的所有数据都将丢失。您可以通过以下方式阻止关闭alertDialog:

passwordInputDialog.setCancelable(false);

或您可以将最后一个值保存在alertDialog中,并通过再次显示将其取回。 (不推荐)


2
投票

要回答您的问题,editText不会消失!这是对话框,并且由于该对话框是承载editText的对话框,因此,如果这正是您正在执行的操作,则对话框的onCancle也将带走editText。

注意:请注意以下代码中的注释。

首次观察:

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); /* why this? when you will still add it back immediately below? */
        enteredPassword.setText(enteredText); /*Change to enteredText.toString() because enteredText is an interface and you need to point to the method toString to get the character array inside it.*/
        enteredPassword.setSelection(enteredText.length()); /*Not sure why you're using this either */
        enteredPassword.addTextChangedListener(this); /*why this? when you're already listening?*/
    }
});

这可以取代您目前对上述enterPassword的了解:

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.setText(enteredText.toString()); /* although this is actually weird! in the sense that enteredPassword already has the text and yet, you're still assigning it texts back to itself, I mean enteredPassword.setText(enteredPassword.getText().toString() is equivalent to enteredText.toString())*/
    }
});

还:因为您说过editText在对话框中,所以如果Dialog实际上被关闭了,那么您应该不会看到editText,好吧,我想该对话框没有被关闭,并且在同一对话框上您正在尝试执行其他操作以及当焦点从editText更改为对话框上的另一个View,然后EditText中用于输入密码的文本消失了吗?如果是:如果您只需要文本,则实际上不需要TextChange侦听器,只需创建一个变量即可存储文本并根据需要使用。

最后:我的问题是,您打算做什么?不要害怕问更多复杂的问题,只需要具体即可。

© www.soinside.com 2019 - 2024. All rights reserved.