Android移动开发基础案例教程 第3章 Activity

7 篇文章 18 订阅


image.png

3.1 Activity的创建

1)包名处点击右键选择【New】【Activity】【Empty Activity】选项,填写Activity信息,完成创建。

image.png

3.2 Activity的生命周期

image.png

image.png

3.3 Activity的启动模式

3.3.1 Android中的任务栈

栈是一种“先进后出”的数据结构。Android中,采用任务栈的形式来管理Activity。

image.png

3.3.2 Activity的四种启动模式

standard模式是Activity的默认启动方式,每启动一个Activity就会在栈顶创建一个新的实例。

1.standard模式

image.png

2.singleTop模式

  • singleTop模式会判断要启动的Activity实例是否位于栈顶,如果位于栈顶则直接复用,否则创建新的实例。

image.png

  • singleTask模式下每次启动该Activity时,系统首先会检查栈中是否存在当前Activity实例,如果存在则直接使用,并把当前Activity之上的所有实例全部出栈。

image.png

3.singleInstance模式

singleInstance模式会启动一个新的任务栈来管理Activity实例,无论从哪个任务栈中启动该Activity,该实例在整个系统中只有一个。

image.png

3.4 Activity之间的跳转

  • Intent被称为意图,是程序中各组件进行交互的一种重要方式,它不仅可以指定当前组件要执行的动作,还可以在不同组件之间进行数据传递。
  • 一般用于启动Activity、Service以及发送广播等。根据开启目标组件的方式不同,Intent被分为两种类型显示意图和隐式意图。

image.png

3.4.1 显式意图

image.png

3.4.2 隐式意图

image.png

清单文件?

3.4.2 实战演练——打开浏览器

image.png

java文件:源代码\chapter03\OpenBrowser\app\src\main\java\cn\itcast

package cn.itcast.openbrowser;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.main_button);
        Intent intent = getIntent();
        String data  = intent.getStringExtra("extra_data");


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                //设置动作为android.intent.action.VIEW
                intent.setAction("android.intent.action.VIEW");
                //设置要打开的网址
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
    }
}

视图文件:\源代码\chapter03\OpenBrowser\app\src\main\res\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:background="@drawable/openbrowser"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/main_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/click"/>
</RelativeLayout>
h

源代码\chapter03\OpenBrowser\app\src\main\AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.itcast.openbrowser" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

15 LAUNCHER <一打开就要发送/>

3.5 Activity中的数据传递

3.5.1 数据传递
  • Activity之间传递数据需要用到Intent提供的putExtra()方法。

image.png

3.5.2 实战演练——注册用户信息

image.pngimage.png

3.5.3 数据回传

image.png

image.png

3.5.4 实战演练——选择宝宝装备

image.png

附代码,讲解待补充

布局文件 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:background="@drawable/loading"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/iv_head"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:src="@drawable/head"/>
    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv_head"
        android:layout_margin="10dp"
        android:orientation="vertical">
        <RelativeLayout
            android:id="@+id/regist_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp">
            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="用户名:"
                android:textSize="20sp"/>
            <EditText
                android:id="@+id/et_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/tv_name"
                android:hint="请输入用户名"
                android:textSize="16sp"/>
        </RelativeLayout>
        <RelativeLayout
            android:id="@+id/regist_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp">
            <TextView
                android:id="@+id/tv_psw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="密    码:"
                android:textSize="20sp"/>
            <EditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@id/tv_psw"
                android:hint="请输入密码"
                android:inputType="textPassword"
                android:textSize="16sp"/>
        </RelativeLayout>
    </LinearLayout>
    <Button
        android:id="@+id/btn_send"
        android:layout_width="160dp"
        android:layout_height="48dp"
        android:layout_below="@id/layout"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/start"
        android:text="注册"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        android:textStyle="bold"/>
</RelativeLayout>

activity_shop.xml 多的代码是宝宝装备案例用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/rl"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/loading"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#307f7f7f"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="5dp">
        <ImageView
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@android:drawable/ic_menu_info_details"/>
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="商品名称"/>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:orientation="vertical">
            <TextView
                android:id="@+id/tv_life"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="生命值"
                android:textSize="13sp"/>
            <TextView
                android:id="@+id/tv_attack"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="攻击力"
                android:textSize="13sp"/>
            <TextView
                android:id="@+id/tv_speed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="速度"
                android:textSize="13sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

