下面列出了android.view.inputmethod.InputMethodManager#isActive ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updateInputState() {
// Make sure that if the user changes the value and the IME is active
// for one of the inputs if this widget, the IME is closed. If the user
// changed the value via the IME and there is a next input the IME will
// be shown, otherwise the user chose another means of changing the
// value and having the IME up makes no sense.
InputMethodManager inputMethodManager = InputMethodManager.peekInstance();
if (inputMethodManager != null) {
if (inputMethodManager.isActive(mYearSpinnerInput)) {
mYearSpinnerInput.clearFocus();
inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
} else if (inputMethodManager.isActive(mMonthSpinnerInput)) {
mMonthSpinnerInput.clearFocus();
inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
} else if (inputMethodManager.isActive(mDaySpinnerInput)) {
mDaySpinnerInput.clearFocus();
inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
}
}
}
private void updateInputState() {
// Make sure that if the user changes the value and the IME is active
// for one of the inputs if this widget, the IME is closed. If the user
// changed the value via the IME and there is a next input the IME will
// be shown, otherwise the user chose another means of changing the
// value and having the IME up makes no sense.
InputMethodManager inputMethodManager = InputMethodManager.peekInstance();
if (inputMethodManager != null) {
if (inputMethodManager.isActive(mHourSpinnerInput)) {
mHourSpinnerInput.clearFocus();
inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
} else if (inputMethodManager.isActive(mMinuteSpinnerInput)) {
mMinuteSpinnerInput.clearFocus();
inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
} else if (inputMethodManager.isActive(mAmPmSpinnerInput)) {
mAmPmSpinnerInput.clearFocus();
inputMethodManager.hideSoftInputFromWindow(mDelegator.getWindowToken(), 0);
}
}
}
/**
* Keyboard back button
*/
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (mKeyboardMandatory) {
if (getContext() != null) {
InputMethodManager imm = (InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
setImeVisibility(true);
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
public static void hideSoftKeyBoard(Activity activity) {
try {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null && imm.isActive() && activity.getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void openSoftKeyBoard(Activity activity) {
try {
InputMethodManager imm =
(InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive() && activity.getCurrentFocus() != null) {
imm.showSoftInput(activity.getCurrentFocus(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void HideKeyboard(View v)
{
InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
if ( imm.isActive( ) ) {
imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );
}
}
/**
* Hides the soft input if it is active for the input text.
*/
private void hideSoftInput() {
InputMethodManager inputMethodManager = InputMethodManager.peekInstance();
if (inputMethodManager != null && inputMethodManager.isActive(mInputText)) {
inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
}
if (mHasSelectorWheel) {
mInputText.setVisibility(View.INVISIBLE);
}
}
public static void hideKeyboard(View view) {
if (view == null) {
return;
}
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (!imm.isActive()) {
return;
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
/**
* Hides the soft input if it is active for the input text.
*/
private void hideSoftInput() {
InputMethodManager inputMethodManager = getValidInputMethodManager();
if (inputMethodManager != null && inputMethodManager.isActive(mInputText)) {
inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
if (mHasSelectorWheel) {
mInputText.setVisibility(View.INVISIBLE);
}
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
pressSearch = (keyCode == KeyEvent.KEYCODE_ENTER);
if (pressSearch && listener != null) {
/*隐藏软键盘*/
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
listener.onSearchClick(v);
}
return false;
}
/**
* 输入法是否显示
*/
public static boolean isKeybord(EditText edittext) {
boolean bool = false;
InputMethodManager inputMethodManager = (InputMethodManager)
edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive()) {
bool = true;
}
return bool;
}
@Override
public void closeKeyboard(View view) {
InputMethodManager manager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager.isActive()) {
manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* 显示软键盘
* @param activity
*/
public static void showKeyboard(Activity activity) {
if(activity != null){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if(!imm.isActive()){
imm.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
public static void hideKeyboard(View view) {
if (view == null) {
return;
}
try {
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (!imm.isActive()) {
return;
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {
FileLog.e(e);
}
}
/**
* 隐藏键盘
*
* @param ctx
* @param v
* @return
*/
public static boolean hideSoftInputFromWindow(Context ctx, View v) {
InputMethodManager imm = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
return true;
}
return false;
}
public static boolean KeyBoard(EditText edittext)
{
boolean bool = false;
InputMethodManager imm = ( InputMethodManager ) edittext.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
if ( imm.isActive( ) )
{
bool = true;
}
return bool;
}
/**
* 隐藏软键盘
*/
public static void hideKeyboard(Context context)
{
Activity activity = (Activity) context;
if (activity != null)
{
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive() && activity.getCurrentFocus() != null)
{
imm.hideSoftInputFromWindow(activity.getCurrentFocus()
.getWindowToken(), 0);
}
}
}
public void onClickClose(View view) {
InputMethodManager input = (InputMethodManager) binding.pinEntry.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (input != null && input.isActive()) {
input.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
dismiss();
}
public static boolean isKeyboardShowed(View view) {
if (view == null) {
return false;
}
try {
InputMethodManager inputManager = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
return inputManager.isActive(view);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean isActive(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.isActive();
}