2

我在我的本机应用程序中通过本机 UI 组件使用 Android 的 AutoCompleteTextview。我能够传递文本更改事件以响应本机应用程序,但无法传递项目单击事件以响应本机。

这是我的 JAVA 代码

 view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WritableMap event = Arguments.createMap();
                event.putString("selectedText", optionList.get(position));
                ReactContext reactContext = (ReactContext) view.getContext();
                reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                        view.getId(),
                        "topSelect",
                        event);

            }
        });

以下是我的反应本机代码

import PropTypes from 'prop-types';
import React from 'react'
import { requireNativeComponent, View } from 'react-native';

class AutoCompleteTextView extends React.Component {
  constructor(props) {
    super(props);
    this._onChange = this._onChange.bind(this);
    this._onSelect = this._onSelect.bind(this);
  }
  _onChange(event: Event) {
    console.log(event.nativeEvent.text);
    if (!this.props.onTextChange) {
      return;
    }
    this.props.onTextChange(event.nativeEvent.text);
  }
  _onSelect(event: Event) {
    console.log(event);
    if (!this.props.itemClickListener) {
      return;
    }
    this.props.itemClickListener(event.nativeEvent.selectedText);
  }
  render() {
    return <RCTAutoCompleteTextView {...this.props} onChange={this._onChange} onSelect={this._onSelect} />;
  }
}

AutoCompleteTextView.propTypes = {
  dataSource: PropTypes.array.isRequired,
  value: PropTypes.string.isRequired,
  onTextChange: PropTypes.func.isRequired,
  itemClickListener: PropTypes.func.isRequired,
  showDropDown: PropTypes.bool,
  showDropDownArrow: PropTypes.bool,
  hint: PropTypes.string,
  ...View.propTypes
};

var RCTAutoCompleteTextView = requireNativeComponent(`RCTAutoCompleteTextView`, AutoCompleteTextView, {
  nativeOnly: {onChange: true, onSelect: true}
});

export { AutoCompleteTextView }

但是当我看到控制台日志时,选择事件不存在。请问这里有什么问题可以指出吗?

4

0 回答 0