android - 键盘显示时如何调用函数

时间:2015-02-16 05:15:40

标签: android android-edittext

我的布局上有一个edittext视图。我想在键盘显示时调用一个函数,并在键盘隐藏时调用另一个函数。

我该怎么办?

3 个答案:

答案 0 :(得分:0)

您必须自己处理配置更改。

http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

样品:

// from the link above
@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();
    }
}

然后只需更改某些视图的可见性,更新字段并更改布局文件。

答案 1 :(得分:0)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        if(imm.isActive())
        {


            //call your function
        }

答案 2 :(得分:0)

     //Hide the keyboard
     ((Activity)mContext).getWindow()
     .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

public static void showKeyboard(Context mContext,EditText edittext){
    //Show the keyboard
    InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
}

public static void hideKeyboard(Context mContext,EditText edittext){
    //Hide the keyboard 
    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
}