隐藏Android软键盘(如果已打开)

隐藏Android软键盘(如果已打开),第1张

概述我有三个编辑文本字段.在这些字段中,我想仅为第一个字段显示软输入键盘,而在后两个字段中禁用这些字段,这些是日期和时间字段.Edit-Text1//ShowthekeyboardEdit-Text2and3//Hidethekeyboard通过使用下面的代码,我可以禁用字段2和3的键盘,但是当用户将焦点放在字段1

我有三个编辑文本字段.在这些字段中,我想仅为第一个字段显示软输入键盘,而在后两个字段中禁用这些字段,这些是日期和时间字段.

Edit-Text 1 //Show the keyboardEdit-Text 2 and 3 //HIDe the keyboard

通过使用下面的代码,我可以禁用字段2和3的键盘,但是当用户将焦点放在字段1上时,键盘会出现,但是当用户点击字段2或3时,键盘不会隐藏.尽管字段2或3是首先敲击没有键盘出现.

//Code to disable soft input keyboardpublic static voID disableSoftinputFromAppearing(EditText editText) {    if (Build.VERSION.SDK_INT >= 11) {        editText.setRawinputType(inputType.TYPE_CLASS_TEXT);        editText.setTextIsSelectable(true);    } else {        editText.setRawinputType(inputType.TYPE_NulL);        editText.setFocusable(true);    }

如果软键盘已经打开,如何隐藏它?

解决方法:

//活动

public static voID hIDeSoftKeyboard(Activity activity) {   inputMethodManager inputMethodManager = (inputMethodManager)activity.getSystemService(Activity.input_METHOD_SERVICE);   inputMethodManager.hIDeSoftinputFromWindow(activity.getCurrentFocus().getwindowToken(), 0); }

//片段

public voID hIDeSoftKeyboard() {    inputMethodManager inputMethodManager = (inputMethodManager) getActivity().getSystemService(Activity.input_METHOD_SERVICE);    inputMethodManager.hIDeSoftinputFromWindow(getActivity().getCurrentFocus().getwindowToken(), 0);}

//如果编辑文本失去焦点,则隐藏键盘

edTxtMessage.setontouchListener(new VIEw.OntouchListener() {        @OverrIDe        public boolean ontouch(VIEw v, MotionEvent event) {            if (isEditable){                v.setFocusable(true);                v.setFocusableIntouchMode(true);            } else {                edTxtMessage.setFocusable(false);            }            return false;        }    });edTxtMessage.setonFocuschangelistener(new VIEw.OnFocuschangelistener({        @OverrIDe        public voID onFocusChange(VIEw vIEw, boolean b) {            if (!b){                hIDeKeyboard(getContext(), vIEw);            }        }    });private voID hIDeKeyboard(Context context, VIEw vIEw) {    if (vIEw != null) {        inputMethodManager imm = (inputMethodManager)context.getSystemService(Context.input_METHOD_SERVICE);        imm.hIDeSoftinputFromWindow(vIEw.getwindowToken(), 0);    }}
总结

以上是内存溢出为你收集整理的隐藏Android软键盘(如果已打开)全部内容,希望文章能够帮你解决隐藏Android软键盘(如果已打开)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1115767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存