安卓学习笔记 2 activity跳转与参数传递

1、activity介绍

 

1.1 activity的生命周期

 

1.2金字塔型的生命周期

 

 

 

1.3什么是intent

 Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件。通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来请求。

在这些组件之间的通讯中,主要是由Intent协助完成的。
Intent
负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。

因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。

例如,在一个联系人维护的应用中,当我们在一个联系人列表屏幕(假设对应的ActivitylistActivity)上,点击某个联系人后,希望能够跳出此联系人的详细信息屏幕(假设对应的ActivitydetailActivity)

跳转的实现

为了实现这个目的,listActivity需要构造一个 Intent,这个Intent用于告诉系统,我们要做“查看”动作,此动作对应的查看对象是“某联系人”,然后调用startActivity (Intent intent)
将构造的Intent传入,系统会根据此Intent中的描述,到ManiFest中找到满足此Intent要求的Activity,系统会调用找到的 Activity,即为detailActivity,最终传入IntentdetailActivity则会根据此Intent中的描述,执行相应的操作。



 

实例1

第一步 建立两个页面的xml布局

Activity_main.xml

<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=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转" />

</RelativeLayout><!--在第一个页面的布局文件新建一个id为btn1的按钮,这是之后要用的按钮-->
Activity_Other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是第二个页面"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
    
    
    
}
//简要的说明第二个界面

第二步 建立跳转到的页面

Otheractivity.java 

package com.example.day2activity;

import android.app.Activity;
import android.os.Bundle;

public class OtherActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);//设置布局文件
    }

第三步 改写MainActivity.java

MainActivity.java
package com.example.day2activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button btn1;//建立一个按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//设置布局
        btn1 = (Button) this.findViewById(R.id.btn1);//给按钮赋值
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override//给按钮增加点击属性
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this,
                        OtherActivity.class);/*建立一个intent,参数是两个页面*/
                startActivity(intent);//打开页面
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

第四步 在androidmanifest声明

 

 

</activity>

        <activity android:name=".OtherActivity"></activity>

技术分享

 

2、activity的传值

Ø 1、通过Intent传递数据

Ø 2、通过静态变量传递数据

Ø 3、通过剪切板传递数据

Ø 4、通过全局变量传递数据

2.1、Intent传递

我们可以通过Intent类中的putExtra方法可以将简单的数据类型和序列化对象保存到Intent对象中,然后在目标的Activity中使用getXxx方法取出对应的数据

实例2:

技术分享

 

第一步 建立布局文档

Layout_main.xml
<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=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转并传递数据" />

</RelativeLayout>
Layout_other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

改写主activity

MainActivity.java
package com.example.day2intent;

import java.io.ObjectOutputStream.PutField;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button btn1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button) this.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this,
                        OtherActivity.class);
                intent.putExtra("name", "刘帅康");
                intent.putExtra("age", 21);
                intent.putExtra("address", "合肥");
                startActivity(intent);

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

建立跳转的activity

OtherActivity.java
package com.example.day2intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {
    private TextView msg;
    private String name;
    private String address;
    private int age;
    @Override
    
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        msg = (TextView)this.findViewById(R.id.msg);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        address = intent.getStringExtra("address");
        age = intent.getIntExtra("age", 0);
        msg.setText("姓名:"+name+"\n"+"年龄:"+age+"\n"+"地址:"+address);
    }

}

Manifest添加

   <activity android:name=".OtherActivity"></activity>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。