人生苦短,kotlin未尝不可~

实现目标

点击FirstActivity(主Activity)的一个Button跳转到SecondActivity

实现效果

方法1:使用显式Intent

首先,我们要为FirstActivity创建一个按钮。
编辑其布局文件activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstActivity">

<!--在中间添加一个按钮(ID:myButton)-->
<Button
android:id="@+id/myButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="341dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

然后为刚创建的按钮添加监听。
编辑FirstActivity.kt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class FirstActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

//为myButton按钮创建监听
myButton.setOnClickListener{
//从当前Activity跳转到SecondActivity
val intent = Intent(this,SecondActivity::class.java)
//startActivity()方法专门用于启动Activity
//他接受一个Intent参数
startActivity(intent)
}

}
}

上述SecondActivity与FirstActivity处于同一个包下

代码说明

  • Intent
    • 是Android程序中各组件之间进行交互的一种重要方式。
      可用于启动Activity、启动Service以及发送广播等场景
    • 上述所用的一个构造函数:Intent(Context packageContext, Class<?> cls)
      • 第一个参数Context要求提供一个Activity的上下文
      • 第二个参数Class用于指定想要启动的目标Activity
  • startActivity()方法 : 接收一个Intent参数,从而启动目标Activity

方法2:使用隐式Intent

  • 使用Intent的另一个构造函数:直接将Activityaction传入

AndroidMainifest.xml中修改SecondActivity的标签属性

1
2
3
4
5
6
7
8
<activity android:name=".SecondActivity" android:label="Second_Activity">
<intent-filter>
<!--指定当前Activity可以响应的action和category-->
<action android:name="com.example.myapplication.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.MYCAT"/>
</intent-filter>
</activity>

只有actioncategory标签中的内容同时匹配Intent中的指定actioncategory时,这个Activity才能响应该Intent

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class FirstActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//添加点击监听
myButton.setOnClickListener{
//将action的内容传入Intent
val intent = Intent("com.example.myapplication.ACTION_START")
//指定category(也可指定多个)
intent.addCategory("android.intent.category.MYCAT")
//启动能同时匹配上述action和category的Activity
startActivity(intent)
}
}
}

每个Intent中只能有一个action,但能指定多个category
如果不指定category,则startActivity()方法会自动将android.intent.category.DEFAULT添加到Intent中

跳转到其他程序的Activity

以跳转到浏览器为例

只需要修改FirstActivity中按钮点击事件的代码:

1
2
3
4
5
6
7
button.setOnClickListener{
//首先指定intent的action是Intent.ACTION_VIEW
val intent = Intent(Intent.ACTION_VIEW)
//然后通过Uri.parse方法将字符串解析为Uri对象,并传递到Intent的data中
intent.data = Uri.parse("https://www.baidu.com")//实际上调用了Intent的setData()方法
startActivity(intent)
}

这样的话就可以实现点击按钮后自动跳转到浏览器并显示百度官网了。

Intent.ACTION_VIEW的常量值为android.intent.action.VIEW

同时,我们还可以在<intent-filter>标签中再配置一个<data>标签,用于更精确的指定当前Activity能够响应的数据。<data>标签中主要可以配置以下内容:

内容 作用
android:scheme 用于指定数据的协议部分,如上述https
android:host 指定数据的主机名部分,如www.baidu.com
android:port 用于指定端口部分
android:path 用于指定端口后面的部分
android:mimeType 指定可以处理的数据类型

比如让SecondActivity能够精准响应https的数据
只需要修改其布局文件second_layout.xml

1
2
3
4
5
6
7
8
9
10
11
<activity
android:name=".SecondActivity"
android:label="Second_Activity">
<intent-filter tools:ignore="AppLinkUrlError">
<!-- 指定所要响应的action内容 -->
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<!-- 使Activity精准响应https的内容 -->
<data android:scheme="https"/>
</intent-filter>
</activity>

只有actiondata内容都匹配才能实现跳转。
这里如果将android:scheme="https"改为android:scheme="http",那么将无法响应。

除了https协议外,我们还可以指定其他的协议,比如geo表示显示地理位置、tel表示拨打电话。
下面的代码展示了如何在程序中调用系统拨号界面:

1
2
3
4
5
button.setOnClickListener{
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:10086")
startActivity(intent)
}

 评论

载入天数...载入时分秒...