
## 实验4 AndroID中Activity使用及数据传递
实验目的
1.掌握Activity的建立与使用
2. 掌握Activity之间的数据传递
实验学时
2学时
实验内容
1.建立2个Activity,并在2个Activity之间切换。设计一个APP,主、从界面包含一个EditText和一个button。用户点击主界面按钮后,EditText显示当前是从界面,点击按钮后可以返回主界面。
第一步,创建配置Activity,再开发安卓项目时候,系统会为我们自动创建一个Activity,但是一般我们在开发过程中需要自己手动创建Activity,创建两个Activity分别为MainActivity和newActivity,如下,直接在java文件夹下的包中NEW,选择Activity,然后输入名称就可以了,创建完成如图所示,这种创建方式是最简单的,系统也会自动给你配置Activity,你不需要自己配置了。
下面在activity_main.xml布局主界面,主界面就是一个按钮和一个可编辑的文本视图,程序如下:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.nuist__njupt.wangguodong.MainActivity"> <button androID:ID="@+ID/btn1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="进入从界面" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true" /> <EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:hint="当前是主界面" androID:ID="@+ID/editText" androID:layout_below="@+ID/btn1" androID:layout_centerHorizontal="true" androID:layout_margintop="21dp" />接下来在,在activity_new中布局从界面,就是一个按钮和一个可编辑视图,代码如下:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.nuist__njupt.wangguodong.newActivity"> <button androID:ID="@+ID/btn2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="返回主界面" androID:layout_below="@+ID/btn1" androID:layout_centerHorizontal="true" androID:layout_margintop="35dp" /> <EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/editText" androID:hint="当前是从界面" androID:layout_below="@+ID/btn2" androID:layout_centerHorizontal="true" androID:layout_margintop="23dp" /></relativeLayout>接下来在MainActivity中编写主界面按钮事件程序以及跳转到从界面,我采用的是最简单的显示启动,代码如下:
import androID.content.Intent;import androID.os.Bundle;import androID.support.v7.app.ActionBaractivity;import androID.vIEw.VIEw;import androID.Widget.button;public class MainActivity extends ActionBaractivity { /* Author WangGuodong */ @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); button button1 = (button) findVIEwByID(R.ID.btn1);//获取按钮属性 //显式启动Activity button1.setonClickListener(new VIEw.OnClickListener() { //创建按钮监听类,重写监听方法 @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(MainActivity.this, newActivity.class); //创建intent对象 startActivity(intent); //启动Activity } }); }}接下来在从界面编写按钮的事件程序以及跳转到主界面,代码如下:
import androID.content.Intent;import androID.os.Bundle;import androID.support.v7.app.ActionBaractivity;import androID.vIEw.VIEw;import androID.Widget.button;public class newActivity extends ActionBaractivity { /* Author WangGuodong */ @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_new); button button2 = (button)findVIEwByID(R.ID.btn2); //获取按钮 button2.setonClickListener(new VIEw.OnClickListener() { //设置按钮监听器 @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(newActivity.this, MainActivity.class);//创建intent对象 startActivity(intent);//启动Activity finish();//关闭Activity } }); }}然后可以在模拟器上调试了,调试的GIF动图如下:
2. 建立2个Activity,并在2个Activity之间传递数据。设计一个APP,主、从界面包含2个EditText(用于输入用户名和密码)和一个button,用户点击按钮后调至从界面,并在从界面的EditText中显示传递过来的用户名和密码,点击按钮返回主界面。
下面在activity_main.xml布局主界面,我全采用的相对布局,因为可以直接拖动组件,比较方便,主界面两个可编辑文本和一个提交按钮,程序如下:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.c,om/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.nuist__njupt.wangguodong.MainActivity"> <EditText androID:ID="@+ID/yonghuminng" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:hint="请输入你的用户名"/> <EditText androID:ID="@+ID/mima" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:hint="请输入密码" androID:layout_below="@+ID/yonghuminng" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" /> <button androID:ID="@+ID/submit" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="提交" androID:layout_below="@+ID/yonghuminng" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:layout_margintop="51dp" /></relativeLayout>接下来在,在activity_new中布局从界面,就是一个按钮和两个文本视图,代码如下:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.nuist__njupt.wangguodong.newActivity"> <button androID:ID="@+ID/btn2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="返回主界面" androID:layout_margintop="132dp" androID:layout_below="@+ID/text1" androID:layout_centerHorizontal="true" /> <TextVIEw androID:ID="@+ID/text1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> <TextVIEw androID:ID="@+ID/text2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/text1" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true"/></relativeLayout>接下来在MainAcctivity中编写主界面按钮事件程序以及跳转到从界面,同时设置监听Edittext的事件,使用Bundle对象在MainActivity和newActivity中传递information对象,代码如下:
import androID.content.Intent;import androID.os.Bundle;import androID.support.v7.app.ActionBaractivity;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Radiobutton;public class MainActivity extends ActionBaractivity { /* Author WangGuodong */ private EditText Username,Password ; private button submit; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); setTitle("用户登录系统"); submit = (button) findVIEwByID(R.ID.submit); submit.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Username = (EditText) findVIEwByID( R.ID.yonghuminng); Password = (EditText) findVIEwByID(R.ID.mima); information information = new information( Username.getText().toString(), Password.getText().toString()); //创建Bundle对象 Bundle bundle = new Bundle(); bundle.putSerializable("information", information); Intent intent = new Intent(MainActivity.this, newActivity.class); intent.putExtras(bundle); startActivity(intent); } }); }}接下来在从界面编写按钮的事件程序以及跳转到主界面,另外获取用户登录名和密码的信息,代码如下:
import androID.content.Intent;import androID.os.Bundle;import androID.support.annotation.Nullable;import androID.support.v7.app.ActionBaractivity;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.TextVIEw;public class newActivity extends ActionBaractivity { /* Author WangGuodong */ @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_new); setTitle("用户登录"); TextVIEw nickname = (TextVIEw) findVIEwByID(R.ID.text1); TextVIEw age = (TextVIEw)findVIEwByID(R.ID.text2); information info = (information) getIntent().getSerializableExtra("information"); nickname.setText("用户名:" + info.getUsername()); age.setText("密码:" + info.getpassword()); button button2 = (button)findVIEwByID(R.ID.btn2); //获取按钮 button2.setonClickListener(new VIEw.OnClickListener() { //设置按钮监听器 @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(newActivity.this, MainActivity.class);//创建intent对象 startActivity(intent);//启动Activity finish();//关闭Activity } }); }}接下来需要创建一个名为information的Activity,直接NEW,输入information,即创建成功,information类是访问和修改用户名与密码的程序。
信息类的代码如下,该类实现了Serializable接口:
import java.io.Serializable;public class information implements Serializable {/* Author WangGuodong */ private String Username, Password; public information(String Username, String Password) { this.Username = Username; this.Password = Password; } public String getUsername() { return Username; } public voID setUsername(String Username) { this.Username = Username; } public String getpassword() { return Password; } public voID setPassword(String Password) { this.Password = Password; } @OverrIDe public String toString() { return "information{" + ", Username='" + Username + '\'' + ", Password='" + Password + '\'' + '}'; }}运行结果如下,看动图:
实验小结没有最终的成功,也没有最终的失败,有的是继续前行的勇气,加油! 总结
以上是内存溢出为你收集整理的实验4 Android中Activity使用及数据传递全部内容,希望文章能够帮你解决实验4 Android中Activity使用及数据传递所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)