您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在React Native中,可以使用react-navigation
库来实现导航栏搜索框。以下是一个简单的示例:
react-navigation
库及其相关依赖。如果尚未安装,可以使用以下命令进行安装:npm install @react-navigation/native @react-navigation/stack
SearchBar.js
组件,用于实现搜索框:import React, { useState } from 'react';
import { View, TextInput, StyleSheet } from 'react-native';
const SearchBar = ({ onSearch }) => {
const [searchTerm, setSearchTerm] = useState('');
const handleSearch = () => {
onSearch(searchTerm);
};
return (
<View style={styles.searchContainer}>
<TextInput
style={styles.searchInput}
placeholder="搜索"
value={searchTerm}
onChangeText={setSearchTerm}
/>
<Button title="搜索" onPress={handleSearch} />
</View>
);
};
const styles = StyleSheet.create({
searchContainer: {
flexDirection: 'row',
alignItems: 'center',
backgroundColor: '#f5f5f5',
paddingHorizontal: 10,
},
searchInput: {
flex: 1,
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginRight: 10,
borderRadius: 4,
},
});
export default SearchBar;
App.js
)中,设置导航栏并使用SearchBar
组件:import React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import SearchBar from './SearchBar';
const Stack = createStackNavigator();
const HomeScreen = ({ navigation }) => {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<SearchBar onSearch={(searchTerm) => navigation.navigate('Results', { searchTerm })} />
</View>
);
};
const ResultsScreen = ({ route }) => {
const { searchTerm } = route.params;
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>搜索结果:{searchTerm}</Text>
</View>
);
};
const App = () => {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="首页" component={HomeScreen} />
<Stack.Screen name="结果" component={ResultsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;
现在,当用户在搜索框中输入文本并点击搜索按钮时,应用程序将导航到ResultsScreen
,并显示搜索结果。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。