本文出自:http://blog.csdn.net/hudashi/article/details/7029992 该博客中有大量的翻译文档!值得大家去看看
一、启动Activity时显示软件盘
启动Activity时显示软件盘,可以用如下的方式
首先得到InputMethodManage
InputMethodManager m = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
然后调用InputMethodManager的以下方法
boolean showSoftInput(View view, int flags, ResultReceiver resultReceiver)
boolean showSoftInput(View view, int flags)
void showSoftInputFromInputMethod(IBinder token, int flags)
public voidtoggleSoftInput(int showFlags, int hideFlags)
public voidtoggleSoftInputFromWindow(IBinder windowToken, int showFlags, int hideFlags)
但是上面InputMethodManager的方法在Actiivtiy的oncreate()和onResume()中调用会没有作用。
可能是因为View还没有准备好,所以不起作用。网上找来的理由说是:软件盘是要在所有view画完才能显示。
可以采用timer或Handler延迟在执行。
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run() {
InputMethodManager imm = (InputMethodManager)vv.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
Toast.makeText(test.this, "show", Toast.LENGTH_SHORT).show();
}
}, 1000);
调用InputMethodManager的以下方法可以关闭软键盘
void hideSoftInputFromInputMethod(IBinder token, int flags)
Close/hide the input method's soft input area, so the user no longer sees it or can interact with it.
boolean hideSoftInputFromWindow(IBinder windowToken, int flags)
Synonym for{@link#hideSoftInputFromWindow(IBinder, int, ResultReceiver) without a result: request to hide the soft input window from the context
of the window that is currently accepting input.
boolean hideSoftInputFromWindow(IBinder windowToken, int flags, ResultReceiver resultReceiver)
Request to hide the soft input window from the context of the window that is currently accepting input.
注意:关闭键盘的时候不再可以像开启一样传递View,而必须是View的IBinder,可以通过View的getWindowToken()得到相应的IBinder。
二、Eidtext可以自己自动地弹出软键盘
Eidtext这个控件默认是你点击了它获得焦点之后如果软键盘还没打开就会自动地弹出软键盘。
前提是你在竖屏的时候。但横盘的时候,不会自动弹出弹出软键盘了。
注意:这种情况下,Eidtext在这次事件中就不再进行光标定位的处理。
三、manifest中对软键盘弹出的属性进行设置
可以在activity的android:windowSoftInputMode属性进行设置
android:windowSoftInputMode=["stateUnspecified",
"stateUnchanged", "stateHidden",
"stateAlwaysHidden", "stateVisible",
"stateAlwaysVisible", "adjustUnspecified",
"adjustResize", "adjustPan"] >
android:windowSoftInputMode
How the main window of the activity interacts with the window containing the on-screen soft keyboard. The setting for this attribute affects two things:
* The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
* The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or
whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.
The setting must be one of the values listed in the following table, or a combination of one "state..." value plus one "adjust..." value.
Setting multiple values in either group — multiple "state..." values, for example — has undefined results. Individual values are separated by a vertical bar (|). For example:
<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.
Value Description
"stateUnspecified"The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an appropriate state or rely on the
setting in the theme.
This is the default setting for the behavior of the soft keyboard.
"stateUnchanged" The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.
"stateHidden"The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than
backs into it because of leaving another activity.
"stateAlwaysHidden" The soft keyboard is always hidden when the activity's main window has input focus.
"stateVisible" The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).
"stateAlwaysVisible" The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of
leaving another activity.
"adjustUnspecified" It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to
make the currentfocus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the
assumption that scrolling can make all of the window's contents visible within a smaller area.
This is the default setting for the behavior of the main window.
"adjustResize" The activity's main window is always resized to make room for the soft keyboard on screen.
这个可以让view自己调整大小以便显示软键盘。这样的话控件可能会变形。
"adjustPan" The activity's main window is not resized to make room for the soft keyboard.
Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing.
This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
这种方式是通过调整view的空白区域来显示软键盘。即使调整空白区域,软键盘还是有可能遮挡一些有内容区域。这样的话用户就只有退出软键盘才能看到这些被遮挡区域并进行交互。
官方相关文档地址:http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
如何监听软键盘是否打开?
到目前为止还没找到监听软键盘是否被开启的方法。
网上建议用onConfigurationChanged (Configuration newConfig)的来对Configuration的keyboardHidden来监听的方法在三星的手机上也无效。
public int keyboardHidden
Since: API Level 1
A flag indicating whether any keyboard is available.
Unlike hardKeyboardHidden, this also takes into account a soft keyboard,
so if the hard keyboard is hidden but there is soft keyboard available, it will be set to NO.
Value is one of: KEYBOARDHIDDEN_NO, KEYBOARDHIDDEN_YES.
注:在三星的手机上可以同过监听广播来监听软键盘的开启和隐藏。
分享到:
相关推荐
Android软件源码短信通讯录毕设项目带需求报告 Android软件源码类似时间轴记录的记账记事本 Android项目源码 studio和eclipse双版本仿bilibili客户端 Android项目源码GPS实时定位位置共享 Android项目源码QQ登录语音...
1 2 1 相关软件的下载与安装 1 2 2 虚拟设备的创建和使用 1 2 3 创建“Hello Android” 1 2 4 Android应用程序的调试 1 3 小结 第2章 Android应用程序的构成 2 1 Android应用程序的解析 2 1 1 目录结构 2 1 2 资源的...
4.4.2 秘诀36:使用复选框和开关按钮 86 4.4.3 秘诀37:使用单选按钮 90 4.4.4 秘诀38:创建下拉菜单 90 4.4.5 秘诀39:使用进度条 92 4.4.6 秘诀40:使用拖动条 94 第5 章 用户界面事件 97 5.1 事件处理器和事件...
(智能机适用),原手机不用改动,安装遥控软件+专用小U盘手机加遥控IC_HXD019,多设备万能+学习型,手机还可以当遥控器使用:可以遥控电视/机顶盒/空调/单反/投影仪/CD/功放/DVD/,风扇,遥控灯,遥控开关插座. ...
是款快速、功能强大、可自定义性高的侧边滑出式托盘应用,允许您将常用的应用和功能开关添加到侧边栏。无论您处在任何界面,只需滑动屏幕的边界,就能调出侧边栏来快速启动应用或打开/关闭系统功能
整合系统开关与天气预报功能的二合一时间插件,使桌面美观整洁。 体感拨号 自带精美拨号盘,可更换皮肤、定制外观,体验快捷拨号。 隐私保护 根据用户需求设置显示或隐藏的应用。开启此功能,保护自己...
解压「无线手写板鼠标」压缩包,安装其中的手机端软件“无线手写板鼠标.apk”到Android手机; 2. 电脑端软件“无线手写板鼠标.exe”无需安装,直接双击运行即可(若无法运行,请下载并安装“.NET Framework 2.0 ...
可以很方便的用手机控制自己家的电脑 GoToMyCloud更新内容如下: ...2. 修改开关一卡通与软件关机的功能; 3. 修改android和IOS的辅助键盘的功能; 4. 修改powerpoint本地输入法输入 5. 修改主控端界面和文件传输功能;
目前固件只支持Android系统共享网络功能,而支持iPhone需要集成的软件太多,空间有限,如果出8M固件 再考虑吧。 Android本来就支持WiFi热点,可能有点画蛇添足吧,呵呵。我是偶然发现openwrt还支持这个功能, ...
FABI由硬件模块(充当计算机鼠标和/或键盘的低成本微控制器)和用于配置所需功能的图形软件应用程序组成。 FABI系统可以通过USB连接到计算机,平板电脑或智能手机。 此外,蓝牙模块可用于没有USB端口的设备(某些...
///\ ///\ /////////\ ///\ //\\/ //\/ //\\\\\\\\//\ //\\/ //\/ //\/ //\\/ \\/ //\/ //\/ //\/ \//\ //\/ /////////////\/ \////
目前固件只支持Android系统共享网络功能,而支持iPhone需要集成的软件太多,空间有限,如果出8M固件 再考虑吧。 Android本来就支持WiFi热点,可能有点画蛇添足吧,呵呵。我是偶然发现openwrt还支持这个功能, ...
嵌入式系统PDA智能手机设计方案 Intel嵌入式设计开发者秘笈(精品) 纯免费android视频大全 软件测试中的性能测试的容量评估 【安博亚威】CCIE考试通过率第一! [OPENLAB]RHCE+架构师优惠2200元 数据库工程师 高级项目...