Android开发学习之Activity的简介
1.Activity的概念介绍
Activity是Android组件中最基本也是最常用的一种组件,在一个Android应用中,一个Activity通常就是一个单独的屏幕。每一个Activity都被实现为一个独立的类,并且继承于Activity这个基类。
activity类处于android.app包中,继承体系如下:
1.java.lang.Object
2.android.content.Context
3.android.app.ApplicationContext
4.android.app.Activity
2.Activity的创建
Activity提供了和用户交互的可视化界面。创建一个Activity一般是继承Activity(也可以是LisActivity,MapActivity等),覆盖Activity的onCreate( )方法,在该方法中调用setContentView( )方法来展示要显示的视图,调用findViewById( )方法实例化组件。注意Activity只有在清单文件中声明才能使用。
3.Activity的应用实例
3.1两个Activity之间的切换
要做到两个Activity之间的切换(也就是从一个Activity启动另一个Activity),可以使用startActivity( )方法或者startActivityForResult( ) (能够返回结果)。这两个方法要传递的参数是组件Intent。
下面的实例是MainActivity和SecondActivity之间的切换:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <TextView
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:text="@string/hello1"
 - />
 - <Button
 - android:id="@+id/btn"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="转到SecondActivity"
 - />
 - </LinearLayout>
 
second.xml
- <?xml version="1.0" encoding="utf-8"?>
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <TextView
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:text="@string/hello2"
 - />
 - <Button
 - android:id="@+id/secondBtn"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="返回"
 - />
 - </LinearLayout>
 
MainActivity.java
- package com.android.test.activity;
 - import android.app.Activity;
 - import android.content.Intent;
 - import android.os.Bundle;
 - import android.view.View;
 - import android.view.View.OnClickListener;
 - import android.widget.Button;
 - public class MainActivity extends Activity {
 - private Button btn;
 - @Override
 - public void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.main);
 - btn = (Button)findViewById(R.id.btn);
 - //响应按钮btn事件
 - btn.setOnClickListener(new OnClickListener() {
 - @Override
 - public void onClick(View v) {
 - //显示方式声明Intent,直接启动SecondActivity
 - Intent it = new Intent(MainActivity.this,SecondActivity.class);
 - //启动Activity
 - startActivity(it);
 - }
 - });
 - }
 - }
 
SecondActivity.java
- package com.android.test.activity;
 - import android.app.Activity;
 - import android.content.Intent;
 - import android.os.Bundle;
 - import android.view.View;
 - import android.view.View.OnClickListener;
 - import android.widget.Button;
 - public class SecondActivity extends Activity {
 - private Button secondBtn;
 - @Override
 - protected void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.second);
 - secondBtn=(Button)findViewById(R.id.secondBtn);
 - //响应按钮secondBtn事件
 - secondBtn.setOnClickListener(new OnClickListener() {
 - @Override
 - public void onClick(View v) {
 - //显示方式声明Intent,直接启动MainActivity
 - Intent intent = new Intent(SecondActivity.this,MainActivity.class);
 - //启动Activity
 - startActivity(intent);
 - }
 - });
 - }
 - }
 
AndroidManifest.xml清单文件,16~18行为SecondActivity在清单文件里的声明
- <?xml version="1.0" encoding="utf-8"?>
 - <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 - package="com.android.test.activity"
 - android:versionCode="1"
 - android:versionName="1.0">
 - <uses-sdk android:minSdkVersion="10" />
 - <application android:icon="@drawable/icon" android:label="@string/app_name">
 - <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=".SecondActivity"
 - android:label="@string/app_name">
 - </activity>
 - </application>
 - </manifest>
 
效果图:


3.2.Activity之间传递数据
在Android开发中不同的Activity之间要传递数据,就需要用到对象Bundle,讲要传递的信息封装在该对象里面,并通过Intent对象传递到另一个Intent中。
下面的实例在MainActivity中输入用户名数据,并将该用户名传递给SecondActivity:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <TextView
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:text="@string/hello"
 - />
 - <EditText
 - android:id="@+id/txt"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - android:hint="请输入用户名"
 - />
 - <Button
 - android:id="@+id/btn"
 - android:layout_width="wrap_content"
 - android:layout_height="wrap_content"
 - android:text="确定"
 - />
 - </LinearLayout>
 
second.xml
- <?xml version="1.0" encoding="utf-8"?>
 - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 - android:orientation="vertical"
 - android:layout_width="fill_parent"
 - android:layout_height="fill_parent"
 - >
 - <TextView
 - android:id="@+id/secondTxt"
 - android:layout_width="fill_parent"
 - android:layout_height="wrap_content"
 - />
 - </LinearLayout>
 
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
 - <resources>
 - <string name="hello">MainActivity</string>
 - <string name="app_name">ActivityDemo</string>
 - <string name="app_name1">SecondActivity</string>
 - </resources>
 
MainActivity.java
- package com.android.test.activity;
 - import android.app.Activity;
 - import android.content.Intent;
 - import android.os.Bundle;
 - import android.view.View;
 - import android.view.View.OnClickListener;
 - import android.widget.Button;
 - import android.widget.EditText;
 - public class MainActivity extends Activity {
 - private Button btn;
 - private EditText txt;
 - @Override
 - public void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.main);
 - btn = (Button)findViewById(R.id.btn);
 - txt=(EditText)findViewById(R.id.txt);
 - //响应按钮btn事件
 - btn.setOnClickListener(new OnClickListener() {
 - @Override
 - public void onClick(View v) {
 - //获得用户名字符串
 - String useName=txt.getText().toString();
 - //声明Bundle对象
 - Bundle data=new Bundle();
 - //讲用户名信息添加到Bundle
 - data.putString("useName", useName);
 - //显示方式声明Intent,直接启动SecondActivity
 - Intent it = new Intent(MainActivity.this,SecondActivity.class);
 - //为Intent添加Bundle
 - it.putExtras(data);
 - //启动Activity
 - startActivity(it);
 - }
 - });
 - }
 - }
 
