React Native KeyboardDidShow/Hide事件在带有adjustResize设置的Android上不起作用

pkmbmrz7  于 2023-08-07  发布在  React
关注(0)|答案(3)|浏览(110)

我正在尝试使用React Native在Android上捕获键盘显示/隐藏事件上的事件。我被困在了一个死胡同里。
以下是我的清单设置:

<activity
    android:launchMode="singleTop"
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
    </intent-filter>
</activity>

字符串
下面是我的RN组件:

import React, {Component} from 'react';
import {
    View,Keyboard
} from 'react-native';

export default class KeyboardAwareComponent extends Component {
    componentDidMount() {
        Keyboard.addListener("keyboardDidShow",()=>{
            console.log("Show event");
        })
    }

    render() {
        return <View />
    }
}


非常感谢提前:)

kh212irz

kh212irz1#

这里有一些事件直接从文档。
键盘将显示
键盘显示
键盘将隐藏
键盘隐藏
键盘将更改帧
键盘DidChangeframe

import React, { Component } from 'react';
import { Keyboard, TextInput } from 'react-native';

class Example extends Component {
  componentWillMount () {
    this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
    this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
  }

  componentWillUnmount () {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
  }

  _keyboardDidShow () {
    alert('Keyboard Shown');
  }

  _keyboardDidHide () {
    alert('Keyboard Hidden');
  }

  render() {
    return (
      <TextInput
        onSubmitEditing={Keyboard.dismiss}
      />
    );
  }
}

字符串

daolsyd0

daolsyd02#

TL/DR:
确保您使用的视图不是由ScrollView包围的
我发现Keyboard事件要工作,必须在Android上计算屏幕的正确可见部分,当视图使用ScrollView时,这并不好用。

bxpogfeg

bxpogfeg3#

对我来说,问题是我使用的是FLAG_LAYOUT_NO_LIMITS,它不能与adjustPanadjustResize一起工作
MainActivity里面我替换了这个

Window w = getWindow(); // in Activity's onCreate() for instance
      w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

字符串
用这个:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        Window w = getWindow();
        w.setStatusBarColor(Color.TRANSPARENT);
        w.setNavigationBarColor(Color.TRANSPARENT);
        w.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        super.onCreate(savedInstanceState);
    }


类似的行为。
以下是与问题/解决方案相关的几个链接:
adjustPan not working with FLAG_LAYOUT_NO_LIMITS的数据。
https://github.com/th3rdwave/react-native-safe-area-context/issues/8
https://unicorn-utterances.com/posts/draw-under-navbar-using-react-native/

相关问题