1、Activity之间的切换通过Intent来完成。

  1)清单文件,配置好Activity,所有Activity都需要在该文件中配置。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.luxh.intent"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 第一个Activity -->
<activity
android:name="cn.luxh.intent.FirstActivity"
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 -->
<activity
android:name="cn.luxh.intent.SecondActivity"
android:label="@string/second_activity">
<intent-filter>
<action android:name="cn.luxh.intent.SecondActivity"/> <!--意图筛选器名称 -->
<category android:name="android.intent.category.DEFAULT"/><!--意图筛选器类别 -->
</intent-filter>
</activity>
</application> </manifest>

  2)Activity代码

package cn.luxh.intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent; public class FirstActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
handleBtnJumpClick();
} /**
* 处理按钮点击事件
*/
private void handleBtnJumpClick() {
Button btn_jump = (Button) findViewById(R.id.btn_jump);
btn_jump.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建一个Intent /*Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);*/ //通过意图筛选器名称创建Intent
Intent intent = new Intent("cn.luxh.intent.SecondActivity"); //启动新的Activity
startActivity(intent);
}
});
} }
package cn.luxh.intent;

import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}

  3)运行效果

2、从Intent中返回数据

  在第二个Activity中输入用户名,然后在第一个Activity中显示。

  1)布局文件activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/second_activity_textview"
android:layout_gravity="center_horizontal"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/second_activity_textview_username"/> <EditText
android:id="@+id/edit_text_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"/> <Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/second_activity_btn_submit"
android:layout_gravity="right"/>
</LinearLayout>

  2)Activity代码

package cn.luxh.intent;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent; public class FirstActivity extends Activity { public static final int REQUEST_CODE = 1;//标识代码 private static final String TAG = "FirstActivity"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
handleBtnJumpClick();
} /**
* 处理按钮点击事件
*/
private void handleBtnJumpClick() {
Button btn_jump = (Button) findViewById(R.id.btn_jump);
btn_jump.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建一个Intent /*Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);*/ //通过意图筛选器名称创建Intent
Intent intent = new Intent("cn.luxh.intent.SecondActivity"); //启动新的Activity,新的Activity结束后有结果返回
startActivityForResult(intent, REQUEST_CODE);
}
});
} /**
* startActivityForResult(intent, REQUEST_CODE)启动的新Activity结束调用该方法获取返回值
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case REQUEST_CODE:
if(resultCode == RESULT_OK) {
String username = data.getData().toString();
Log.d(TAG, "返回的数据是:"+username);
Toast.makeText(this,username, Toast.LENGTH_LONG).show();
}
}
} }
package cn.luxh.intent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
handleBtnSubmitClick();
} /**
* 处理提交按钮点击
*/
private void handleBtnSubmitClick(){
Button btn_submit = (Button) findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获取输入的用户名
EditText et_username = (EditText) findViewById(R.id.edit_text_username);
String username = et_username.getText().toString();
Intent data = new Intent();
data.setData(Uri.parse(username));
setResult(RESULT_OK, data);//RESULT_OK = -1, operation succeeded
finish();//关闭当前Activity
}
});
}
}

  3)运行效果

Android--Intent的使用的更多相关文章

  1. android Intent介绍

    Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 ...

  2. android:Intent匹配action,category和data原则

    1.当你在androidmanifest里面定义了一个或多个action时 你使用隐式意图其他activity或者service时,规定你隐式里面的action必须匹配XML中定义的action,可以 ...

  3. Android Intent

    Intent在Android中的重要性不言而喻.本文主要总结下Intent使用过程中需要注意的一些问题. 1.隐式Intent AndroidManifest.xml声明时<intent-fil ...

  4. android intent和intent action大全

    1.Intent的用法:(1)用Action跳转1,使用Action跳转,如果有一个程序的AndroidManifest.xml中的某一个 Activity的IntentFilter段中 定义了包含了 ...

  5. Android总结篇系列:Android Intent

    Intent在Android中的重要性不言而喻.本文主要总结下Intent使用过程中需要注意的一些问题. 1.隐式Intent AndroidManifest.xml声明时<intent-fil ...

  6. 什么时候加上android.intent.category.DEFAULT

    什么时候加上android.intent.category.DEFAULT 1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent ...

  7. (转)android.intent.action.MAIN与android.intent.category.LAUNCHER

    android.intent.action.MAIN决定应用程序最先启动的Activity android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里 在网上看到 ...

  8. android之android.intent.category.DEFAULT的用途和使用

    1.要弄清楚这个问题,首先需要弄明白什么是implicit(隐藏) intent什么是explicit(明确) intent. Explicit Intent明确的指定了要启动的Acitivity , ...

  9. 理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER

    刚才看了一下sundy的视频<LLY110426_Android应用程序启动>,里面讲到luncher这个activity通过获取应用程序信息来加载应用程序,显示给用户,其中就是通过一个应 ...

  10. Android Intent的几种用法全面总结

    Android Intent的几种用法全面总结 Intent, 用法 Intent应该算是Android中特有的东西.你可以在Intent中指定程序要执行的动作(比如:view,edit,dial), ...

随机推荐

  1. CentOS6.5系统挂载NTFS分区的移动硬盘 centos安装repoforge源(yum)

    CentOS6.5系统挂载NTFS分区的移动硬盘 作为IT的工作者,避免不了使用Linux系统,我现在使用的系统是CentOS6.5 X86_64位版本,但是插入NTFS移动硬盘没有办法识别.通过下面 ...

  2. sqlite 下载的 ZIP 包的区别

    https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx20-binary-bundle- ...

  3. zend studio 13 curl 请求本机地址 无法跟踪调试的问题解决方案。。。(chrome等浏览器调试原理相同)

    方案如下: <?php $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, 'http://YOUR-SITE.com/your-script. ...

  4. 剑指offer系列44---只出现一次 的数字

    [题目]一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. * [思路]异或性质:数异或自己即为0: * 一个数组中,从头到尾异或的结果为不重复数字异或结果. ...

  5. 已跳过 'cache' -- 节点处于冲突状态

    svn resolved ./cache ./cache 为冲突文件路径“cache”的冲突状态已解决

  6. 导航栏4种效果---原生js

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 黄聪:TinyMCE 4 增强 添加样式、按钮、字体、下拉菜单和弹出式窗口

    我最喜欢 WordPress 3.9 的更新是使用了 TinyMCE 4.0 编辑器.新的 TinyMCE 看起来看起来更整洁(真正匹配WP仪表板),它有一些非常不错的附加功能.我的很多老主题和插件必 ...

  8. iphone dev 入门实例7:How to Add Splash Screen in Your iOS App

    http://www.appcoda.com/how-to-add-splash-screen-in-your-ios-app/ What’s Splash Screen? For those who ...

  9. C产品狗

    作者:郭琦链接:https://www.zhihu.com/question/29342383/answer/110823046来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. Camel、Pastal、匈牙利标记法

    原来我一直用的是Camel标记法……收藏学习了. */       Camel标记法采用首字母小写,接下来的单词都以大写字母开头的方法,如myName.       Pastal标记法采用首字母大写, ...