简体   繁体   中英

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

I have a set of EditText fields generated by a for loop. Each time that field is edited, I need to run some calculations and update other text fields (think excel spreadsheet sums).

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 .

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. Now the values are updated whenever any text inside the EditText is changed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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