zoukankan      html  css  js  c++  java
  • 使用Intent在活动之间穿梭

    使用Intent在活动之间穿梭

    1.在com.example.activitytest中创建第二个活动SecondActivity:

    /**
     * 第二个活动
     */
    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_layout);
        }
    }
    

    创建完成后会自动生成second_layout.xml,这里我们进行修改如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/button_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button 2"
            />
    </LinearLayout>
    
    

    这时AndroidManifest.xml已经帮我们注册了活动:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.activitytest">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity
                android:name=".FirstActivity"
                android:label="this is first">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"></activity>
        </application>
    
    </manifest>
    

    2.使用Intent启动活动

    Intent是Android中各组件进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据.

    Intent大致可以分为两种:显示Intent和隐式Intent

    一.显式Intent

    Intent中有多个构造函数的重载,其中一个Intent(Context packageContext,Class<?> cls),这个构造函数第一个参数是启动活动的上下文,第二个启动活动的目标.

                //启动第二个活动
                public void onClick(View v) {
                //FirstActivity.this 指上下文 SecondActivity.class指活动目标
                    Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                    startActivity(intent);
                }
    

    二.隐式Intent

    通过<activity>标签下配置<intent-filter>的内容,可以指定当前活动能够响应的action和category,

    打开AndroidManifest.xml,添加如下代码:

     <activity android:name=".SecondActivity">
           <intent-filter>
               <action android:name="com.example.activitytest.ACTION_START" />
               <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
     </activity>
    

    修改FirstActivity中按钮的点击事件:

     //隐式使用Intent
                public void onClick(View v) {
                    Intent intent = new Intent("com.example.activitytest.ACTION_START");
                    startActivity(intent);
                }
    

    可以选择添加多个category:

    //隐式使用Intent
                public void onClick(View v) {
                    Intent intent = new Intent("com.example.activitytest.ACTION_START");
                    intent.addCategory("com.example.activitytest.MY_CATEGORY");
                    startActivity(intent);
                }
    

    打开AndroidManifest.xml,添加如下代码:

    <activity android:name=".SecondActivity">
                <intent-filter>
                    <action android:name="com.example.activitytest.ACTION_START" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="com.example.activitytest.MY_CATEGORY" />
                </intent-filter>
            </activity>
    

    3.Intent的其他使用方法

    跳转第三方链接

    //跳转第三方链接
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse("https://www.baidu.com"));
                    startActivity(intent);
                }
    

    调用拨号功能

    //调用拨号功能
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:10086"));
                    startActivity(intent);
                }
    
  • 相关阅读:
    分类算法 学习笔记
    机器学习概述 & 特征工程 学习笔记
    Java基础知识
    牛客中Java工程师模拟面试整理
    leetcode142. 环形链表 II
    面经中的题目整理
    面经总结
    软件设计师补题(2007下半年上午题)
    软件设计师补题(2005上半年上午题)
    软件设计师补题(2005下半年上午题)
  • 原文地址:https://www.cnblogs.com/charlypage/p/9961151.html
Copyright © 2011-2022 走看看