Android项目学习—Intent的作用详解
2015-03-07 17:49
369 查看
1.什么是Intent
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。通过使用Intent,程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。
2.显式Intent
显式Intent指定了组件属性的Intent,通过指定具体的组件类。实例如下
(1)新建布局文件Xiaoxi.xml
<TabHost
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@android:id/tabhost"
android:layout_weight="1"
><!--引用android系统已有的id-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--定义第一个标签页的内容-->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="消息页面"
android:textSize="30sp"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
(2).创建活动类XiaoXi.java
package xiaocool.net.my;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
/**
* Created by MRYU on 2015/3/7.
*/
public class XiaoXi extends TabActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xiaoxi);
//获取tabhost组件
TabHost tabHost=getTabHost();
//创建第一个Tab页
TabHost.TabSpec tab1=tabHost.newTabSpec("tab1")
.setIndicator("消息")//设置标题
.setContent(R.id.tab01);
//添加第一个tab页
}
}
(3).注册活动XiaoXi
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xiaocool.net.my" >
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".XiaoXi"></activity>
</application>
</manifest>
XiaoXi不是主活动,所以不需要配置intend-filter。
(4).测试显示Intent
//监听点击事件
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//登录信息合法性验证
//判断是否为空
if(username.getText().toString().trim().length()==0||password.getText().toString().trim().length()==0){
Toast.makeText(MainActivity.this,"用户名密码不能为空!", 1) .show();
return;
}
if(username.getText().toString().trim().equals("18653503680")&&password.getText().toString().trim().equals("cool")){
//读取服务器端数据,与用户填写的信息比对,根据用户名密码返回信息
//登录成功后填到系统主页面
Intent intent=new Intent(MainActivity.this,XiaoXi.class);
startActivity(intent);
}
}
});
即可进入到消息界面。
3.Android内置的Action
Android内置了很多Action,比如ACTION_VIEW:数据显示,ACTION_DIAL:拨打电话等
(1)修改按钮点击事件
//监听点击事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//跳转到活动MyActivity2
//第一个参数:上下文,第二个参数:目标活动类
// Intent intent = new Intent(MyActivity.this,MyActivity2.class); //显式Intent
// Intent intent = new Intent("com.yy.testactivity.MyActivity2.ACTION_START");//隐式Intent
// intent.addCategory("com.yy.testactivity.MY_CATEGORY");
Intent intent = new Intent(Intent.ACTION_VIEW); //内置Action
intent.setData(Uri.parse("http://www.baidu.com"));//跳转到页面
//启动活动
startActivity(intent);
}
});
最终链接到百度。其他功能如下
3.1ACTION_MAIN
Android.intent.action.MAIN,在每个AndroidManifest.xml问洞中都能看到,标记当前的Activity作为一个程序的入口。
3.2ACTION_DIAL
用于描述给用户打电话的动作
3.3ACTION_PICK
从特定的一组数据中进行选择数据操作
3.4ACTION_DEIT
编辑特定的数据
3.5ACTION_DELETE
删除特定的数据
4.Intent的其他属性
类别category:它为执行动作的附加信息
数据类型type:显式指定Intent的目标组件的类名称。
组件component:指定Intent的目标组件的类名称
附加信息extras:是其他所有附加信息的集合
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件。通过使用Intent,程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来完成请求。
2.显式Intent
显式Intent指定了组件属性的Intent,通过指定具体的组件类。实例如下
(1)新建布局文件Xiaoxi.xml
<TabHost
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@android:id/tabhost"
android:layout_weight="1"
><!--引用android系统已有的id-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--定义第一个标签页的内容-->
<LinearLayout
android:id="@+id/tab01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="消息页面"
android:textSize="30sp"
/>
</LinearLayout>
</FrameLayout>
</TabHost>
(2).创建活动类XiaoXi.java
package xiaocool.net.my;
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
/**
* Created by MRYU on 2015/3/7.
*/
public class XiaoXi extends TabActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xiaoxi);
//获取tabhost组件
TabHost tabHost=getTabHost();
//创建第一个Tab页
TabHost.TabSpec tab1=tabHost.newTabSpec("tab1")
.setIndicator("消息")//设置标题
.setContent(R.id.tab01);
//添加第一个tab页
}
}
(3).注册活动XiaoXi
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xiaocool.net.my" >
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".XiaoXi"></activity>
</application>
</manifest>
XiaoXi不是主活动,所以不需要配置intend-filter。
(4).测试显示Intent
//监听点击事件
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//登录信息合法性验证
//判断是否为空
if(username.getText().toString().trim().length()==0||password.getText().toString().trim().length()==0){
Toast.makeText(MainActivity.this,"用户名密码不能为空!", 1) .show();
return;
}
if(username.getText().toString().trim().equals("18653503680")&&password.getText().toString().trim().equals("cool")){
//读取服务器端数据,与用户填写的信息比对,根据用户名密码返回信息
//登录成功后填到系统主页面
Intent intent=new Intent(MainActivity.this,XiaoXi.class);
startActivity(intent);
}
}
});
即可进入到消息界面。
3.Android内置的Action
Android内置了很多Action,比如ACTION_VIEW:数据显示,ACTION_DIAL:拨打电话等
(1)修改按钮点击事件
//监听点击事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//跳转到活动MyActivity2
//第一个参数:上下文,第二个参数:目标活动类
// Intent intent = new Intent(MyActivity.this,MyActivity2.class); //显式Intent
// Intent intent = new Intent("com.yy.testactivity.MyActivity2.ACTION_START");//隐式Intent
// intent.addCategory("com.yy.testactivity.MY_CATEGORY");
Intent intent = new Intent(Intent.ACTION_VIEW); //内置Action
intent.setData(Uri.parse("http://www.baidu.com"));//跳转到页面
//启动活动
startActivity(intent);
}
});
最终链接到百度。其他功能如下
3.1ACTION_MAIN
Android.intent.action.MAIN,在每个AndroidManifest.xml问洞中都能看到,标记当前的Activity作为一个程序的入口。
3.2ACTION_DIAL
用于描述给用户打电话的动作
3.3ACTION_PICK
从特定的一组数据中进行选择数据操作
3.4ACTION_DEIT
编辑特定的数据
3.5ACTION_DELETE
删除特定的数据
4.Intent的其他属性
类别category:它为执行动作的附加信息
数据类型type:显式指定Intent的目标组件的类名称。
组件component:指定Intent的目标组件的类名称
附加信息extras:是其他所有附加信息的集合
相关文章推荐
- Android开发学习笔记:Intent的简介以及属性的详解
- android学习----(三)android项目结构详解
- Android学习指南之八:详解Intent及其应用实例
- Android开发学习笔记:Intent的简介以及属性的详解
- Android开发学习之Intent详解
- Android开发学习之Intent详解
- Android的学习之路(三)项目的启动过程和安装过程详解
- Android开发学习笔记:Intent的简介以及属性的详解
- android开发学习之路(1)---- activity及intent基本详解(1)
- Android项目之HomeHealth基础学习3:Intent使用Component
- Intent的Component,Action和Category属性详解-android学习之旅(五十)
- Intent的Component,Action和Category属性详解-android学习之旅(五十)
- Intent的Component,Action和Category属性详解-android学习之旅(五十)
- Android开发学习笔记:Intent的简介以及属性的详解
- Android开发学习笔记:Intent的简介以及属性的详解
- Android开发学习笔记:Intent的简介以及属性的详解
- Android开发学习笔记:Intent的简介以及属性的详解
- android开发学习之路(1)---- activity及intent基本详解(2)
- Android开发学习笔记:Intent的简介以及属性的详解
- Android项目学习—Tabhost用法详解