Android学习之Soft Keyboard使用文档翻译

个人爱好,权作参考,翻译不好,请勿见怪。


文章来源:

http://guides.codepath.com/android/Working-with-the-Soft-Keyboard

用软键盘工作

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" />

引用:

http://developer.android.com/training/keyboard-input/style.html
http://developer.android.com/training/keyboard-input/navigation.html
http://developer.android.com/training/keyboard-input/commands.html
http://developer.android.com/training/keyboard-input/visibility.html

后记:大家可能对上面的属性定义不太了解,我也记不住

详情请看http://blog.csdn.net/qq_16131393/article/details/50934240

谢谢,码字不易,且行且珍惜!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.安装 打开模拟器,adb install ADBKeyBoard.apk安装该输入法 2.设置 模拟器的settings-----language&input;-----勾选上ADB keyboard如下图 默认输入法也选择ADB keyboard,还有一个也要设置,很奇怪,不设置该项就无法成功输入中文,我也是无意中发现的,那就是选择默认输入法的时候,hardware physical keyboard得off,默认是on,如下图 好了接下来试试 adb shell am broadcast -a ADB_INPUT_TEXT --es msg '中文输入' 命令比较长,无碍 成功!不止中文(包括中文标点),其他语言也可以,日文亲测也可用,其他的还没试 再教一招:如何切换输入法 看到上图中放大镜上面的那个小键盘没有,往下拉就可以选择了 有用请支持,有问题请留言 adbkeyboard 通过亚洲开发银行的虚拟键盘输入 adbkeyboard是一个虚拟的键盘,从系统广播意图接收命令,你可以使用adb发送文本输入。 有一个“命令”命令“输入”,可以帮助您将文本输入发送到安卓系统。 keyEvent |用法:输入[文本] 输入文本 输入keyEvent 但是你不能使用这个命令将Unicode字符,因为它是不适合使用这种方式。 参考:http://stackoverflow.com/questions/14224549/adb-shell-input-unicode-character 例如 adb shell输入文本的你好嗎” 是不打算工作。 ADBKeyboard将有助于在这些情况下,特别是在自动化测试设备。 构建和安装APK 有一个设备或模拟器连接,使用这些简单的步骤来安装键盘: 获得来源:git clone https://github.com/senzhk/adbkeyboard.git 进入项目目录CD adbkeyboard

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值