java文件:MainActivity.java

package cn.itcast.userregist;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
    private EditText et_password;
    private Button btn_send;
    private EditText et_name;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_send = (Button) findViewById(R.id.btn_send);
        //点击开始游戏按钮进行数据传递
        btn_send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                passDate();
            }
        });
    }
    //传递数据
    public void passDate() {
        //创建Intent对象,启动Activity02
        Intent intent = new Intent(this, ShowActivity.class);
        //将数据存入Intent对象
        intent.putExtra("name", et_name.getText().toString().trim());
        intent.putExtra("password", et_password.getText().toString().trim());
        startActivity(intent);
    }
}

\源代码\chapter03\UserRegist\app\src\main\java\cn\itcast\userregist\ShowActivity.java

package cn.itcast.userregist;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ShowActivity extends AppCompatActivity {
    private ProgressBar mProgressBar1;
    private ProgressBar mProgressBar2;
    private ProgressBar mProgressBar3;
    private TextView mLifeTV;
    private TextView mAttackTV;
    private TextView mSpeedTV;
    private TextView tv_name;
    private TextView tv_password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        //获取Intent对象(隐式意图)
        Intent intent = getIntent();
        //取出key对应的value值
        String name = intent.getStringExtra("name");
        String password = intent.getStringExtra("password");
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_password = (TextView) findViewById(R.id.tv_password);
        tv_name.setText("用户名:" + name);
        tv_password.setText("密    码:" + password);
        //下面是数据围传例子的相关代码
        mLifeTV = (TextView) findViewById(R.id.tv_life_progress);
        mAttackTV = (TextView) findViewById(R.id.tv_attack_progress);
        mSpeedTV = (TextView) findViewById(R.id.tv_speed_progress);
        initProgress();                    //初始化进度条
    }
    private void initProgress() {
        mProgressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
        mProgressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
        mProgressBar3 = (ProgressBar) findViewById(R.id.progressBar3);
        mProgressBar1.setMax(1000);        //设置最大值1000
        mProgressBar2.setMax(1000);
        mProgressBar3.setMax(1000);
    }
    // 开启新的activity并获取他的返回值
    public void click(View view) {
        Intent intent = new Intent(this, ShopActivity.class);
        startActivityForResult(intent, 1); // 返回请求结果,请求码为1
    }
    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            // 判断结果码是否等于1,等于1为宝宝添加装备
            if (resultCode == 1) {
                if (requestCode == 1) {
                    ItemInfo info =
                            (ItemInfo) data.getSerializableExtra("equipment");
                    //更新ProgressBar的值
                    updateProgress(info);
                }
            }
        }
    }
    //更新ProgressBar的值
    private void updateProgress(ItemInfo info) {
        int progress1 = mProgressBar1.getProgress();
        int progress2 = mProgressBar2.getProgress();
        int progress3 = mProgressBar3.getProgress();
        mProgressBar1.setProgress(progress1 + info.getLife());
        mProgressBar2.setProgress(progress2 + info.getAcctack());
        mProgressBar3.setProgress(progress3 + info.getSpeed());
        mLifeTV.setText(mProgressBar1.getProgress() + "");
        mAttackTV.setText(mProgressBar2.getProgress() + "");
        mSpeedTV.setText(mProgressBar3.getProgress() + "");
    }
}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.itcast.userregist" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ShowActivity" >
        </activity>
        <activity android:name=".ShopActivity" >
        </activity>
    </application>

</manifest>

\源代码\chapter03\UserRegist\app\src\main\java\cn\itcast\userregist*ShopActivity*.java

package cn.itcast.userregist;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ShopActivity extends AppCompatActivity implements
        View.OnClickListener {
    private ItemInfo itemInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        itemInfo = new ItemInfo("金剑", 100, 20, 20);
        findViewById(R.id.rl).setOnClickListener(this);
        TextView mLifeTV = (TextView) findViewById(R.id.tv_life);
        TextView mNameTV = (TextView) findViewById(R.id.tv_name);
        TextView mSpeedTV = (TextView) findViewById(R.id.tv_speed);
        TextView mAttackTV = (TextView) findViewById(R.id.tv_attack);
        mLifeTV.setText("生命值+" + itemInfo.getLife());
        mNameTV.setText(itemInfo.getName() + "");
        mSpeedTV.setText("敏捷度+" + itemInfo.getSpeed());
        mAttackTV.setText("攻击力+" + itemInfo.getAcctack());
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.rl:
                Intent intent = new Intent();
                intent.putExtra("equipment", itemInfo);
                setResult(1, intent);
                finish();
                break;
        }
    }
}

ItemInfo.java封装类

package cn.itcast.userregist;
import java.io.Serializable;
public class ItemInfo implements Serializable {
    private String name;
    private int acctack;
    private int life;
    private int speed;
    public ItemInfo(String name, int acctack, int life, int speed) {
        this.name = name;
        this.acctack = acctack;
        this.life = life;
        this.speed = speed;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAcctack() {
        return acctack;
    }
    public void setAcctack(int acctack) {
        this.acctack = acctack;
    }
    public int getLife() {
        return life;
    }
    public void setLife(int life) {
        this.life = life;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}

3.6 本章小结

​ 本章主要讲解了Activity的相关知识,包括Activity入门、Activity生命周期、Activity启动模式、Intent的使用以及Activity中的数据传递,并在讲解各个知识点时编写了相应的使用案例。在应用程序中凡是有界面都会使用到Activity,因此,要求初学者必须熟练掌握该组件的使用。

【学习笔记】

【学习资料】

  • 3
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 《Android移动开发基础案例教程第二版》是一本非常实用的Android开发指南,提供了丰富的基础案例教程,帮助读者提高Android开发技能。 该书配合源码使用,学习者可根据书中教程,从源码中学习如何使用Android SDK进行开发。源码中包含了丰富的示例代码,如UI控件基础、布局管理器、事件处理、Fragment碎片、Intent启动、ListView列表显示、GridView网格显示、RecyclerView列表及网格等常用控件使用方法,通过运行源码,读者可以更加清楚地理解这些知识点的实际应用场景。 同时,源码中也提供了完整的工程结构,读者可以从项目中学习如何搭建Android应用,如何进行资源文件的管理和使用等,为日后开发奠定了扎实的基础。 总之,《Android移动开发基础案例教程第二版》配合源码使用,可以帮助读者迅速掌握开发Android应用的基础技能,在实践中积累经验,为日后的Android开发之路打下坚实的基础。 ### 回答2: 《Android移动开发基础案例教程(第二版)》是一本针对初学者编写的Android开发指南。本书的源码包含了许多实用案例,可以帮助读者更好地学习和掌握Android开发的基础知识。 本书的源码总共包含了16个案例,其中包括了Activity、Intent、UI设计、布局、数据存储、网络通信等多个方面的内容。每个案例都循序渐进地展示了整个开发过程,从界面设计到逻辑代码的实现,再到测试和发布,全方位地让读者理解每一步。 此外,本书的源码也具有高精度、高效率、易扩展的特点,并且代码注释非常详细,使得读者可以快速了解每个函数和变量的作用,加深对代码的理解。 总体来说,《Android移动开发基础案例教程(第二版)》是一本优秀的Android开发入门指南,通过学习书中的源码,读者不仅可以熟悉Android开发的基础知识,还可以掌握开发过程中的一些规范和最佳实践,从而更加高效地进行开发。 ### 回答3: 《Android移动开发基础案例教程 第二版》是一本介绍Android开发基础知识的教材,它通过不同的案例来讲解Android开发中常用的技术和工具。本书的第二版比第一版更新了更多的实例代码,同时更新了对Android Studio工具的介绍。 本书的代码清晰易懂,是初学者入门Android开发的好教材。该书源码可以在GitHub上下载,包含了本书中所有案例的完整代码和资源文件。学习者可以通过运行源码来快速了解案例的实现原理,并且可以自己进行修改和改进。 本书的案例涉及的知识点包括了Android界面设计、多媒体开发、网络编程等多个方面。通过学习这些案例,读者可以逐步掌握Android开发的关键技术,提升自己的编程能力。 总之,通过学习《Android移动开发基础案例教程 第二版》的源码,读者可以快速入门Android开发,掌握关键的技术和工具,同时也可以通过源码的改进和扩展来深入学习Android开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是我,Zack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值