SecondActivity.java
- package com.android.test.activity;
 - import android.app.Activity;
 - import android.content.Intent;
 - import android.os.Bundle;
 - import android.widget.TextView;
 - public class SecondActivity extends Activity {
 - private TextView secondTxt;
 - @Override
 - protected void onCreate(Bundle savedInstanceState) {
 - super.onCreate(savedInstanceState);
 - setContentView(R.layout.second);
 - //获得Intent
 - Intent it=getIntent();
 - //从Intent中获得Bundle对象
 - Bundle bundle=it.getExtras();
 - //从Bundle中获得那么
 - String name=bundle.getString("useName");
 - secondTxt=(TextView)findViewById(R.id.secondTxt);
 - secondTxt.setText(name);
 - }
 - }
 
AndroidManifest.xml清单文件
- <?xml version="1.0" encoding="utf-8"?>
 - <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 - package="com.android.test.activity"
 - android:versionCode="1"
 - android:versionName="1.0">
 - <uses-sdk android:minSdkVersion="10" />
 - <application android:icon="@drawable/icon" android:label="@string/app_name">
 - <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=".SecondActivity"
 - android:label="@string/app_name1">
 - </activity>
 - </application>
 - </manifest>
 
效果图:


Android开发学习之Activity的简介的更多相关文章
- android开发学习——关于activity 和 fragment在toolbar上设置menu菜单
		
在做一个项目,用的是Android Studio 系统的抽屉源码,但是随着页面的跳转,toolbar的title需要改变,toolbar上的menu菜单也需要改变,在网上找了好久,也尝试了很多,推荐给 ...
 - Android开发学习之路--Activity之初体验
		
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
 - Android开发学习路线的七个阶段和步骤
		
Android开发学习路线的七个阶段和步骤 Android学习参考路线 第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和St ...
 - Android开发学习之LauncherActivity开发启动的列表
		
Android开发学习之LauncherActivity开发启动的列表 创建项目:OtherActivity 项目运行结果: 建立主Activity:OtherActivity.java [jav ...
 - Android开发学习之路--基于vitamio的视频播放器(二)
		
终于把该忙的事情都忙得差不多了,接下来又可以开始good good study,day day up了.在Android开发学习之路–基于vitamio的视频播放器(一)中,主要讲了播放器的界面的 ...
 - Android开发学习之路-RecyclerView滑动删除和拖动排序
		
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
 - Android开发学习路线图
		
Android开发学习方法: Android是一个比较庞大的体系,从底层的Linux内核到上层的应用层,各部分的内容跨度也比较大.因此,一个好的学习方法对我们学习Android开发很重要. 在此建议, ...
 - android开发学习笔记000
		
使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...
 - Android开发学习总结(一)——搭建最新版本的Android开发环境
		
Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...
 
随机推荐
- mysql必知必会(四、检索数据,五、排序检索数据,六、过滤数据,七、数据过滤)
			
四.select语句 1.检索单个列 select prod_name from products; 2.检索多个列 select prod_name, prod_price from product ...
 - 还原JavaScript的真实历史~
			
问题 ============ JavaScript真的继承自Cmm吗? JavaScript与Java有多少关系? JavaScirpt最初的设计是怎样的?在许多资料,JavaScript的语源被追 ...
 - 超酷的响应式dribbble设计作品瀑布流布局效果
			
相信做设计的朋友肯定都知道dribbble.com,它是一个非常棒的设计师分享作品的网站,全世界数以万计的设计高手和行家都在这个网站上分享自己的作品,当然,如果你常在上面闲逛的话,经常得到一些免费的好 ...
 - Python3 写Windows Service服务程序
			
用Python开发Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,下载路径:https://pypi.org/project/pywin32/#files ...
 - sql-的int和varchar类型拼接的问题
			
将int类型转换为varchar ,如cast(1 as varchar(10)),再进行连接set @sql =@sql+'update User set Medal='+@count+' wher ...
 - setw()函数使用
			
在C++中,setw(int n)用来控制输出间隔.例如:cout<<'s'<<setw(8)<<'a'<<endl;则在屏幕显示s a ...
 - fatal error C1853: '<filename>' is not a precompiled header file
			
当编译c和c++混合的项目时,会出现如下类似的错误 fatal error C1853: '<filename>' is not a precompiled header file 解决方 ...
 - Android实现固定头部信息,挤压动画(相似通讯录)
			
半年前,那时候我还是个大四的学生,每天都在找工作度过,想去北京体验一下蚁族生活,奋然离开了济南,哎...在济南我们学校还是数得着的好学校,去了北京就什么都不是了,一切的辛酸仅仅有自己知道,那时候的我仅 ...
 - shell命令xargs解析
			
1.多行变成单行 -bash-3.2# cat test.txt a b c d e f g o p q -bash-3.2# cat test.txt |xargs a b c d e f g o ...
 - Eclipse开发Android的配置(包括ADT安装,SDK配置)
			
1. 下载Android SDK http://code.google.com/android/download.html下载后直接解压就可以使用了. 为了在DOS控制台中直接使用SDK的工具,可 ...