关于Intent

今天仍在学习Intent
Intent其实就是两个Activity之间的纽带,它实现了从一个活动跳转到另一个活动,其中也有一些功能,比如说数据的传递。
Intent分为显式和隐式:

  • 显示Intent
    比如说点击一个按钮切换到另一个事件,可在button的OnClick()方法内写入如下代码:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent); 
  • 隐式Intent
    step1:在要跳转到的活动的标签中加入以下代码(相当于指明了当前的活动能够匹配的action和category):
 <action android:name="com.example.activitytest.ACTION_START" />         
 <category android:name="android.intent.category.DEFAULT" /> 

step2:在FirstActivity中的onClick()事件中添加如下代码:

Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent); 

另外还要注意一点,每个Intent中只能指定一个action,却能制定多个category。
代码如下(已经实例化了一个intent对象):

intent.addCategory("com.example,activitytest.MY_CATEGORY");

别忘了还要在SecondActivity的中添加:

<category android:name="com.example.activitytest.MY_CATEGORY"/>
  • 更多用法
    利用intent跳转至百度的网页
    onClick()中添加:
Intent intent = new Intent(Intent.ACTION_VIEW);   
intent.setData(Uri.parse("http://www.baidu.com"));   
startActivity(intent);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值