版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中利用Intent实现一个页面跳转功能
这篇文章主要介绍了怎么在Android中利用Intent实现一个页面跳转功能,此处给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:下图中两个不同的方法就是两种页面之间跳转的情况1).跳转不返回数据2).跳转返回数据实例:第一种启动方式(跳转不返回数据)第二种启动方式(跳转返回数据)先看第一种:点击第一种启动方式按钮会出现右边的图,然后再点击Button按钮返回左边的界面,TextView中的内容没变。再看第二种启动方式不同的是,点击Button按钮返回左边的界面,TextView中的内容变成了你好。下面是所有代码AndroidManifest.xml<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="/apk/res/android"
package="ent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity">
</activity>
<activity
android:name="ent.firstactivity">
<intent-filter>
<action
android:name="ent.action.MAIN"
/>
<category
android:name="ent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity
android:name="ent.Secondactivity">
</activity>
</application>
</manifest>factivity<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/bt1__first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种启动方式"
/>
<Button
android:id="@+id/bt2__second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种启动方式"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="吧第二个页面回传的数据显示出来"
/>
</LinearLayout>sactivity<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button"
/>
</LinearLayout>firstactivity.javapackage
ent;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
/**
*
Created
by
lenovo
on
2018/2/27.
*/
public
class
firstactivity
extends
Activity
{
private
Button
bt1;
private
Button
bt2;
private
TextView
tv;
@Override
protected
void
onCreate(Bundle
savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
/*
通过点击bt1实现界面之间的跳转
1.通过startActivity的方式来实现
1>初始Intent(意图)
*/
bt1=(Button)
findViewById(R.id.bt1__first);
bt2=(Button)findViewById(R.id.bt2__second);
tv=(TextView)
findViewById(R.id.textView1);
//给bt1添加点击事件
bt1.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
view)
{
/*
第一个参数:上下文对象this
第二个参数:目标文件
*/
Intent
intent
=
new
Intent(firstactivity.this,Secondactivity.class);
startActivity(intent);
}
});
/*
2.通过startActivityForResult的方式来实现
*/
//给bt2添加点击事件
bt2.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
view)
{
Intent
intent
=
new
Intent(firstactivity.this,Secondactivity.class);
/*
第一个参数:Intent对象
第二个参数:请求的一个标识
*/
startActivityForResult(intent,1);
}
});
}
/*
通过startActivityForResult的方式接受返回数据的方法
requestCode:请求的标志,给每个页面发出请求的标志不一样,这样以后通过这个标志接受不同的数据
resultCode:这个参数是setResult(int
resultCode,Intent
data)方法传来的,这个方法用在传来数据的那个页面
*/
@Override
protected
void
onActivityResult(int
requestCode,int
resultCode
,Intent
data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==1&&resultCode==2){//当请求码是1&&返回码是2进行下面操作
String
content=data.getStringExtra("data");
tv.setText(content);
}
}
}Secondactivity.javapackage
ent;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
/**
*
Created
by
lenovo
on
2018/2/27.
*/
public
class
Secondactivity
extends
Activity
{
private
Button
bt;
String
content="你好";//想返回的内容
@Override
protected
void
onCreate(
Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
/*
第二个页面什么时候给第一个页面回传数据
回传到第一个页面的实际上是一个Intent对象
*/
bt=(Button)
findViewById(R.id.button);
bt.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
view)
{
Intent
data
=
new
Intent(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国初级形状的聚亚甲基苯基异氰酸酯行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 中国CV驱动轴行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 讲话者传播信息的途径
- 上海市桐柏高级中学2022-2023学年高三英语知识点试题含解析
- 浙江省台州市椒江区第二中学2022-2023学年高三英语月考试题含解析
- 中秋活动方案6篇
- 保龄球馆消毒指导记录
- 安徽省六安市霍邱户胡镇中学高三数学文模拟试卷含解析
- 摩托车俱乐部传染病管理规范
- 养马场病虫害防治检查记录
- 高三下学期第三次高考模拟测试语文试卷-带参考答案
- 仓库设计与规划流程
- 2024年湖州市新 闻传媒中心招聘笔试参考题库附带答案详解
- 2024-2029全球及中国蛋白质纯化系统行业市场发展分析及前景趋势与投资发展研究报告
- 小学班会 课堂互动小游戏 扭蛋机抽奖课堂奖励游戏 课件
- 智慧养老项目可行性研究报告
- 人工智能的教育与培训
- 天津南开中学2024届高三第二次检测政治试卷及答案
- 灵芝融资计划书
- 智慧教育平台应用培训课件
- 老年人衰弱护理课件
评论
0/150
提交评论