Android 显示和隐藏软键盘的方法(手动)

时间:2022-04-29 09:29:42

在android开发中,经常会有一个需求,做完某项操作后,隐藏键盘,也即让android中的软键盘不显示。今天,和大家分享如何利用代码来实现对android的软件盘的隐藏、显示的操作。

Android 显示和隐藏软键盘的方法(手动)

1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)

?
1
2
inputmethodmanager imm = (inputmethodmanager) getsystemservice(context.input_method_service);
imm.togglesoftinput(0, inputmethodmanager.hide_not_always);

2、方法二(view为接受软键盘输入的视图,show_forced表示强制显示)

?
1
2
3
inputmethodmanager imm = (inputmethodmanager) getsystemservice(context.input_method_service);
imm.showsoftinput(view,inputmethodmanager.show_forced);
imm.hidesoftinputfromwindow(view.getwindowtoken(), 0); //强制隐藏键盘

3、调用隐藏系统默认的输入法

?
1
((inputmethodmanager)getsystemservice(context.input_method_service)).hidesoftinputfromwindow(widgetsearchactivity.this.getcurrentfocus().getwindowtoken(), inputmethodmanager.hide_not_always); (widgetsearchactivity是当前的activity)

4、获取输入法打开的状态

?
1
2
inputmethodmanager imm = (inputmethodmanager)getsystemservice(context.input_method_service);
boolean isopen=imm.isactive();//isopen若返回true,则表示输入法打开

以上所述是针对android 显示和隐藏软键盘的方法(手动),希望对大家有所帮助。