1.0 在helloworld项目基础上创建活动SecondActivity:

2.0 其中main.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item
  4. android:id="@+id/add_item"
  5. android:title="添加"
  6. />
  7. <item
  8. android:id="@+id/remove_item"
  9. android:title="移除"
  10. />
  11. </menu>

3.0 activity_second.xml不做修改。

4.0 SecondActivity.java:

  1. package com.example.helloworld;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.widget.Toast;
  8.  
  9. public class SecondActivity extends AppCompatActivity {
  10.  
  11. @Override
  12. public boolean onCreateOptionsMenu(Menu menu) {
  13. //通过getMenuInflater()方法能够调用MenuInflate对象,
  14. // 在调用它的inflate()方法就可以给当前活动创建菜单。
  15. //inflate()方法第一个参数我们通过哪一个资源文件来创建菜单(R.menu.main)
  16. //第二个参数指定我们的菜单项将添加到哪一个Menu对象中
  17. //使用onCreateOptionsMenu()方法传入menu参数,再用这个方法返回true,表示允许创建的菜单显示出来
  18. //如果返回false,创建的菜单将无法显示。
  19. getMenuInflater().inflate(R.menu.main, menu);
  20. return true;
  21. // return super.onCreateOptionsMenu(menu);
  22. }
  23.  
  24. @Override
  25. public boolean onOptionsItemSelected(MenuItem item) {
  26. switch (item.getItemId()) {
  27. case R.id.add_item:
  28. Toast.makeText(this, "你点击了添加", Toast.LENGTH_SHORT).show();
  29. break;
  30. case R.id.remove_item:
  31. Toast.makeText(this, "你点击了删除", Toast.LENGTH_SHORT).show();
  32. break;
  33. default:
  34.  
  35. }
  36. return true;
  37. // return super.onOptionsItemSelected(item);
  38. }
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_second);
  44.  
  45. }
  46. }

其中的onCreateOptionsMenu()方法和onOptionsItemSelected()方法可以通过Ctrl+O键(Mac系统是control+O)调用。

5.0  AndroidMainifest.xml:

  1. <activity android:name=".SecondActivity"
  2. android:label="第二个活动">
  3. <intent-filter>
  4. <action android:name="android.intent.action.MAIN" />
  5.  
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>

运行:

6.0 Intent一般用于启动活动、启动服务以及发送广播等场景,其中显式Intent,Intent传入两个参数:第一个是上下文,第二个是目标活动:

新建活动ThirdActivity, 勾选“Generate Layout File”和“Launcher Activity”。

ThirdActivity.java:

  1. package com.example.helloworld;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class ThirdActivity extends AppCompatActivity {
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_third);
  15. Button button3 = (Button) findViewById(R.id.button_3);
  16. button3.setOnClickListener(new View.OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. // Toast.makeText(FirstActivity.this,"你点击了按钮1",Toast.LENGTH_SHORT).show();
  20. //Intent一般用于启动活动、启动服务以及发送广播等场景
  21. //显式Intent,Intent传入两个参数:第一个是上下文,第二个是目标活动
  22. Intent intent = new Intent(ThirdActivity.this,SecondActivity.class);
  23. startActivity(intent);
  24. }
  25. });
  26. }
  27. }

activity_third.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".ThirdActivity">
  8.  
  9. <Button
  10. android:id="@+id/button_3"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="按钮 3"
  14. />
  15. </android.support.constraint.ConstraintLayout>

运行(点击“按钮3”,进入第二个活动):

7.0 而隐式Intent,<action>和<category>需要同时匹配上才能响应,一个是"com.example.helloworld.ACTION_START",一个是一个默认的category("android.intent.category.DEFAULT"),调用startActivity时会自动将其添加到Intent。

同样,新建活动FourthActivity,勾选“Generate Layout File”。

修改AndroidMainifest.xml关于活动“FourthActivity”的内容:

  1. <activity android:name=".FourthActivity"
  2. android:label="第四个活动">
  3. <intent-filter>
  4. <action android:name="com.example.helloworld.ACTION_START" />
  5. <category android:name="android.intent.category.DEFAULT" />
  6. <category android:name="com.example.helloworld.MY_CATEGORY" />
  7. </intent-filter>
  8. </activity>

第一个action,当前活动可以响应"com.example.helloworld.ACTION_START"这个action,只有当action和category都匹配上才能激活该活动,第一个category是系统默认的配置,在Intent匹配的时候可以不提供category。

举例,我在SecondActivity活动中,设置了Intent,提供了action和category匹配,如果不提供category,发现就会像6.0里面一样,活动FourthActivity可以匹配得到,就会激活(打开)活动FourthActivity,用了第二个category 就会匹配到活动FifthActivity。

进一步修改AndroidMainifest.xml关于活动“FourthActivity”的内容:

  1. <activity
  2. android:name=".FourthActivity"
  3. android:label="第四个活动">
  4. <intent-filter>
  5. <action android:name="com.example.helloworld.ACTION_START" />
  6. <category android:name="android.intent.category.DEFAULT" />
  7. </intent-filter>
  8. </activity>

新建活动FifthActivity,勾选“Generate Layout File”。

修改AndroidMainifest.xml关于活动“FifthActivity”的内容:

  1. <activity android:name=".FifthActivity"
  2. android:label="第五个活动">
  3. <intent-filter>
  4. <action android:name="com.example.helloworld.ACTION_START" />
  5. <category android:name="com.example.helloworld.MY_CATEGORY" />
  6. </intent-filter>
  7. </activity>

这里需要修改下活动SecondActivity.java的代码:

  1. package com.example.helloworld;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11.  
  12. public class SecondActivity extends AppCompatActivity {
  13.  
  14. @Override
  15. public boolean onCreateOptionsMenu(Menu menu) {
  16. //通过getMenuInflater()方法能够调用MenuInflate对象,
  17. // 在调用它的inflate()方法就可以给当前活动创建菜单。
  18. //inflate()方法第一个参数我们通过哪一个资源文件来创建菜单(R.menu.main)
  19. //第二个参数指定我们的菜单项将添加到哪一个Menu对象中
  20. //使用onCreateOptionsMenu()方法传入menu参数,再用这个方法返回true,表示允许创建的菜单显示出来
  21. //如果返回false,创建的菜单将无法显示。
  22. getMenuInflater().inflate(R.menu.main, menu);
  23. return true;
  24. // return super.onCreateOptionsMenu(menu);
  25. }
  26.  
  27. @Override
  28. public boolean onOptionsItemSelected(MenuItem item) {
  29. switch (item.getItemId()) {
  30. case R.id.add_item:
  31. Toast.makeText(this, "你点击了添加", Toast.LENGTH_SHORT).show();
  32. break;
  33. case R.id.remove_item:
  34. Toast.makeText(this, "你点击了删除", Toast.LENGTH_SHORT).show();
  35. break;
  36. default:
  37.  
  38. }
  39. return true;
  40. // return super.onOptionsItemSelected(item);
  41. }
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46. setContentView(R.layout.activity_second);
  47. Button button2 = (Button) findViewById(R.id.button_2);
  48. button2.setOnClickListener(new View.OnClickListener() {
  49. @Override
  50. public void onClick(View v) {
  51. //Intent一般用于启动活动、启动服务以及发送广播等场景
  52. //隐式Intent,<action>和<category>需要同时匹配上才能响应,
  53. //一个是"com.example.helloworld.ACTION_START",
  54. //一个是一个默认的category("android.intent.category.DEFAULT"),
  55. //调用startActivity时会自动将其添加到Intent。
  56. Intent intent = new Intent("com.example.helloworld.ACTION_START");
  57. startActivity(intent);
  58. }
  59. });
  60. }
  61. }

修改活动FourthActivity.java的代码:

  1. package com.example.helloworld;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class FourthActivity extends AppCompatActivity {
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_fourth);
  15. Button button4 = (Button) findViewById(R.id.button_4);
  16. button4.setOnClickListener(new View.OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. //Intent一般用于启动活动、启动服务以及发送广播等场景
  20. //隐式Intent,<action>和<category>需要同时匹配上才能响应,
  21. //一个是"com.example.helloworld.ACTION_START",
  22. //一个是一个默认的category("android.intent.category.DEFAULT"),
  23. //调用startActivity时会自动将其添加到Intent。
  24.  
  25. Intent intent = new Intent("com.example.helloworld.ACTION_AA");
  26. intent.addCategory("com.example.helloworld.MY_CATEGORY");
  27. startActivity(intent);
  28. }
  29. });
  30. }
  31. }

activity_second.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".SecondActivity">
  8.  
  9. <Button
  10. android:id="@+id/button_2"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="按钮 2 指向4"
  14. />
  15. </android.support.constraint.ConstraintLayout>

运行测试:

点击“按钮4 指向5”,程序奔溃,目前找不到原因……

8.0 更多Intent运用,可以实现多个应用程序之间实现功能共享。

新建活动SixthActivity.java,勾选“Generate Layout File”和“Launcher Activity”。

修改AndroidMainifest.xml关于活动“SixthActivity”的内容:

  1. <activity android:name=".SixthActivity"
  2. android:label="第六个活动">
  3. <intent-filter>
  4. <action android:name="android.intent.action.MAIN" />
  5. <category android:name="android.intent.category.LAUNCHER" />
  6. </intent-filter>
  7. </activity>

activity_sixth.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".SixthActivity">
  8.  
  9. <Button
  10. android:id="@+id/button_6"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="按钮 6 指向浏览器支撑网站"
  14. />
  15. </android.support.constraint.ConstraintLayout>
  1. 活动SixthActivity.java
  1. package com.example.helloworld;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9.  
  10. public class SixthActivity extends AppCompatActivity {
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_sixth);
  16. Button button6 = (Button) findViewById(R.id.button_6);
  17. button6.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. // 指定Intent的action是Intent.ACTION_VIEW
  21. // 这是一个Android系统内置的动作,常量值为android.intent.action.ACTION_VIEW
  22. // 通过Uri.parse()方法,将一个网址字符串解析成uri对象,
  23. // 再调用Intent的setData()方法将URI对象传递进去。
  24. Intent intent = new Intent(Intent.ACTION_VIEW);
  25. intent.setData(Uri.parse("https://www.cnblogs.com/xiaofu007/"));
  26. startActivity(intent);
  27. }
  28. });
  29. }
  30. }

运行:

9.0 setData()方法,这个方法的使用可以结合在<intent-filter>标签中再配置一个<data>标签。

android:scheme:用于指定数据的协议部分,例如http部分

android:host:用于指定数据的主机名部分,例如www.baidu.com

android:port:用于指定数据的端口部分,一般紧随在主机名之后

android:path:用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容。

android:mimeType:用于指定可以处理的数据额、类型,允许使用通配符的方式进行指定。

只有<data>标签中指定内容和Intent中携带的Data完全一致时,当前活动才会完全响应Intent。

不过一般<data>标签不好记指定过多内容,比如指定android:scheme为http就可以响应所有Intent了。

新建活动SeventhActivity,勾选“Generate Layout File”和“Launcher Activity”。

修改AndroidMainifest.xml关于活动“SeventhActivity”的内容:

  1. <activity
  2. android:name=".SeventhActivity"
  3. android:label="第七个活动">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. <data android:scheme="http"/>
  8. </intent-filter>
  9. </activity>
  1. SeventhActivity.java
  1. package com.example.helloworld;
  2.  
  3. import android.content.Intent;
  4. import android.net.Uri;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9.  
  10. public class SeventhActivity extends AppCompatActivity {
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_seventh);
  16. Button button7 = (Button) findViewById(R.id.button_7);
  17. button7.setOnClickListener(new View.OnClickListener() {
  18. @Override
  19. public void onClick(View v) {
  20. Intent intent = new Intent(Intent.ACTION_VIEW);
  21. intent.setData(Uri.parse("https://www.baidu.com"));
  22. startActivity(intent);
  23. }
  24. });
  25. }
  26. }

activity_seventh.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".SeventhActivity">
  8.  
  9. <Button
  10. android:id="@+id/button_7"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="按钮 7 指向浏览器支撑网站"
  14. />
  15. </android.support.constraint.ConstraintLayout>

运行:

10.0  除了http之外,还可以指定geo,表示显示地理位置,tel表示拨打电话号码:

  1. button7.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. // Intent intent = new Intent(Intent.ACTION_VIEW);
  5. // intent.setData(Uri.parse("https://www.baidu.com"));
  6. // startActivity(intent);
  7. Intent intent = new Intent(Intent.ACTION_DIAL);
  8. intent.setData(Uri.parse("tel:10010"));
  9. startActivity(intent);
  10. }
  11. });

相应的修改AndroidMainifest.xml关于活动“SeventhActivity”<data>标签内容为tel

运行:

【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent的更多相关文章

  1. Android添加Menu菜单

    在安卓中添加Menu菜单十分简单. 步骤: 1.在menu文件夹中的main.xml文件中添加要添加的项目. <menu xmlns:android="http://schemas.a ...

  2. 第二百零六节,jQuery EasyUI,Menu(菜单)组件

    jQuery EasyUI,Menu(菜单)组件 学习要点: 1.加载方式 2.菜单项属性 3.菜单属性 4.菜单事件 5.菜单方法 本节课重点了解 EasyUI 中 Menu(菜单)组件的使用方法, ...

  3. Android笔记(五)利用Intent启动活动

    Intent是意图的意思,分为显式 Intent 和隐式 Intent. 以下我们试图在FirstActivity中通过点击button来启动SecondActivity 1.显式Intent 在应用 ...

  4. 在活动之间切换(隐式Intent)

    实验名称:在活动之间切换 实验现象:在主活动中点击button1可以进入下一个活动 使用技术:隐式Intent 步骤: 1.创建一个项目,加载布局并在布局中添加一个button 部分截图未截,直接Ne ...

  5. 从零開始学android&lt;Menu菜单组件.三十.&gt;

    在Android系统之中.菜单一共同拥有三类:选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 今天我们就用几个样例来分别介绍下菜单的使用 acti ...

  6. 安卓开发笔记——Menu菜单组件(选项菜单,上下文菜单,子菜单)

    菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 菜单的实现 ...

  7. MTK Android 设置下添加一级菜单[ZedielPcbTest]

    功能描述:Android7.1.2 设置下添加一级菜单[ZedielPcbTest],点击ZedielPcbTest,启动ZedielPcbTest.apk应用. 编译:需要在out目录删除Settt ...

  8. Android开发中的menu菜单

    复写onCreateOptionsMenu方法,当点击menu菜单时,调用该方法. @Override public boolean onCreateOptionsMenu(Menu menu) { ...

  9. Android 控件 之 Menu 菜单

    http://www.cnblogs.com/Mrs-cc/archive/2012/07/21/2603042.html 1.OptionsMenu (选项菜单)用法总结   使用方法: 方法一:添 ...

随机推荐

  1. Python实现——决策树(部分函数/连续数据)

    由于上一例的实现中只针对了离散数据,为了扩充处理范围,我实现了一下对线性数据的简单处理,在其中我选择用中位数作为指标,平均数.众数等等其他数据在我看来异曲同工,最终也都会有较相似的结构. 求连续数据的 ...

  2. js 三大事件(鼠标.键盘.浏览器)

    鼠标事件: click:单击 dblclick:双击 mousedown:鼠标按下 mouseup:鼠标抬起 mouseover:鼠标悬浮(进入) mouseout:鼠标离开(离开) mousemov ...

  3. python期中总结

    1.tuple 元组 与列表类似 tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的. # tup1[0] = 100 # 创建一个新的 ...

  4. 报表中经常遇到的一个头疼的问题是需要自动选择过去一个月的数据作为当前报表输出。网上查询了一些.NET 的C#例子,发现都实现的比较复杂

    报表中经常遇到的一个头疼的问题是需要自动选择过去一个月的数据作为当前报表输出.网上查询了一些.NET 的C#例子,发现都实现的比较复杂,其实这个问题可以很简单的通过.NET的DateTime函数来实现 ...

  5. 多租户概念以及FreeLink多租户设计思想

    多租户实现思想 多租户技术的实现重点,在于不同租户间应用程序环境的隔离(application context isolation)以及数据的隔离(data isolation),以维持不同租户间应用 ...

  6. 使用 .NET Core CLI 创建 .NET Core 全局工具

    https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=2&ch=&tn=baiduhome_pg& ...

  7. JS中String()与.toString()的区别

    1..toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined 例如将false转为字符串类型 <script> var str = false.t ...

  8. SpringMVC HandlerMethodArgumentResolver自定义参数转换器

    来源: https://www.cnblogs.com/daxin/p/3296493.html 自定义Spring MVC3的参数映射和返回值映射 + fastjson首先说一下场景:在一些富客户端 ...

  9. 网站ico那点事儿

    一. 如何获取某个网站的favicon.ico http://moco.imooc.com/player/report.html 今天看到这个网站上,左侧的小图片挺好看的,想弄下来,检查源码,也没有看 ...

  10. 设计模式学习总结(十)责任链模式(Chain Of Responsibility)

    责任链主要指通过一连串的操作来实现某项功能或者在处理相关的业务时,对于自己的业务则进行处理,反之,对于不属于自己的业务,则进行下发!   一.示例展示: 以下例子主要通过对煤矿对井下警告信息的处理来进 ...