安卓学习笔记 3 activity参数传递

Android学习 Day3 Activity几种参数传递方式

Ø 1、通过Intent传递数据

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

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

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

昨天已经使用intent跳转并且传递数值

今天使用剩下的三种方式。传递变量。

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

使用Intent可以很方便在不同的Activity之间传递数据,这个也是官方推荐的方式,但是也有一定的局限性,就是Intent无法传递不能序列化的对象。我们可以使用静态变量来解决这个问题

首先在OtherActivity生命静态全局变量

然后在mainActivity设置。

主要代码OtherActivity:

private TextView msg;

public static String name;

public static String address;

public static 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);

msg.setText("姓名:"+name+"\n"+"年龄:"+age+"\n"+"地址:"+address);

}

mainActivity:

Intent intent = new Intent();

intent.setClass(MainActivity.this, OtherActivity.class);

OtherActivity.age = 21;

OtherActivity.name = "刘帅康";

OtherActivity.address = "利辛县";

startActivity(intent);

MainActivity.java
package com.example.day3intent2;

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();
                intent.setClass(MainActivity.this, OtherActivity.class);
                OtherActivity.age = 21;
                OtherActivity.name = "刘帅康";
                OtherActivity.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;
    }

}
otherActivity.java
package com.example.day3intent2;

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

public class OtherActivity extends Activity {
    private TextView msg;
    public static String name;
    public static String address;
    public static 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);
        msg.setText("姓名:"+name+"\n"+"年龄:"+age+"\n"+"地址:"+address);
    }

}
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"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="静态变量传递数值" />

</RelativeLayout>
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/msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="default" />

</LinearLayout>

Manifest

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

3、通过剪切板传递数据

在Activity之间数据传递还可以利用一些技巧,不管是Windows还是Linux操作系统,都会支持一种叫剪切板的技术,也就是某一个程序将一些数据复制到剪切板上,然后其他的任何程序都可以从剪切板中获取数据。

MainActivity.java
package com.example.day3intent3;

import java.net.ContentHandler;

import android.R.integer;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
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() {

            @SuppressLint("NewApi")
            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            //获取系统剪贴板服务到fs【发送】
                ClipboardManager fs = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //定义发送内容以及发送
                String fscontent = "姓名:刘帅康\n年龄:21\n地址:利辛县";
                fs.setText(fscontent);//向剪贴板发送!
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, OtherActivity.class);
                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;
    }

}
OtherActivity.java
package com.example.day3intent3;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;

public class OtherActivity extends Activity {
    private TextView msg;

    @SuppressLint("NewApi")
    @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);
        // 定义一个剪贴板接收器js【接受】!
        ClipboardManager js = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        String message;// 定义存储内容的变量
        message = js.getText().toString();// 接收剪贴板文本
        msg.setText(message);

    }

}

Ø 全局变量传递数

在Activity之间数据传递中还有一种比较实用的方式,就是全局对象,实用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是Page、Request、Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问。

需要新建一个集成application的类!例如类名为MyName 并且在manifest里面需要在application标签里声明android:name=".MyName"

MyName.java
package com.example.day4intent4;

import android.app.Application;

public class MyName extends Application {
public String name;

    public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

    @Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    setName("张三");
}

}
MainActivity.java
package com.example.day4intent4;

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;
    private MyName myname;

    @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
                myname = (MyName) getApplication();
                myname.setName("刘帅康");
                Intent intent = new Intent(MainActivity.this,
                        OtherActivity.class);
                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;
    }

}
MainActivity.java
package com.example.day4intent4;

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;
    private MyName myname;

    @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
                myname = (MyName) getApplication();
                myname.setName("刘帅康");
                Intent intent = new Intent(MainActivity.this,
                        OtherActivity.class);
                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;
    }

}
main_activity.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="Application传递数据"/>
</RelativeLayout>
Other_activity.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_height="wrap_content"
    android:layout_width="match_parent"
    android:text="default text"/>
</LinearLayout>
时间: 2024-07-31 02:36:16

安卓学习笔记 3 activity参数传递的相关文章

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

1.activity介绍 1.1 activity的生命周期   1.2金字塔型的生命周期 1.3什么是intent  Intent是一种运行时绑定(runtime binding)机制,它能在程序运行的过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意愿的内容选择适当的组件来请求. 在这些组件之间的通讯中,主要是由Intent协助完成的. Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根

安卓学习笔记(二)基本构件

