【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent
1.0 在helloworld项目基础上创建活动SecondActivity:
2.0 其中main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/add_item"
- android:title="添加"
- />
- <item
- android:id="@+id/remove_item"
- android:title="移除"
- />
- </menu>
3.0 activity_second.xml不做修改。
4.0 SecondActivity.java:
- package com.example.helloworld;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Toast;
- public class SecondActivity extends AppCompatActivity {
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- //通过getMenuInflater()方法能够调用MenuInflate对象,
- // 在调用它的inflate()方法就可以给当前活动创建菜单。
- //inflate()方法第一个参数我们通过哪一个资源文件来创建菜单(R.menu.main)
- //第二个参数指定我们的菜单项将添加到哪一个Menu对象中
- //使用onCreateOptionsMenu()方法传入menu参数,再用这个方法返回true,表示允许创建的菜单显示出来
- //如果返回false,创建的菜单将无法显示。
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- // return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.add_item:
- Toast.makeText(this, "你点击了添加", Toast.LENGTH_SHORT).show();
- break;
- case R.id.remove_item:
- Toast.makeText(this, "你点击了删除", Toast.LENGTH_SHORT).show();
- break;
- default:
- }
- return true;
- // return super.onOptionsItemSelected(item);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- }
- }
其中的onCreateOptionsMenu()方法和onOptionsItemSelected()方法可以通过Ctrl+O键(Mac系统是control+O)调用。
5.0 AndroidMainifest.xml:
- <activity android:name=".SecondActivity"
- android:label="第二个活动">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
运行:
6.0 Intent一般用于启动活动、启动服务以及发送广播等场景,其中显式Intent,Intent传入两个参数:第一个是上下文,第二个是目标活动:
新建活动ThirdActivity, 勾选“Generate Layout File”和“Launcher Activity”。
ThirdActivity.java:
- package com.example.helloworld;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class ThirdActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_third);
- Button button3 = (Button) findViewById(R.id.button_3);
- button3.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Toast.makeText(FirstActivity.this,"你点击了按钮1",Toast.LENGTH_SHORT).show();
- //Intent一般用于启动活动、启动服务以及发送广播等场景
- //显式Intent,Intent传入两个参数:第一个是上下文,第二个是目标活动
- Intent intent = new Intent(ThirdActivity.this,SecondActivity.class);
- startActivity(intent);
- }
- });
- }
- }
activity_third.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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=".ThirdActivity">
- <Button
- android:id="@+id/button_3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按钮 3"
- />
- </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”的内容:
- <activity android:name=".FourthActivity"
- android:label="第四个活动">
- <intent-filter>
- <action android:name="com.example.helloworld.ACTION_START" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="com.example.helloworld.MY_CATEGORY" />
- </intent-filter>
- </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”的内容:
- <activity
- android:name=".FourthActivity"
- android:label="第四个活动">
- <intent-filter>
- <action android:name="com.example.helloworld.ACTION_START" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </activity>
新建活动FifthActivity,勾选“Generate Layout File”。
修改AndroidMainifest.xml关于活动“FifthActivity”的内容:
- <activity android:name=".FifthActivity"
- android:label="第五个活动">
- <intent-filter>
- <action android:name="com.example.helloworld.ACTION_START" />
- <category android:name="com.example.helloworld.MY_CATEGORY" />
- </intent-filter>
- </activity>
这里需要修改下活动SecondActivity.java的代码:
- package com.example.helloworld;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class SecondActivity extends AppCompatActivity {
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- //通过getMenuInflater()方法能够调用MenuInflate对象,
- // 在调用它的inflate()方法就可以给当前活动创建菜单。
- //inflate()方法第一个参数我们通过哪一个资源文件来创建菜单(R.menu.main)
- //第二个参数指定我们的菜单项将添加到哪一个Menu对象中
- //使用onCreateOptionsMenu()方法传入menu参数,再用这个方法返回true,表示允许创建的菜单显示出来
- //如果返回false,创建的菜单将无法显示。
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- // return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.add_item:
- Toast.makeText(this, "你点击了添加", Toast.LENGTH_SHORT).show();
- break;
- case R.id.remove_item:
- Toast.makeText(this, "你点击了删除", Toast.LENGTH_SHORT).show();
- break;
- default:
- }
- return true;
- // return super.onOptionsItemSelected(item);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- Button button2 = (Button) findViewById(R.id.button_2);
- button2.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //Intent一般用于启动活动、启动服务以及发送广播等场景
- //隐式Intent,<action>和<category>需要同时匹配上才能响应,
- //一个是"com.example.helloworld.ACTION_START",
- //一个是一个默认的category("android.intent.category.DEFAULT"),
- //调用startActivity时会自动将其添加到Intent。
- Intent intent = new Intent("com.example.helloworld.ACTION_START");
- startActivity(intent);
- }
- });
- }
- }
修改活动FourthActivity.java的代码:
- package com.example.helloworld;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class FourthActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fourth);
- Button button4 = (Button) findViewById(R.id.button_4);
- button4.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //Intent一般用于启动活动、启动服务以及发送广播等场景
- //隐式Intent,<action>和<category>需要同时匹配上才能响应,
- //一个是"com.example.helloworld.ACTION_START",
- //一个是一个默认的category("android.intent.category.DEFAULT"),
- //调用startActivity时会自动将其添加到Intent。
- Intent intent = new Intent("com.example.helloworld.ACTION_AA");
- intent.addCategory("com.example.helloworld.MY_CATEGORY");
- startActivity(intent);
- }
- });
- }
- }
activity_second.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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=".SecondActivity">
- <Button
- android:id="@+id/button_2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按钮 2 指向4"
- />
- </android.support.constraint.ConstraintLayout>
运行测试:
点击“按钮4 指向5”,程序奔溃,目前找不到原因……
8.0 更多Intent运用,可以实现多个应用程序之间实现功能共享。
新建活动SixthActivity.java,勾选“Generate Layout File”和“Launcher Activity”。
修改AndroidMainifest.xml关于活动“SixthActivity”的内容:
- <activity android:name=".SixthActivity"
- android:label="第六个活动">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
activity_sixth.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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=".SixthActivity">
- <Button
- android:id="@+id/button_6"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按钮 6 指向浏览器支撑网站"
- />
- </android.support.constraint.ConstraintLayout>
- 活动SixthActivity.java:
- package com.example.helloworld;
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class SixthActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_sixth);
- Button button6 = (Button) findViewById(R.id.button_6);
- button6.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 指定Intent的action是Intent.ACTION_VIEW
- // 这是一个Android系统内置的动作,常量值为android.intent.action.ACTION_VIEW
- // 通过Uri.parse()方法,将一个网址字符串解析成uri对象,
- // 再调用Intent的setData()方法将URI对象传递进去。
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("https://www.cnblogs.com/xiaofu007/"));
- startActivity(intent);
- }
- });
- }
- }
运行:
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”的内容:
- <activity
- android:name=".SeventhActivity"
- android:label="第七个活动">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- <data android:scheme="http"/>
- </intent-filter>
- </activity>
- SeventhActivity.java:
- package com.example.helloworld;
- import android.content.Intent;
- import android.net.Uri;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class SeventhActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_seventh);
- Button button7 = (Button) findViewById(R.id.button_7);
- button7.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("https://www.baidu.com"));
- startActivity(intent);
- }
- });
- }
- }
activity_seventh.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.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=".SeventhActivity">
- <Button
- android:id="@+id/button_7"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="按钮 7 指向浏览器支撑网站"
- />
- </android.support.constraint.ConstraintLayout>
运行:
10.0 除了http之外,还可以指定geo,表示显示地理位置,tel表示拨打电话号码:
- button7.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // Intent intent = new Intent(Intent.ACTION_VIEW);
- // intent.setData(Uri.parse("https://www.baidu.com"));
- // startActivity(intent);
- Intent intent = new Intent(Intent.ACTION_DIAL);
- intent.setData(Uri.parse("tel:10010"));
- startActivity(intent);
- }
- });
相应的修改AndroidMainifest.xml关于活动“SeventhActivity”<data>标签内容为tel
运行:
【Android】6.0 添加Menu菜单组件、Intent启动活动、显式Intent、隐式Intent的更多相关文章
- Android添加Menu菜单
在安卓中添加Menu菜单十分简单. 步骤: 1.在menu文件夹中的main.xml文件中添加要添加的项目. <menu xmlns:android="http://schemas.a ...
- 第二百零六节,jQuery EasyUI,Menu(菜单)组件
jQuery EasyUI,Menu(菜单)组件 学习要点: 1.加载方式 2.菜单项属性 3.菜单属性 4.菜单事件 5.菜单方法 本节课重点了解 EasyUI 中 Menu(菜单)组件的使用方法, ...
- Android笔记(五)利用Intent启动活动
Intent是意图的意思,分为显式 Intent 和隐式 Intent. 以下我们试图在FirstActivity中通过点击button来启动SecondActivity 1.显式Intent 在应用 ...
- 在活动之间切换(隐式Intent)
实验名称:在活动之间切换 实验现象:在主活动中点击button1可以进入下一个活动 使用技术:隐式Intent 步骤: 1.创建一个项目,加载布局并在布局中添加一个button 部分截图未截,直接Ne ...
- 从零開始学android<Menu菜单组件.三十.>
在Android系统之中.菜单一共同拥有三类:选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 今天我们就用几个样例来分别介绍下菜单的使用 acti ...
- 安卓开发笔记——Menu菜单组件(选项菜单,上下文菜单,子菜单)
菜单是用户界面中最常见的元素之一,使用非常频繁,在Android中,菜单被分为如下三种,选项菜单(OptionsMenu).上下文菜单(ContextMenu)和子菜单(SubMenu). 菜单的实现 ...
- MTK Android 设置下添加一级菜单[ZedielPcbTest]
功能描述:Android7.1.2 设置下添加一级菜单[ZedielPcbTest],点击ZedielPcbTest,启动ZedielPcbTest.apk应用. 编译:需要在out目录删除Settt ...
- Android开发中的menu菜单
复写onCreateOptionsMenu方法,当点击menu菜单时,调用该方法. @Override public boolean onCreateOptionsMenu(Menu menu) { ...
- Android 控件 之 Menu 菜单
http://www.cnblogs.com/Mrs-cc/archive/2012/07/21/2603042.html 1.OptionsMenu (选项菜单)用法总结 使用方法: 方法一:添 ...
随机推荐
- Python实现——决策树(部分函数/连续数据)
由于上一例的实现中只针对了离散数据,为了扩充处理范围,我实现了一下对线性数据的简单处理,在其中我选择用中位数作为指标,平均数.众数等等其他数据在我看来异曲同工,最终也都会有较相似的结构. 求连续数据的 ...
- js 三大事件(鼠标.键盘.浏览器)
鼠标事件: click:单击 dblclick:双击 mousedown:鼠标按下 mouseup:鼠标抬起 mouseover:鼠标悬浮(进入) mouseout:鼠标离开(离开) mousemov ...
- python期中总结
1.tuple 元组 与列表类似 tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # 以下修改元组元素操作是非法的. # tup1[0] = 100 # 创建一个新的 ...
- 报表中经常遇到的一个头疼的问题是需要自动选择过去一个月的数据作为当前报表输出。网上查询了一些.NET 的C#例子,发现都实现的比较复杂
报表中经常遇到的一个头疼的问题是需要自动选择过去一个月的数据作为当前报表输出.网上查询了一些.NET 的C#例子,发现都实现的比较复杂,其实这个问题可以很简单的通过.NET的DateTime函数来实现 ...
- 多租户概念以及FreeLink多租户设计思想
多租户实现思想 多租户技术的实现重点,在于不同租户间应用程序环境的隔离(application context isolation)以及数据的隔离(data isolation),以维持不同租户间应用 ...
- 使用 .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& ...
- JS中String()与.toString()的区别
1..toString()可以将所有的的数据都转换为字符串,但是要排除null 和 undefined 例如将false转为字符串类型 <script> var str = false.t ...
- SpringMVC HandlerMethodArgumentResolver自定义参数转换器
来源: https://www.cnblogs.com/daxin/p/3296493.html 自定义Spring MVC3的参数映射和返回值映射 + fastjson首先说一下场景:在一些富客户端 ...
- 网站ico那点事儿
一. 如何获取某个网站的favicon.ico http://moco.imooc.com/player/report.html 今天看到这个网站上,左侧的小图片挺好看的,想弄下来,检查源码,也没有看 ...
- 设计模式学习总结(十)责任链模式(Chain Of Responsibility)
责任链主要指通过一连串的操作来实现某项功能或者在处理相关的业务时,对于自己的业务则进行处理,反之,对于不属于自己的业务,则进行下发! 一.示例展示: 以下例子主要通过对煤矿对井下警告信息的处理来进 ...