c# - Entry 获得焦点时隐藏键盘

标签 c# android xamarin keyboard android-softkeyboard

情况

我有一个使用外部键盘的 Android 应用程序,并且希望在 Entry 控件获得焦点时隐藏软键盘。

引用

正在关注 this android documentation它声明如下:

Note: If the user's device has an attached hardware keyboard, the soft input method does not appear.

但是,在某些 Android 设备中,当 Entry 获得焦点时,软输入会出现

在这些情况下我怎样才能隐藏软输入?

提前致谢!

最佳答案

您可以使用此代码隐藏软键盘

  getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

获取 EditText 的一个实例

EditText editText = (EditText) findViewById(R.id.edittext);

要防止默认软键盘出现在 EditText 中,请覆盖以下事件

//显示自定义键盘

    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) showCustomKeyboard(v);
            else hideCustomKeyboard();
        }
    });
    edittext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showCustomKeyboard(v);
        }
    });
    edittext.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            EditText edittext = (EditText) v;
            int inType = edittext.getInputType();       // Backup the input type
            edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
            edittext.onTouchEvent(event);               // Call native handler
            edittext.setInputType(inType);              // Restore input type
            return true; // Consume touch event
        }
    });
}
public void hideCustomKeyboard() {
    keyboardView.setVisibility(View.GONE);
    keyboardView.setEnabled(false);
}
public void showCustomKeyboard( View v) {
    keyboardView.setVisibility(View.VISIBLE);
    keyboardView.setEnabled(true);
    if( v!=null ){
        ((InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}
public boolean isCustomKeyboardVisible() {
    return keyboardView.getVisibility() == View.VISIBLE;
}
@Override public void onBackPressed() {
    if( isCustomKeyboardVisible() ) hideCustomKeyboard(); else this.finish();
}

免责声明:- 我已经在我的应用程序中实现了自定义键盘,并且我编写了本教程 - http://inducesmile.com/android/how-to-create-an-android-custom-keyboard-application/

关于c# - Entry 获得焦点时隐藏键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490974/

相关文章:

c# - MVVM 中的事件处理

c# - 使用 API 连接到远程 mysql 数据库

android - 窗口动画与 Android 5.0 上的导航栏重叠

c# - 为什么我会收到有关版本和区域性似乎相同的程序集引用的警告

C#计算美元面额

c# - IEnumerator 的随机顺序

java - 如何使用即将推出的 Scoped 存储通过 Room 在外部存储中写入 SQLite 文件?

android - Google Play 中具有相同名称但包名称不同的新应用

c# - 上下文操作菜单项在 iOS PCL 项目中不起作用

xamarin.ios - 使用 PCL 构建的 Servicestack monotouch DLL