繁体   English   中英

Android:点击按钮后隐藏键盘

[英]Android:Hide keyboard after button click

单击按钮后,我需要隐藏 android 键盘。

我已经看过很多关于如何执行此操作的示例,但是,它们似乎都使用特定的 editText 对象。

例如

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

我的问题是我正在动态构建屏幕,因此可能会有鬃毛编辑文本字段。 有没有一种方法可以隐藏键盘,而不必指定我为哪个 editText 对象隐藏它。

您可以改为将其设置为您的布局,即:

LinearLayout mainLayout;

// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);

// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);

这是一个示例,假设您的布局将被创建,而不管其上放置了多少EditText对象(或其他对象)。

编辑:另外,我发现非常有用的一点是确保在活动首次启动时隐藏键盘(即:如果EditText是第一个焦点)。 为此,我将其放在 Activity 的onCreate()方法中:

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

不要忘记使用 try catch 博客,因为如果您的键盘未打开并且如果您使用键盘隐藏代码应用程序将崩溃

try {
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
    // TODO: handle exception
}

您可以使用以下代码隐藏键盘,可能在Button click Event 上:

//================ Hide Virtual Key Board When  Clicking==================//

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);

//======== Hide Virtual Keyboard =====================//

如果问题出在活动上,则只需执行以下操作:

    try {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

否则,如果片段中需要代码,请执行以下操作

    try {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

这将处理在按钮单击或在事件块中写入时被视为特定的任何其他事件上的键盘隐藏。

记录并基于@burmat 和@Prashant Maheshwari Andro 的回答

假设您按如下方式调用单击按钮。 其中 buttonAndroidLogin_button 是 Button 对象。

protected void onCreate(Bundle savedInstanceState) {
    // your code...
    buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideKeyboard((Button)v);
            // ....
} // end onCreate

在活动上,您必须设置下一个方法

public void hideKeyboard(View view) {
    try {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch(Exception ignored) {
    }
}

它使用相同的按钮隐藏输入,因此我们不需要任何线性布局、文本视图或任何其他任意控件。 此外,该代码可重用于更多按钮。

您使用此代码

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);

在科特林:

在你的fragment

像这样创建扩展:

fun Fragment.hideKeyboard() {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}

然后像这样使用它:

hideKeyboard()

在您的activity

像这样创建扩展:

fun AppCompatActivity.hideKeyboard() {
    val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}

然后像这样使用它:

   hideKeyboard()
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);

暂无
暂无

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

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