简体   繁体   English

Android轻触键盘

[英]Android soft touch keyboard

I'm new to programming with Android and am trying to figure out how to get the soft touch keyboard to appear when an EditText item is clicked in, (when the activity loads there is only one user input field, so it stays in focus) so far I have the below method I made however when it shows the keyboard, the keyboard cannot be closed by the user... the button to close the keyboard is there however it does not hide it. 我是Android编程的新手,正在尝试找出如何使软触摸键盘在单击EditText项时出现(当活动加载时只有一个用户输入字段,因此它保持焦点)到目前为止,我有以下方法,但是当它显示键盘时,用户无法关闭键盘...关闭键盘的按钮在那里,但是它没有将其隐藏。

public void showKeyboard(View view) {
    InputMethodManager myKeyboard = (InputMethodManager)getSystemService(Context
        .INPUT_METHOD_SERVICE);
    myKeyboard.showSoftInput(PN_input,InputMethodManager.SHOW_IMPLICIT);
}

Any help or point in the right direction would be helpful :) 任何帮助或指出正确的方向将是有帮助的:)

Edit: Hide keyboard method I'm using when the find button is clicked 编辑:单击查找按钮时隐藏我正在使用的键盘方法

public void hideKeyboard() {
    InputMethodManager myKeyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    myKeyboard.hideSoftInputFromWindow(PN_input.getWindowToken(),0);
}

Put this to your Activity onCreate to hide softkey when Activity starts below code hides your softkey. 将其放在您的Activity onCreate上时,当Activity在代码下方开始时,将隐藏软键。

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);// for hide keypad

When you touch on EditText, automatically softkey will appears, No need of Code for do this, for hiding softkey touch anywhere inside the screen. 当您触摸EditText时,将自动显示软键,无需执行此操作即可隐藏屏幕内任何位置的软键。

For Show KeyPad use this code : 对于Show KeyPad,请使用以下代码:

 InputMethodManager imm = (InputMethodManager)
                                 getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
    }

For Hide Keypad use this code : 对于隐藏键盘,请使用以下代码:

 InputMethodManager imm = (InputMethodManager)
                                  getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

From digging in the android developer website, I was able to create a method that works to show the keyboard and the 'done' button works to close the keyboard. 通过在android开发人员网站中进行挖掘,我能够创建一种方法来显示键盘,而“完成”按钮可以关闭键盘。

    InputMethodManager myKeyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    myKeyboard.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html https://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM