【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 (选项菜单)用法总结 使用方法: 方法一:添 ...
随机推荐
- 存入azure table时忽略某个属性
public class CustomTableEntity : TableEntity { public override IDictionary<string, EntityProperty ...
- bit、Byte、bps、Bps、pps、Gbps的单位的说明及换算
一.bit与Byte区别 1. bit(比特) 是电脑记忆体的最小单元,在二进制计算机中,每一比特代表0或1的数位信号. 2. Byte(单位字节) 一般表示存储介质大小的单位,数字.字母和特殊符号占 ...
- ubuntu 上的python不能解析jpeg,png?
在安装pillow之前需要先安装以下支持包1.apt-get install libjpeg-dev libfreetype6-dev zlib1g-dev libpng12-dev2.安装pillo ...
- POJ3322 Bloxorz I 无脑广搜(我死了。。。)
多测不清空,爆零两行泪....我死了QWQ 每个节点3个状态:横坐标,纵坐标,和方向 说一下方向:0:立着,1:竖着躺着,上半部分在(x,y),2:横着躺着,左半部分在(x,y) 然后就有了常量数组: ...
- 119th LeetCode Weekly Contest K Closest Points to Origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the d ...
- C# Task超时规则
需要知道以下的知识 正规的骚操作:https://stackoverflow.com/questions/4238345/asynchronously-wait-for-taskt-to-comple ...
- [转] Emmet-前端开发神器
[From] https://segmentfault.com/a/1190000007812543 Emmet是一款编辑器插件,支持多种编辑器支持.在前端开发中,Emmet 使用缩写语法快速编写 H ...
- 【研究】缓慢的http拒绝服务攻击
1 详细描述: 缓慢的http拒绝服务攻击是一种专门针对于Web的应用层拒绝服务攻击,攻击者操纵网络上的肉鸡,对目标Web服务器进行海量http request攻击,直到服务器带宽被打满,造成了拒绝服 ...
- sqlmap命令小结
--technique 这个参数可以指定sqlmap使用的探测技术,默认情况下会测试所有的方式. 支持的探测方式如下: B: Boolean-based blind SQL injection(布尔型 ...
- 使用Junit进行自动单元测试
软件工程第二次作业 选择开发工具 使用Eclipse进行java程序编写:安装过程如图: 练习自动单元测试技术 参考资料:[Junit入门使用教程][https://www.cnblogs.com/y ...