我在本机应用中通过Native UI Component使用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 }
但是当我看到控制台日志时,select事件就不存在了。这里有什么问题可以有人指出,拜托?