Android studio 使用Intent 在Activity 之间穿梭

使用Intent 在Activity 之间穿梭

\small{1.创建新的Empty Activity}
\small{2.在主界面的FirstActivity中添加button事件跳转}

复制代码
        //找到视图中button的Id
        val button1: Button =findViewById(R.id.button1)
        //给button赋予点击事件
        button1.setOnClickListener{
      //第一个参数传入this也就是FirstActivity 作为上下文
      //第二个参数传入SecondActivity::class.java作为目标Activity
      //接下来再通过startActivity()方法执行这个Intent 就可以了
        val intent=Intent(this,SecondActivity::class.java)
            startActivity(intent)
        }
复制代码

 

使用隐式Intent

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

\small{button事件中执行}

button1.setOnClickListener {
val intent = Intent("com.example.activitytest.ACTION_START")
startActivity(intent)
}

更多隐式Intent 的用法

button1.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.baidu.com")
startActivity(intent)
}

\small{android:scheme。用于指定数据的协议部分,如上例中的https 部分。}
\small{android:host。用于指定数据的主机名部分,如上例中的www.baidu.com 部分。}
\small{android:port。用于指定数据的端口部分,一般紧随在主机名之后。}
\small{android:path。用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内 容}
\small{android:mimeType。用于指定可以处理的数据类型,允许使用通配符的方式进行指定。}

选择响应Intent 的程序

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

//首先指定了Intent 的action是Intent.ACTION_DIAL,这又是一个Android 系统的内置动作
     val intent =Intent(Intent.ACTION_DIAL)
//然后在data部分指定了协议是tel,号码是10086
            intent.data=Uri.parse("tel:10086")
            startActivity(intent)

 

posted @   一招致命九虎山  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示