1.Activity 一个Activity,通常是用户在某一个时刻,在设备上看到的单独的界面.一个应用程序通常含有多个Activity,用户可在期间进行切换.对用户而言,这就是程序的外观部分. Activity的生命周期:启动一个Activity可能会消耗大量资源.他可能会涉及到新建一个Linux进程.为UI对象申请内存空间.从UML布局填充所有对象,以及创建整个界面.为了避免这种浪费,Android通过ActivityManager来管理活动的生命周期. ActivityManager负责创建

【安卓学习笔记1】安卓基本组件Activity,Service,BoradCastReceiver,ContentProvider简介

因为博主以前学习过安卓的一点知识,所以学习笔记和别人不太一样. 博主的学习参考书籍是疯狂android讲义第二版.本系列博客都是记录学习笔记的. 因为博主平时还要上班,所以也就晚上有时间看书,写博客. 博主坚持每天看一个小时的书,记录一下随笔心得. 希望能给大家带来帮助. =========================================================================== 安卓的基本组件 =============================

安卓学习笔记一

在学习安卓之前,首先要对编程平台有一定的了解,才会方便我们日后的操作,这里我用的是eclipse集成的安卓环境. 编写代码总的来说可以划分为两部分,一个是安卓语言如配置权限,设置样式,放置控件之类的,另一个是java语言,实现程序的功能.所以安卓的学习主要还是控件与安卓API的使用,而JAVA语言这里不再赘述. 至于创建安卓工程,只要能大概认识英语,也就知道过程中设置的具体是什么, 其中依次为设置工程名,安卓型号,创建ACTIVITY,设置工作目录,设置图标样式,设置ACTIVITY样式,设置a

安卓开发笔记——深入Activity

在上一篇文章<安卓开发笔记——重识Activity >中,我们了解了Activity生命周期的执行顺序和一些基本的数据保存操作,但如果只知道这些是对于我们的开发需求来说是远远不够的,今天我们继续探索Activity,来了解下关于Activity任务栈和Activity四种启动模式的区别. 为什么需要了解关于Activity的任务栈,其实最直接的体现就是提高用户交互友好性. 举个例子,当我们去浏览一个新闻客户端的时候,我们进入了新闻详情页,在这个页面有相隔两条的新闻标题,当我们去点击这个标题的时

安卓学习笔记---Activity

由于学期实训的要求,我开始学习安卓了.从本月一号开始,学了五天了.时间短,刚学到的东西容易忘,我记一下笔记. 首先是对Activity的理解.activity首先是一个java类,我们创建一个新的activity类,得继承Activity,这是android的jar包里的. 我的理解他就是一个控件的容器,就是我们使用手机时看的的一个窗口.本身没有什么实质的内容.       每一个创建的activity都得在AndroidManifest.xml文件中注册一下. 都要重新onCreate方法.

【读书笔记】安卓学习笔记第一篇——个人杂谈

最近打算转向安卓平台的安全了,因为之前一直没有接触过安卓平台的安全,所以要从最底层的安全机制开始学起.一直以来都是在做Windows平台的安全的,对于Linux的了解不多,而安卓恰好又是基于linux平台的东西.所以搞安卓平台就很有必要去学习一下系统机制方面的东西了.最近看了一些资料, 对于安卓平台有了一个初步的理解,首先安卓给我的感受就是系统的层次很复制.我比较熟悉Windows的东西,Windows是基于子系统进行架构的,但是事实上子系统根本就只有一个win32,对于应用层来说很简明,就是系

【安卓学习笔记2】UI基础知识&amp;View&amp;&amp;ViewGroup

万丈高楼平地起,安卓开发的最直观个人也认为任务最多的部分就是UI的开发了. 那么用户在前台看到的东西是什么呢? 没错,是Activity! 而Activity只是一个窗体而已,真正显示给用户看的是View. 如果熟悉Web开发的人员就明白MVC的概念.这个View就是V层,显示给用户看的东西 同时用于交互产生Model数据,以便交给Controller处理. Android中View是所有UI组件的基类,其下有ViewGroup子类,一般作为各View组件的容器使用 常见如我们熟悉的各种Layo

安卓学习之活动(Activity)

1)什么是活动 活动(Activity)是最容易吸引到用户的地方了,它是一种可以包含用户界面的组件,主要用于和用户进行交互. 2)手动创建活动 1.在src中新新建包,然后新建类继承自Activity,然后点击Finish.        2.对于活动,我们要重写它的onCreate()方法.我们可以根据需要在这个方法里面添加我们所要的.        3.创建和加载布局,在res/layout目录下新建Android XML File,然后我们通过xml文件的方式来编辑,可以在里面写入需要的.