Android中Intent初识.docx

上传人:b****8 文档编号:23867198 上传时间:2023-05-21 格式:DOCX 页数:20 大小:469.76KB
下载 相关 举报
Android中Intent初识.docx_第1页
第1页 / 共20页
Android中Intent初识.docx_第2页
第2页 / 共20页
Android中Intent初识.docx_第3页
第3页 / 共20页
Android中Intent初识.docx_第4页
第4页 / 共20页
Android中Intent初识.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

Android中Intent初识.docx

《Android中Intent初识.docx》由会员分享,可在线阅读,更多相关《Android中Intent初识.docx(20页珍藏版)》请在冰豆网上搜索。

Android中Intent初识.docx

Android中Intent初识

Intent初识

Intent是一个对象,它包含了一组信息

1.Componentname:

组件名称

2.Action:

动作

3.Data:

数据

4.Category

5.Extras:

额外的信息,通常为一些键值对

6.Flags

实例一:

使用Intent实现多个Activity之间的跳转,多个Activity可能并不在一个应用程序中,可能在多个应用程序中

再次运行,结果如下:

strings.xml文件类容如下:

xmlversion="1.0"encoding="utf-8"?

>

点击获取到第一个Activity传过来的信息!

IntentTest

点击跳转到第二个Activity

main.xml文件类容如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/second"

/>

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

id="@+id/second"

android:

text="@string/second"

/>

second.xml文件内容如下:

xmlversion="1.0"encoding="utf-8"?

>

xmlns:

android="

android:

orientation="vertical"

android:

layout_width="match_parent"

android:

layout_height="match_parent">

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

text="@string/first"

/>

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

id="@+id/first"

android:

text="@string/first"

/>

IntentestActivity.java文件类容如下:

packagejiao.jiao;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

publicclassIntentTestActivityextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

privateButtonbutton;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

button=(Button)this.findViewById(R.id.second);

button.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

Intentintent=newIntent();//使用Intent的无参构造函数

//其中两个参数,表示,从哪个Activity跳转到哪个Activity

intent.setClass(IntentTestActivity.this,secondActivity.class);

//可以向第二个Activity传递数据

intent.putExtra("usr","jiaoxiaohua");

startActivity(intent);

}

});

}

}

secondActivity.java内容如下:

packagejiao.jiao;

importandroid.app.Activity;

importandroid.content.Intent;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.Toast;

publicclasssecondActivityextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

privateButtonbutton;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.second);

button=(Button)this.findViewById(R.id.first);

button.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

Intentintent=getIntent();//捕获传递过来的Intent对象

Stringstring=intent.getStringExtra("usr");//通过键获取传递过来的信息

Toast.makeText(secondActivity.this,string,2).show();

}

});

}

}

如果此时测试,会出现问题

因为每多一个Activity就要在AndroidManifest.xml中注册

AndroidManifest.xml类容如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

package="jiao.jiao"

android:

versionCode="1"

android:

versionName="1.0">

minSdkVersion="10"/>

icon="@drawable/icon"android:

label="@string/app_name">

name=".IntentTestActivity"

android:

label="@string/app_name">

name="android.intent.action.MAIN"/>

name="android.intent.category.LAUNCHER"/>

name=".secondActivity"android:

label="@string/first">

上图标注的地方为注册代码,然后就可以正常运行了

创新设计:

设计一个注册账号的Activity,当注册成功后跳转到登陆界面,用刚注册的账号进行登陆

 

Strings.xml文件如下:

xmlversion="1.0"encoding="utf-8"?

>

HelloWorld,RegesterActivity!

Regest0r

输用户名:

输入密码:

确认密码:

邮箱账号:

提交(submit)

重置(reset)

取消(cansal)

记住密码

记住地点

记住人物

登陆

取消

修改密码

欢迎来到登陆界面

 

main.xml文件如下:

xmlversion="1.0"encoding="utf-8"?

>

android="

android:

orientation="vertical"

android:

layout_height="fill_parent"

android:

layout_width="match_parent">

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

stretchColumns="1">

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

text="@string/usr"

android:

textSize="20dp"

android:

gravity="bottom"

/>

android:

layout_height="wrap_content"

android:

layout_width="match_parent"

android:

id="@+id/usrEdit"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

text="@string/password"

android:

textSize="20dp"

/>

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

password="true"

android:

id="@+id/passwordEdit"

android:

layout_below="@id/usrEdit"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

text="@string/repassword"

android:

textSize="20dp"

/>

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

id="@+id/repasswordEdit"

android:

password="true"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

text="@string/email"

android:

textSize="20dp"

/>

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

id="@+id/emailEdit"

android:

layout_below="@id/repasswordEdit"

/>

android="

android:

orientation="vertical"

android:

layout_height="fill_parent"

android:

layout_width="match_parent">

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

id="@+id/submit"

android:

text="@string/submit"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

layout_toRightOf="@id/submit"

android:

id="@+id/reset"

android:

text="@string/reset"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

layout_toRightOf="@id/reset"

android:

id="@+id/casal"

android:

text="@string/casal"

/>

 

login.xml文件如下:

xmlversion="1.0"encoding="utf-8"?

>

xmlns:

android="

android:

orientation="vertical"

android:

layout_width="match_parent"

android:

layout_height="match_parent">

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content"

android:

stretchColumns="1">

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

text="@string/usr"

android:

textSize="20dp"

android:

gravity="bottom"

/>

android:

layout_height="wrap_content"

android:

layout_width="match_parent"

android:

id="@+id/usrEdit1"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

text="@string/password"

android:

textSize="20dp"

/>

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

password="true"

android:

id="@+id/passwordEdit1"

/>

xmlns:

android="

android:

orientation="vertical"

android:

layout_width="match_parent"

android:

layout_height="match_parent">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

id="@+id/remeber"

android:

text="@string/remeber"

/>

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

id="@+id/place"

android:

text="@string/place"

/>

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

id="@+id/people"

android:

text="@string/people"

/>

android="

android:

orientation="vertical"

android:

layout_height="fill_parent"

android:

layout_width="match_parent">

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

id="@+id/denglu"

android:

text="@string/login"

/>

android:

layout_height="wrap_content"

android:

layout_width="wrap_content"

android:

layout_toRightOf="@id/denglu"

andro

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 唐诗宋词

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1