如何在React Native中通过滑动打开设备的联系人屏幕

huangapple go评论49阅读模式
英文:

How to open device's contact screen by swipe in react native

问题

如何在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
英文:

如何在React Native中通过滑动打开设备的联系人屏幕

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 }) =&gt; {
useEffect(() =&gt; {
Linking.openURL(&quot;content://com.android.contacts/contacts&quot;)
}, []);
}
export default ContactComponents

BottomNavigation.js

const Tab = createMaterialTopTabNavigator();
export default function BottomNavigation() {
return (
&lt;Tab.Navigator
tabBarPosition=&#39;bottom&#39;
initialRouteName=&#39;Dialer&#39;
screenOptions={{ 
tabBarLabelPosition: &quot;beside-icon&quot;,
//tabBarLabelStyle: { fontWeight: &quot;700&quot;, fontSize: 15, color : &#39;white&#39;},
tabBarIconStyle: { display: &quot;none&quot; },
tabBarStyle :{backgroundColor : bottomTabBarColor},
tabBarIndicatorStyle : {position : &#39;relative&#39;, borderWidth : 2 , borderColor : bottomTabBarIndicatorColor}
}}&gt;
&lt;Tab.Screen 
name=&quot;Messages&quot; 
component={MessageComponent} 
options = {{
tabBarLabel: ({focused, color, size}) =&gt; (
&lt;Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? &#39;bold&#39; : &#39;normal&#39; }] }&gt;Messages &lt;/Text&gt;
),
}}
/&gt;
&lt;Tab.Screen 
name=&quot;Last Ones&quot; 
component={LastOnesComponent} 
options = {{
tabBarLabel: ({focused, color, size}) =&gt; (
&lt;Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? &#39;bold&#39; : &#39;normal&#39; }] }&gt;Last Ones &lt;/Text&gt;
),
}}
/&gt;
&lt;Tab.Screen 
name=&quot;Dialer&quot; 
component={Dialpad} 
options = {{
tabBarLabel: ({focused, color, size}) =&gt; (
&lt;Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? &#39;bold&#39; : &#39;normal&#39; }] }&gt;Dialer &lt;/Text&gt;
),
}}
/&gt;
&lt;Tab.Screen 
name=&quot;Contacts&quot; 
component={ContactComponents} 
options = {{
tabBarLabel: ({focused, color, size}) =&gt; (
//&lt;View style={focused ?  styles.topTabLine : null}&gt;
&lt;Text style={ [styles.textStyle, {color: focused ? focusedLabelColor : unfocusedLableColor , fontWeight : focused ? &#39;bold&#39; : &#39;normal&#39; }] }&gt;Contacts &lt;/Text&gt;
//&lt;/View&gt;
),
}}
/&gt;
&lt;/Tab.Navigator&gt;

答案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 = () =&gt; {
  if (Platform.OS === &#39;android&#39;) {
    Linking.openURL(&#39;content://com.android.contacts/contacts&#39;);
  }
};

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 &#39;react&#39;;
import { Linking, Platform } from &#39;react-native&#39;;
import { useFocusEffect } from &#39;@react-navigation/native&#39;;

.
.
.

useFocusEffect(
  useCallback(() =&gt; {
    if (Platform.OS === &#39;android&#39;) {
      Linking.openURL(&#39;content://com.android.contacts/contacts&#39;);
    }
  }, [])
);

Here's a Snack for the implementation

huangapple
  • 本文由 发表于 2023年4月10日 18:39:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75976359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定