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

当我键入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中写的内容以再次显示它们,因此用户不必多次键入内容。

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

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

passwordInputDialog.setCancelable(false);

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

,
  

要回答您的问题,editText不会消失!   是对话框,由于对话框是托管editText的对话框,因此,如果这正是您正在执行的操作,则对话框的onCancle也会直接删除editText。

     
    

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

  
     

首次观察

backward()
  

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

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); /* 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?*/
    }
});
  

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

     

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

本文链接:https://www.f2er.com/3043576.html

大家都在问