android中的键盘处理

本文详细介绍了Android中键盘输入处理的方法,包括如何指定键盘类型、设置键盘按钮功能、控制输入法显示与隐藏、调整UI响应输入法弹出、支持键盘导航及监听按键事件等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>







Android 系统的键盘事件 是由InputManagerService 来监控的, 而InputManagerService 是由 SystemServer 来启动的 创建了一个类的对象 WindowInputEvent Receiver InputEventRe ceiver的构造 函数 WindowInputEventReceiver extends InputEventReceiver nativeInit InputEventReceiver.cpp nativeInit android_view_InputE ventReceiver.cpp NativeInputE ventReceiver 的构造函数 LooperCallback: :handleEvent NativeInputEve ntReceiver::ha ndleEvent Looper::p ollInner Looper::p ollOnce NativeInputEventRecei ver : public LooperCallback 有方法 NativeInputEventReceiver:: handleEvent 没有键盘事件发生, InputManager 中的InputReader 正在睡眠等待键盘事件 的发生,而InputManager 中的 InputDispatcher 正在等待InputReader 从睡眠中醒过来并且唤醒它,而应用程 序也正在消息循环中等待InputDispatcher 从睡眠中醒过来并且唤醒它。 分析InputManager 分发键盘消息给应用程序的过程 InputReader .pollOnce EventHub.g etEvent InputReader::pr ocessEventsLock ed InputReader::pr ocessEventsForD eviceLocked InputDevic e::process 没有键盘事件发生, InputReaderThread 线程就会睡眠在EventHub.getEvent 函数 上,当键盘事件发生后,就会把这个事件封 装成一个RawEvent 对象,然后返回到 pollOnce函数中,执行此函数 有键盘事件 InputReader.cpp InputMapper::process( 这里 是KeyboardInputMapper) InputReader::createDeviceLock ed中根据类型创建的,负责处理轨迹 球事件的TrackballInputMapper 对 象以及负责处理触摸屏事件的 TouchInputMapper 对象等 KeyboardInputMap per::processKey 这个函数首先对对按 键作一些处理,例如 需要根据当时屏幕的 方向来调整键盘码 InputDispatcher::notify Key( 继承自 InputListenerInterface) KeyboardInputMappge r函数通知 InputDispatcher ,有 键盘事件发生了 1. 调用 validateKeyEvent 函数来验 证action参数是否正确 2. 参数action检查通过后,还通过 policyFlags 参数来检查一下同时是 否有ALT和SHIFT键被按下 3. 调用 enqueueInboundEventLocked 函数 把这个按键事件封装成一
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值