android软键盘的用法总结

摘要:
“状态未指定”:未指定软键盘的状态。系统将选择适当的状态或其中的设置。这是软键盘状态的默认设置。未指定软键盘的状态。“stateUnchanged”(状态未更改):当活动开始时,软键盘会被认为其状态是持久的,无论是可见的还是隐藏的。软键盘保持在最后一个状态。“stateHidden”(状态隐藏):当用户选择某项活动时,软键盘将隐藏,也就是说,当用户确定要浏览某项活动,而不是因为退出某项活动而返回该活动。当用户选择活动时,软键盘将隐藏。“stateAlwaysHidden”:当活动的主窗口具有输入焦点时,软键盘始终隐藏。软键盘始终隐藏。“stateVisible”:软键盘在正常情况下可见。)活动的主窗口不适合使用软键盘。相反,Windows的内容会自动切换,以确保当前焦点不会被键盘遮挡,用户可以查看他们正在键入的内容。这通常需要调整大小,因为用户可能需要关闭软键盘才能与窗口的隐藏部分接触。主活动窗口不会调整屏幕大小,以便为软键盘留出空间。这通常不会比调整大小更大,因为用户可能会关闭软键盘,以便与覆盖的内容进行交互。此时,您需要聆听软键盘的显示和隐藏。

1.软键盘的显示原理
       软键盘其实是一个Dialog。InputMethodService为我们的输入法创建了一个Dialog,并且对某些参数进行了设置,使之能够在底部 或者全屏显示。当我们点击输入框时,系统会对当前的主窗口进行调整,以便留出相应的空间来显示该Dialog在底部,或者全屏。

2.活动主窗口调整
       Android定义了一个属性windowSoftInputMode, 用它可以让程序控制活动主窗口调整的方式。我们可以在配置文件AndroidManifet.xml中对Activity进行设置。这个属性的设置将会影响两件事情:
       1>软键盘的状态——隐藏或显示。
       2>活动的主窗口调整——是否减少活动主窗口大小以便腾出空间放软键盘或是否当活动窗口的部分被软键盘覆盖时它的内容的当前焦点是可见的。
       故该属性的设置必须是下面列表中的一个值,或一个“state…”值加一个“adjust…”值的组合。在任一组设置多个值,各个值之间用|分开。
"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. 当用户选择该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. 当用户选择这个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. 它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种 主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区 域中可见的。这个是主窗口默认的行为设置。也就是说,系统自动决定是采用平移模式还是压缩模式,决定因素在于内容是否可以滚动。
"adjustResize": (压缩模式)The activity's main window is always resized to make room for the soft keyboard on screen. 当软键盘弹出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。
"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. 该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容 的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。。

3.侦听软键盘的显示隐藏
       有时候,借助系统本身的机制来实现主窗口的调整并非我们想要的结果,我们可能希望在软键盘显示隐藏的时候,手动的对布局进行修改,以便使软键盘弹出时更加美观。这时就需要对软键盘的显示隐藏进行侦听。
       我们可以借助软键盘显示和隐藏时,对主窗口进行了重新布局这个特性来进行侦听。如果我们设置的模式为压缩模式,那么我们可以对布局的onSizeChanged函数进行跟踪,如果为平移模式,那么该函数可能不会被调用。

4.EditText默认不弹出软件键盘
方法一:
在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden
例如:<activity android:name=".Main"
                  android:label="@string/app_name"
                  android:windowSoftInputMode="adjustUnspecified|stateHidden"
                  android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
方法二:
让EditText失去焦点,使用EditText的clearFocus方法
例如:EditText edit=(EditText)findViewById(R.id.edit);
           edit.clearFocus();
方法三:
强制隐藏Android输入法窗口
例如:EditText edit=(EditText)findViewById(R.id.edit);  
           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

5.EditText始终不弹出软件键盘
例:EditText edit=(EditText)findViewById(R.id.edit);
       edit.setInputType(InputType.TYPE_NULL);

免责声明:文章转载自《android软键盘的用法总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇python计算前一天、前一小时、前一分钟、前一秒的时间钉钉开发系列(十二)机器人下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Android中节操播放器JieCaoVideoPlayer使用

效果 使用 即便是自定义UI,或者对Library有过修改,也是这五步骤来使用播放器。 1.添加类库 compile 'cn.jzvd:jiaozivideoplayer:6.0.0' 2.添加布局 <cn.jzvd.JZVideoPlayerStandard android: android:layout_width="matc...

Android中ActionBar的使用

ActionBar是一个显示在屏幕顶部的控件,它包括了在左边显示的应用的logo图标和右边操作菜单的可见项。 ActionBar的基本操作 启用ActionBar Android3.0版本已经默认使用了ActionBar,因此只要在Mainifest.xml中配置的targetSdkVersion高于11(Android3.0),则默认会使用ActionB...

获取apk的Activity和Package

2.查看包名和activity adb logcat|grep -i activitymanager 获取当前界面的activity 1.adb shell dumpsys window windows 2.adb shell dumpsys window windows| grep mCurrentFocus 3.获取Package aapt d...

位置动画Android的Activity屏幕切换动画(一)左右滑动切换

查了好多资料,发现还是不全,干脆自己理整吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧!     在Android发开中程过,经常会到碰Activity之间的换切效果的题问,上面绍介一下如何实现阁下滑动的换切效果,首先懂得一下Activity换切的实现,从Android2.0开始在Activity增加了一个方法:     public v...

unity重写软键盘for Android NGUI

一 原因 以为某些原因,需要重写Unity游戏中Android的软键盘的样式,然而unity对android和ios的ios的软键盘都有封装,在Unity中TouchScreenKeyboard.Open可以打开一个软键盘,然而对这个软键盘的操作函数少的可怜,所以有了这篇文章. 二 目的 本文的主要目的是重写NGUI的UIInput,也就是TouchScr...

android开机自启动

原理,在收到系统开机广播后,启动一个透明的activity,在activity里面启动一个服务。 关键代码如下: 1、开机广播接受者 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 publicclassBootReceiver extendsB...