显示/隐藏基于软键盘的视图

时间:2015-02-24 12:52:41

标签: android android-layout

我遇到了困难。我需要在显示软键盘时隐藏某些视图,并在软键盘隐藏时显示回来。

我尝试过不同的方法,通过解决方法检测软键盘的可见性,并相应地显示/隐藏视图,但事务处理不顺畅,整个布局在显示时闪烁。

下面是类似于我想要的东西。

softkeyboard hidden softkeyboard visible

是否还有其他类似的替代方法,而无需解决软键盘的可见性问题?

感谢。

3 个答案:

答案 0 :(得分:0)

我认为你可能想要扭转局面,而不是检测软键盘的可见性,然后控制它。

我正在使用的代码是:

public static void showKeyboard(Activity activity) {
    if (activity == null || activity.getCurrentFocus() == null)
        return;

    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    // will only trigger if no physical keyboard is open
    inputMethodManager.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
}

public static void hideKeyboard(Activity activity) {
    if (activity == null || activity.getCurrentFocus() == null)
        return;

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(
            Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

我还有第三种隐藏键盘的方法。在一个对话框中需要这个,这个对话框显示了一个edittext,当点击'Ok / cancel'时强制键盘关闭:

public static void hideKeyboard(Context c, IBinder windowToken) {
    InputMethodManager mgr = (InputMethodManager) c
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(windowToken, 0);
}

用法:

Util.hideKeyboard(getActivity(), editText.getWindowToken());

答案 1 :(得分:0)

不知道"某些观点"意思是更难以回答。您可以使用任何软输入模式平移/调整大小等,还是需要更多自定义行为?

您可以在视图根目录上使用OnGlobalLayoutListener等变通方法技巧,但由于它经常被调用,因此成本很高。 InputMethodManager上有一些方法可以请求显示/隐藏IME,并使用结果调用回调,但它并不总是可靠的。您的用例是一个非常常见的问题,但根据您要完成的内容,很难推荐任何内容。

答案 2 :(得分:0)

我通过覆盖EditText并向其添加一个OnCloseSoftKeyboardListener接口解决了类似的问题,该接口在软键盘关闭时调用:

public class MyEditText extends EditText {

private OnCloseSoftKeyboardListener mBackButtonListener;
private OnTouchListener mTouchListener;

// HANDLING OF SOFT KEYBOARD BACK BUTTON
public void setOnCloseSoftKeyboardListener(OnCloseSoftKeyboardListener callback) {
    mBackButtonListener = callback;
}


/**
 * Overrides the handling of the back key to move fields or whatever, instead of dismissing the input method.
 */
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (mBackButtonListener != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
        if (mBackButtonListener.onCloseSoftKeyboard(this)) {
            return true;
        }
    }

    return super.dispatchKeyEventPreIme(event);
}


public interface OnCloseSoftKeyboardListener {

    /**
     * @param myEditText 
     * @return true if event was consumed, false otherwise
     */
    boolean onCloseSoftKeyboard(EditText view);
}
}

在您的活动中,您实现了界面:

public class MyActivity implements OnCloseSoftKeyboardListener {

MyEditText mTextView; 
(...)

mTextView.setOnCloseSoftKeyboardListener(this);
(...)

@Override
public boolean onCloseSoftKeyboard(EditText view) {

    // do what you need to do...

    return false;
}

如果您还在EditText中添加OnTouchListener,则可以同时处理softKeyboard的打开和关闭。

相关问题