用软键盘工作
android系统会在屏幕上展示一个键盘,称之为软输入法,当一个文本字段在你的UI上获取到焦点时,提供更好的用户体验,你可以输入你期望的字符(例如手机号码或者邮箱地址),怎样让输入法更智能(例如当拼写错误时自动提示更正)
The Android system shows an on-screen keyboard, known as a soft input method, when a text field in your UI receives focus. To provide the best user experience, you can specify characteristics about the type of input you expect (such as whether it’s a phone number or email address) and how the input method should behave (such as whether it performs auto-correct for spelling mistakes).
显示软键盘
AVD Manager
通常,软键盘不会出现在模拟器上,如果你想测试软键盘,请确保打开android虚拟机管理器(Tools => Android => AVD Manager ) 在你的虚拟器中取消”Enable Keyboard Input”
By default, the soft keyboard may not appear on the emulator. If you want to test with the soft keyboard, be sure to open up the Android Virtual Device Manager ( Tools => Android => AVD Manager ) and uncheck “Enable Keyboard Input” for your emulator.
现在重启虚拟器
Genymotion
如果你正在用genymotion,你需要点击这个图标
在你的虚拟机图标上,选中Use virtual keyboard for text input 在你启动虚拟器之前。
If you are using Genymotion, you need to click on the wrench icon () on the emulator image and check Use virtual keyboard for text input before starting the emulator.
以编程方式展示软键盘
下面的代码显示软键盘将基于一个特定视图
The following code will reveal the soft keyboard focused on a specified view:
public void showSoftKeyboard(View view){
if(view.requestFocus()){
InputMethodManager imm =(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_IMPLICIT);
}
}
(当view获取焦点时,显示软键盘)
在编程中隐藏软件盘
你可以用 InputMethodManager来强制安卓隐藏虚拟键盘,称之为hideSoftInputFromWindow,在窗口中传递你的编辑字段。
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field.
public void hideSoftKeyboard(View view){
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
这将会在所有的情况下强制隐藏键盘。
This will force the keyboard to be hidden in all situations.
添加一个“Done”关键字
在键盘中,你可以隐藏“Next”按钮,添加“Done”按钮,相反为EditText视图添加imeOptions 属性
In the keyboard, you can hide the “Next” key and add “Done” instead by adding the following to the imeOptions for the EditText view:
<EditText
android:imeOptions="actionDone">
</EditText>
或者在java中如下设置
myEditText.setImeOptions(EditorInfo.IME_ACTION_DONE);
配置软键盘模式
Configuring the Soft Keyboard Mode
软键盘可以在每个活动的清单文件中(AndroidManifest.xml )配置,用android:windowSoftInputMode属性来调整默认的可见性和当显示的时候键盘如何影响UI
The soft keyboard can be configured for each activity within the AndroidManifest.xml file using the android:windowSoftInputMode attribute to adjust both default visibility and also how the keyboard affects the UI when displayed.
当活动开启时显示软键盘
Showing the Keyboard when Activity Starts
尽管android在活动启动时给了一个焦点给你布局中的一个文本字段,它不显示软键盘,在活动启动时显示软键盘,在清单文件的标签添加android:windowSoftInputMode属性,设置”stateVisible”
Although Android gives focus to the first text field in your layout when the activity starts, it does not show the soft keyboard. To show the keyboard when your activity starts, add the android:windowSoftInputMode attribute to the element with the “stateVisible” value within the Android manifest. Check out this guide for more details. Within the AndroidManifest.xml file:
<activity
android:name="com.example.myactivity"
android:windowSoftInputMode="stateVisible" />
这个模式包含两个方面,键盘的可视性,和UI的调整,可见性选项包括 include stateUnchanged , stateHidden , stateVisible 。
The options for the mode include two aspects: visibility of the keyboard and adjustment of the UI. Visibility options include stateUnchanged , stateHidden , stateVisible and [several others](listed in full here.
改变UI反应
虚拟键盘减少了你App空间的可见性,我们可以在标签中设置这个相同的属性android:windowSoftInputMode 来让软件盘移动视图元素(顶起软键盘)
The virtual keyboard reduces the amount of space available for your app’s UI. We can also use this same android:windowSoftInputMode property within the note to change the way that the soft keyboard displaces the view elements when appearing within the AndroidManifest.xml file:
<!-- Configures the UI to be resized to make room for the keyboard -->
<activity
android:name="com.example.myactivity"
android:windowSoftInputMode="adjustResize" />
这个选项包括可见性和可调整性,调整选项包括adjustResize , adjustPan , 和adjustUnspecified 。
The options for the mode include two aspects: visibility and adjustment. Adjustment options include adjustResize , adjustPan , and adjustUnspecified and are [listed in full here]. Both visibility and adjustment can be combined with:
<!-- Configures the keyboard to be visible right away and for UI to be resized when shown -->
<activity
android:name="com.example.myactivity"
android:windowSoftInputMode="stateVisible|adjustResize" />