英文:
How to open device's contact screen by swipe in react native
问题
我正在使用底部导航,当我通过滑动从拨号屏幕导航到联系人屏幕时,我希望直接打开手机的联系人屏幕。以下是ContactComponent文件和BottomNavigation文件的代码。
ContactComponent.js
const ContactComponents = ({ navigation }) => {
useEffect(() => {
Linking.openURL("content://com.android.contacts/contacts");
}, []);
}
export default ContactComponents
BottomNavigation.js
const Tab = createMaterialTopTabNavigator();
export default function BottomNavigation() {
return (
<Tab.Navigator
tabBarPosition='bottom'
initialRouteName='Dialer'
screenOptions={{
tabBarLabelPosition: "beside-icon",
tabBarIconStyle: { display: "none" },
tabBarStyle: { backgroundColor: bottomTabBarColor },
tabBarIndicatorStyle: { position: 'relative', borderWidth: 2, borderColor: bottomTabBarIndicatorColor }
}}
>
<Tab.Screen
name="Messages"
component={MessageComponent}
options={{
tabBarLabel: ({ focused, color, size }) => (
<Text style={[styles.textStyle, { color: focused ? focusedLabelColor : unfocusedLableColor, fontWeight: focused ? 'bold' : 'normal' }]>Messages</Text>
),
}}
/>
<Tab.Screen
name="Last Ones"
component={LastOnesComponent}
options={{
tabBarLabel: ({ focused, color, size }) => (
<Text style={[styles.textStyle, { color: focused ? focusedLabelColor : unfocusedLableColor, fontWeight: focused ? 'bold' : 'normal' }]>Last Ones</Text>
),
}}
/>
<Tab.Screen
name="Dialer"
component={Dialpad}
options={{
tabBarLabel: ({ focused, color, size }) => (
<Text style={[styles.textStyle, { color: focused ? focusedLabelColor : unfocusedLableColor, fontWeight: focused ? 'bold' : 'normal' }]>Dialer</Text>
),
}}
/>
<Tab.Screen
name="Contacts"
component={ContactComponents}
options={{
tabBarLabel: ({ focused, color, size }) => (
<Text style={[styles.textStyle, { color: focused ? focusedLabelColor : unfocusedLableColor, fontWeight: focused ? 'bold' : 'normal' }]}>Contacts</Text>
),
}}
/>
</Tab.Navigator>
);
}
[1]: https://i.stack.imgur.com/RQO02.jpg
英文:
I'm using bottom navigation , and when I navigate from Dialer screen to contacts screen by swiping, at that time I want to open phone's contact screen directly.
Below I have shared the code of ContactComponent file and BottomNavigation file.
I'm new in React Native, so please help me in this.
Thanks in advance.
ContactComponent.js
const ContactComponents = ({ navigation }) => {
useEffect(() => {
Linking.openURL("content://com.android.contacts/contacts")
}, []);
}
export default ContactComponents
BottomNavigation.js
const Tab = createMaterialTopTabNavigator();
export default function BottomNavigation() {
return (
<Tab.Navigator
tabBarPosition='bottom'
initialRouteName='Dialer'
screenOptions={{
tabBarLabelPosition: "beside-icon",
//tabBarLabelStyle: { fontWeight: "700", fontSize: 15, color : 'white'},
tabBarIconStyle: { display: "none" },
tabBarStyle :{backgroundColor : bottomTabBarColor},
tabBarIndicatorStyle : {position : 'relative', borderWidth : 2 , borderColor : bottomTabBarIndicatorColor}
}}>
<Tab.Screen
name="Messages"
component={MessageComponent}
options = {{
tabBarLabel: ({focused, color, size}) => (
<Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? 'bold' : 'normal' }] }>Messages </Text>
),
}}
/>
<Tab.Screen
name="Last Ones"
component={LastOnesComponent}
options = {{
tabBarLabel: ({focused, color, size}) => (
<Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? 'bold' : 'normal' }] }>Last Ones </Text>
),
}}
/>
<Tab.Screen
name="Dialer"
component={Dialpad}
options = {{
tabBarLabel: ({focused, color, size}) => (
<Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? 'bold' : 'normal' }] }>Dialer </Text>
),
}}
/>
<Tab.Screen
name="Contacts"
component={ContactComponents}
options = {{
tabBarLabel: ({focused, color, size}) => (
//<View style={focused ? styles.topTabLine : null}>
<Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? 'bold' : 'normal' }] }>Contacts </Text>
//</View>
),
}}
/>
</Tab.Navigator>
答案1
得分: 1
For Android, you can use native Linking
from react-native
to open the contacts app.
const openContacts = () => {
if (Platform.OS === 'android') {
Linking.openURL('content://com.android.contacts/contacts');
}
};
Now, if you want to open the contacts screen when the Dialer screen is focused, you can use the useFocusEffect
hook from @react-navigation/native
to perform that operation.
In the screen from which you want to navigate to contacts app, use this hook as shown.
import { useCallback } from 'react';
import { Linking, Platform } from 'react-native';
import { useFocusEffect } from '@react-navigation/native';
.
.
.
useFocusEffect(
useCallback(() => {
if (Platform.OS === 'android') {
Linking.openURL('content://com.android.contacts/contacts');
}
}, [])
);
Here's a Snack for the implementation.
英文:
For Android, you can use native Linking
from react-native
to open the contacts app.
const openContacts = () => {
if (Platform.OS === 'android') {
Linking.openURL('content://com.android.contacts/contacts');
}
};
Now, if you want to open the contacts screen when the Dialer screen is focused, you can use the useFocusEffect
hook from @react-navigation/native
to perform that operation.
In the screen from which you want to navigate to contacts app, use this hook as shown.
import { useCallback } from 'react';
import { Linking, Platform } from 'react-native';
import { useFocusEffect } from '@react-navigation/native';
.
.
.
useFocusEffect(
useCallback(() => {
if (Platform.OS === 'android') {
Linking.openURL('content://com.android.contacts/contacts');
}
}, [])
);
Here's a Snack for the implementation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论