简体   繁体   English

单击edittext后如何隐藏键盘

[英]How to hide keyboard after edittext click

I have a edittext in my android app where I can put a person birthdate.我的 android 应用程序中有一个 edittext,我可以在其中放置一个人的生日。 When I click on it a dialog fragment appears, but I have to click two times.当我点击它时会出现一个对话框片段,但我必须点击两次。 After first click there is an keyboard and I don't want it.第一次点击后有一个键盘,我不想要它。

I know that I can use button instead, but i prefer editext, because it looks better :) How can I change it?我知道我可以使用按钮代替,但我更喜欢editext,因为它看起来更好:) 我该如何更改它? Can I open diagram in first edittext click?我可以在第一次编辑文本单击中打开图表吗? Thanks in advance!提前致谢!

You can make your EditText not focusable by adding android:focusable="false" in XML declaration and then use simple onClickListener on the EditText.您可以通过在 XML 声明中添加android:focusable="false"然后在 EditText 上使用简单的 onClickListener 来使您的 EditText 不可聚焦。

Else, you can also use the following method to hide the keyboard:否则,您还可以使用以下方法隐藏键盘:

public void hideSoftKeyBoard(Activity activity) {
    // Check if no view has focus:
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

This can be invoked as follows: hideSoftKeyBoard(this) from an activity or hideSoftKeyBoard(getActivity()) from a fragment.这可以按如下方式调用:来自活动的hideSoftKeyBoard(this)或来自片段的hideSoftKeyBoard(getActivity())

Hope this helps!希望这可以帮助!

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

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