简体   繁体   中英

Detect EditText pressed outside of custom keyboard (IME) Android

I am developing an android keyboard with a search box inside it. I can detect when the user press my search box and then commit the text he types to my search box.

The problem is that I can't manage to detect when the user is pressing the original EditText (the one that opened the keyboard) because it's not my view.

This is how basically the keyboard looks like:

键盘

The keyboard is surrounded by the blue square, and my edit text is surrounded by the red square.

I tried setting this listener on my editText but with no luck: ( source )

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
        }
    }
});

The listener isn't being called when I press on the other editText. Probably because it is not a part of my service.

Even if I could some how detect a click on screen that is outside of my service layout, it would be great.

Thanks for any help!

Although your question is very basic I think I can help here try this below snip of code and let me know if it works for you.

public void showKeyboard(View view) {
    InputMethodManager myKeyboard = (InputMethodManager) getSystemService(Context
            .INPUT_METHOD_SERVICE);
    myKeyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

}

public void hideKeyboard() {
    InputMethodManager myKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    myKeyboard.hideSoftInputFromWindow(EditText1.getWindowToken(), 0);
}

Also this is a duplicate question, see here: Android: show soft keyboard automatically when focus is on an EditText

Please try the ol' Google before posting questions here, it can stall your progress greatly while you wait for others to comment on your post when the answer is out there :) Also as a side note if you post duplicate questions and do not get enough up votes on your post, Stack Overflow can ban you from posting.

Also you can review the Android Developer Docs on this topic as well: https://developer.android.com/training/keyboard-input/visibility

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