문제

I want to hide the cursor and do some other stuff as soon as the keyboard is minimized. I already enable the cursor and show the keyboard when the editext is touched, but I can't find a way to know when a user minimizes it without pressing 'Done'.

Is there a way to do that?

도움이 되었습니까?

해결책

try below code:-

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    activityRootView.getWindowVisibleDisplayFrame(r);

                    int heightDiff = activityRootView.getRootView()
                            .getHeight() - (r.bottom - r.top);
                    if (lastDiff == heightDiff)
                        return;
                    lastDiff = heightDiff;
                    Log.i("aerfin","arefin "+lastDiff);
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        flag2 = 0;
                    } else {
                        if (flag == false)
                            flag2 = 1;
                    }
                }
            });

more info see below link least use full for me:-

Maintain keyboard open/closed state for EditText when app comes to foreground

How to check visibility of software keyboard in Android?

다른 팁

In your manifest, make sure the configuration changes when the keyboard shows or hides by putting this line as argument in your <activity> tag in your manifest:

android:configChanges="orientation|keyboardHidden"

Then override this method in your Activity.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks whether a hardware keyboard is available
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
        Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
        Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
    }
}
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top