简体   繁体   English

在 EditText 中检测键盘的关闭(通过后退按钮)

[英]Detect dismissal of the keyboard (by back button) in EditText

I have a set of EditText fields generated by a for loop.我有一组由 for 循环生成的EditText字段。 Each time that field is edited, I need to run some calculations and update other text fields (think excel spreadsheet sums).每次编辑该字段时,我都需要运行一些计算并更新其他文本字段(想想 excel 电子表格总和)。

My logic is working, and I even have it set up to replace the last field's down focus change to the first field (to avoid the "Done" button showing on the keyboard).我的逻辑正在运行,我什至将它设置为将最后一个字段的向下焦点更改替换为第一个字段(以避免键盘上显示“完成”按钮)。

However, if the user presses the Back button to dismiss the keyboard, the focus is not changed and the calculations are not done.但是,如果用户按下后退按钮关闭键盘,焦点不会改变,计算也不会完成。 The entered number is different but now the totals are wrong.输入的数字不同,但现在总数是错误的。 I can't find where to detect when the keyboard is dismissed so I can work around this.我找不到在哪里可以检测键盘何时关闭,所以我可以解决这个问题。

What I have now:我现在所拥有的:

final EditText etWeight = new EditText(this);
etWeight.setText("initWt");
etWeight.setSelectAllOnFocus(true);
etWeight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        //code to update after weight is changed
    }
));
llWeightRow.addView(etWeight);

I have also tried putting this code in setOnEditorActionListener , setOnClickListener , and setOnKeyListener .我也尝试将这段代码放在setOnEditorActionListenersetOnClickListenersetOnKeyListener

Aside from some of these not working as expected, none of them appear to trigger if the back button is pressed to dismiss the keyboard.除了其中一些无法按预期工作之外,如果按下后退按钮以关闭键盘,它们似乎都不会触发。 I have searched online but only came up with suggestions on how to manually hide the keyboard with my own button;我在网上搜索过,但只提出了如何用我自己的按钮手动隐藏键盘的建议; I can't seem to find anything concerning the back button on the tablet itself.我似乎无法找到有关平板电脑本身后退按钮的任何信息。

How can I detect that the keyboard has been dismissed so I can force a focus change (or just re-run the calculation code)?我怎样才能检测到键盘已被解除,以便我可以强制改变焦点(或只是重新运行计算代码)?

Update : Not marking this as answered because this doesn't answer this specific question, but I currently am producing my desired results (other fields always updated) by switching to a onTextChanged method.更新:不将此标记为已回答,因为这不能回答这个特定问题,但我目前正在通过切换到onTextChanged方法来产生我想要的结果(其他字段总是更新)。 Now the values are updated whenever any text inside the EditText is changed.现在,只要EditText任何文本发生更改,这些值就会更新。

etWeight.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence cs, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence cs, int i, int i1, int i2) {
        //calc and update code
    }
    @Override
    public void afterTextChanged(Editable editable) {
    }
});

Have you tried to override onBackPressed?您是否尝试过覆盖 onBackPressed? You can make your manipulations with focus there.您可以在那里专注地进行操作。

@Override
public void onBackPressed() {
    // Make needed preparations here
    super.onBackPressed();
}

try this尝试这个

InputMethodManager imm = (InputMethodManager) ActivityName.this.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (imm.isAcceptingText()) {
            Log.d(TAG,"Software Keyboard was shown");
        } else {
            Log.d(TAG,"Software Keyboard was not shown");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM