android中的键盘处理

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;">@Override
public 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;">@Override
public 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>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值