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
Activity提供了和用户交互的可视化界面。创建一个Activity一般是继承Activity(也可以是LisActivity,MapActivity等),覆盖Activity的onCreate( )方法,在该方法中调用setContentView( )方法来展示要显示的视图,调用findViewById( )方法实例化组件。注意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>
效果图:
在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>
效果图: