Java Code Examples for android.app.Activity#getCurrentFocus()

The following examples show how to use android.app.Activity#getCurrentFocus() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: RxZhihuDaily   File: BaseFragment.java    License: MIT License 6 votes vote down vote up
protected void showKeyboard() {
    Activity activity = getActivity();
    if (activity == null) {
        return;
    }

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm == null) {
        return;
    }

    if (activity.getCurrentFocus() == null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    } else {
        imm.showSoftInput(activity.getCurrentFocus(), 0);
    }
}
 
Example 2
Source Project: UltimateAndroid   File: BasicUiUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Hide soft keyboard method.
 *
 * @param context
 * @param activity
 */
public static void hiddenKeyboard(Context context, Activity activity) {
    try {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        if (activity.getCurrentFocus() != null) {
            if (activity.getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(activity
                                .getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example 3
Source Project: orz   File: BaseUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void showInputMethod(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        View currentFocus = activity.getCurrentFocus();
        if (currentFocus != null) {
            imm.hideSoftInputFromWindow(currentFocus.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
            imm.showSoftInput(currentFocus, InputMethodManager.SHOW_IMPLICIT);
        }

//        if (imm != null) {
//            imm.viewClicked(this);
//        }
//        if (imm != null && getShowSoftInputOnFocus()) {
//            imm.showSoftInput(this, 0);
//        }
    }
 
Example 4
Source Project: CountryPicker   File: CountryPicker.java    License: Apache License 2.0 6 votes vote down vote up
/**
* Method to hide keyboard if it's open
*/
public void hideKeyboard() {
  try {
    Activity activity = getActivity();
    if (activity == null) {
      return;
    }
    InputMethodManager input
          = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    if (input == null) {
      return;
    }
    View currentFocus = activity.getCurrentFocus();
    if (currentFocus == null || currentFocus.getWindowToken() == null) {
      return;
    }

    input.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
  } catch (Exception e) {
      // ignoring any exceptions
  }
}
 
Example 5
Source Project: green_android   File: UI.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static void hideSoftKeyboard(final Activity activity) {
    if (activity == null)
        return;
    final InputMethodManager inputMethodManager =
        (InputMethodManager) activity.getSystemService(
            Activity.INPUT_METHOD_SERVICE);
    if (inputMethodManager == null || activity.getCurrentFocus() == null)
        return;
    inputMethodManager.hideSoftInputFromWindow(
        activity.getCurrentFocus().getWindowToken(), 0);
}
 
Example 6
Source Project: SimpleProject   File: InputMethodUtil.java    License: MIT License 5 votes vote down vote up
/**
 * 隐藏键盘
 * @param context
 */
public static void hideKeyboard(Activity context) {
	InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
	View view = context.getCurrentFocus();
	if (view != null && imm != null) {
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
	}
}
 
Example 7
Source Project: MHViewer   File: AppHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void hideSoftInput(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
 
Example 8
Source Project: EhViewer   File: AppHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static void hideSoftInput(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
 
Example 9
/**
 * Hides the onscreen keyboard if it is visisble
 */
public static void hideKeyboard(Activity activity) {
    // Check if no view has focus:
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
 
Example 10
Source Project: pandroid   File: DeviceUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void closeKeyboard(Activity activity) {
    if(activity == null)
        return;
    View view = activity.getCurrentFocus();
    if (view != null) {
        closeKeyboard(view);
    }
}
 
Example 11
public static void hideSoftKeyboard(@Nullable Activity activity) {
    if (activity != null) {
        View currentFocus = activity.getCurrentFocus();
        if (currentFocus != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
        }
    }
}
 
Example 12
Source Project: KUAS-AP-Material   File: Utils.java    License: MIT License 5 votes vote down vote up
public static void hideSoftKeyboard(@NonNull Activity activity) {
	InputMethodManager inputManager =
			(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	if (activity.getCurrentFocus() != null) {
		inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
 
Example 13
Source Project: imsdk-android   File: KPSwitchConflictUtil.java    License: MIT License 5 votes vote down vote up
/**
 * Hide the panel and the keyboard.
 *
 * @param panelLayout the layout of panel.
 */
public static void hidePanelAndKeyboard(final View panelLayout) {
    final Activity activity = (Activity) panelLayout.getContext();

    final View focusView = activity.getCurrentFocus();
    if (focusView != null) {
        KeyboardUtil.hideKeyboard(activity.getCurrentFocus());
        focusView.clearFocus();
    }else{
        KeyboardUtil.hideKeyboard(panelLayout);
    }

    panelLayout.setVisibility(View.GONE);
}
 
Example 14
Source Project: Kalle   File: ActivitySource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
void closeInputMethod() {
    Activity activity = getSource();
    View focusView = activity.getCurrentFocus();
    if (focusView != null) {
        InputMethodManager manager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (manager != null) {
            manager.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
        }
    }
}
 
Example 15
Source Project: GSYRickText   File: MVViewModel.java    License: MIT License 5 votes vote down vote up
/**
 * 隐藏软键盘
 */
private void hideKeyboard() {
    Activity context = (Activity) imvvmView.getContext();
    if (context.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
        if (context.getCurrentFocus() != null) {
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).
                    hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
 
Example 16
Source Project: KUtils-master   File: HideUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Forced hidden keyboard
 * @param activity
 */
public static void hideSoftKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    if (null != view) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
 
Example 17
Source Project: Travel-Mate   File: Utils.java    License: MIT License 5 votes vote down vote up
public static void hideKeyboard(Activity th) {
    View view = th.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) th.getSystemService(Context.INPUT_METHOD_SERVICE);
        Objects.requireNonNull(inputManager)
                .hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
 
Example 18
Source Project: Ruisi   File: KeyboardUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void hideKeyboard(Activity activity) {
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}
 
Example 19
Source Project: KUAS-AP-Material   File: Utils.java    License: MIT License 5 votes vote down vote up
public static void hideSoftKeyboard(@NonNull Activity activity) {
	InputMethodManager inputManager =
			(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
	if (activity.getCurrentFocus() != null) {
		inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
}
 
Example 20
public static void hideKeyBoard(Activity activity) {
    if ((activity.getCurrentFocus() != null) && (activity.getCurrentFocus().getWindowToken() != null)) {
        ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).
                hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
}