android中涉及到的键盘输入处理:

android中用到键盘输入的地方就是EditText,我们在使用它的时候就要指定它接受输入的类型,有号码,email等等,指定了以后,当我们输入的时候,键盘就会自动的弹出我们需要的键盘类型。有时候我们还可以定义键盘中某些按键的文字,如Done,Next。

1.指定键盘类型:android:inputType这里的值也可以是组合的:phone,textPasswordtextCapSentences|textAutoCorrect 首字母大写,拼写检查

<span style="font-size:14px;"><EditText    android:id="@+id/phone"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:hint="@string/phone_hint"    android:inputType="phone" /> </span>

2.指名键盘上右下角按钮的含义,这样对用户有提示作用:android:imeOptions:可取值actionSend,actionSearch;然后可以监听该按键的动作:

<span style="font-size:14px;">EditText editText = (EditText) findViewById(R.id.search);editText.setOnEditorActionListener(new OnEditorActionListener() {    @Override    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {        boolean handled = false;        if (actionId == EditorInfo.IME_ACTION_SEND) {            sendMessage();            handled = true;        }        return handled;    }});</span>


3.控制输入法的可见性:
activity创建后,如果需要输入法弹出,可以在manifest下的activity指定 android:windowSoftInputMode="stateVisible"或者用java代码控制:

<span style="font-size:14px;">view.requestFocus();public void showSoftKeyboard(View view) {    if (view.requestFocus()) {        InputMethodManager imm = (InputMethodManager)                getSystemService(Context.INPUT_METHOD_SERVICE);        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);    }}</span>

一旦输入法可见后,你不应该通过程序来关闭它,应该交由用户来关闭。


4.当输入法弹出后 ,指定你的UI该如何响应:
虽然系统会帮我们处理当输入法弹出后,我们的app的UI该如何变化,但是系统的做法有时候并不能满足要求。例如经常会碰到输入法弹出后,我们的app里的输入框被输入法遮盖了,无法友好的输入了。这时候就要我们调整了。
解决办法是在manifest下的activity指定 android:windowSoftInputMode="adjustResize"


5.支持键盘导航:
有下面几个属性可以指定:
android:nextFocusForward 后面值取的是下个控件id
android:nextFocusUp
android:nextFocusDown
android:nextFocusLeft
android:nextFocusRight

<span style="font-size:14px;"><RelativeLayout ...>    <Button        android:id="@+id/button1"        android:layout_alignParentTop="true"        android:layout_alignParentRight="true"        android:nextFocusForward="@+id/editText1"        ... />    <Button        android:id="@+id/button2"        android:layout_below="@id/button1"        android:nextFocusForward="@+id/button1"        ... />    <EditText        android:id="@id/editText1"        android:layout_alignBottom="@+id/button2"        android:layout_toLeftOf="@id/button2"        android:nextFocusForward="@+id/button2"        ...  />    ...</RelativeLayout><Button    android:id="@+id/button1"    android:nextFocusRight="@+id/button2"    android:nextFocusDown="@+id/editText1"    ... /><Button    android:id="@id/button2"    android:nextFocusLeft="@id/button1"    android:nextFocusDown="@id/editText1"    ... /><EditText    android:id="@id/editText1"    android:nextFocusUp="@id/button1"    ...  /></span>


6.监听按键事件:
单个键的识别:
<span style="font-size:14px;">@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {    switch (keyCode) {        case KeyEvent.KEYCODE_D:            moveShip(MOVE_LEFT);            return true;        case KeyEvent.KEYCODE_F:            moveShip(MOVE_RIGHT);            return true;        case KeyEvent.KEYCODE_J:            fireMachineGun();            return true;        case KeyEvent.KEYCODE_K:            fireMissile();            return true;        default:            return super.onKeyUp(keyCode, event);    }}</span>

多个按键同时识别:在单个按键的基础上,在加一个 诸如isShiftPressed()来实现:

<span style="font-size:14px;">@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {    switch (keyCode) {        ...        case KeyEvent.KEYCODE_J:            if (event.isShiftPressed()) {                fireLaser();            } else {                fireMachineGun();            }            return true;        case KeyEvent.KEYCODE_K:            if (event.isShiftPressed()) {                fireSeekingMissle();            } else {                fireMissile();            }            return true;        default:            return super.onKeyUp(keyCode, event);    }}</span>







更多相关文章

  1. android EditText不自动弹出软键盘
  2. Android 4.0中按键的处理流程
  3. Android中单击空白区域隐藏键盘
  4. android按键 禁用
  5. android 软键盘自动弹出和关闭
  6. android隐藏以及显示软键盘以及不自动弹出键盘的方法
  7. Android 判断软键盘的状态(显示,隐藏)
  8. android 禁止系统软键盘,拦截键盘事件

随机推荐

  1. android触摸语音事件
  2. Android Studio 配置和技巧
  3. Android 漏洞分析入门 (一)
  4. 《Android开发从零开始》――9.Activity
  5. Android官方开发文档Training系列课程中
  6. Android中Canvas绘图方法的实现
  7. android widget组件之Button
  8. Android 蓝牙设备的查找与连接
  9. Android中SQLite数据库操作(2)——使用SQLi
  10. android fitSystemWindow属性