Android 使用全局变量传递数据
使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量。申明后,全局调用。只有当内存被清理后,才被销毁。否则一直可以调用。
还是使用点击一个button,传递一个数据到另一个页面中显示。
首先,新建工程,然后,打开mainActivity.xml文件,然后,添加button。
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用Application传递数据" />
先新建一个MyApp.java类,存储全局变量
public String name;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setName("hello");
}
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public MyApp() {
// TODO Auto-generated constructor stub
}
然后在MainActivity.java中,添加
private Button button;
private MyApp myapp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取全局变量数据
myapp=(MyApp)getApplication();
//myapp.setName("world");//可重新赋值
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
}
新建布局文件other.xml文件加入
<TextView android:id="@+id/msg" android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
新建OtherActivity.java文件
public class OtherActivity extends Activity {
private MyApp myapp;
private TextView textview;
public OtherActivity() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textview = (TextView)this.findViewById(R.id.msg);
myapp = (MyApp)getApplication();
textview.setText("name---->"+myapp.getName());
}
}
最后在清单文件中注意:先在标签application中加入
<activity android:name=".OtherActivity"></activity>
再在application标签中加入属性android:name=".MyApp"
Android 使用全局变量传递数据的更多相关文章
- android使用全局变量传递数据
android中Application是用来保存全局变量的,在package创建的时候就存在了,到所有的activity都被destroy掉之后才会被释放掉.所以当我们需要全局变量的时候只要在appl ...
- activity之间通过全局变量传递数据
activity之间通过全局变量传递数据 一.简介 Application域中的onCreate方法是Android程序的入口,Android程序运行的时候就自动加载Application的对象,感觉 ...
- Android剪切板传递数据传递序列化对象数据
一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用A ...
- Android 使用意图传递数据
使用意图传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <But ...
- Android与NativeC传递数据不正确问题
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Android studio 2.3.3 这两天一直在调试一个BUG,具体为通过 NativeC 来处理上层Android ...
- Android 通过Application 传递数据
</pre><pre> package com.example.ApplicationTest; import android.app.Application; /** * C ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
- Android开发—— 传递数据
一:使用静态变量传递数据 (1)静态变量传递数据,在目标Activity中声明静态变量,然后使用setText()方法将静态变量的值导出即可: (2)静态变量传递数据,在主Activity中对目标Ac ...
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...
随机推荐
- “我爱淘”冲刺阶段Scrum站立会议4
完成任务: 完成了搜索界面的Activity的编写. 计划任务: 实现数据库的链接,用户可以查到自己需要的书籍的信息. 遇到问题: 数据库的操作,实现查询功能:
- 三、freemarker数据、模版指令
数据类型 1. 直接指定值(字符串.数值.布尔值.集合.Map对象) 2. 字符串:直接指定字符串使用单引号.双引号,字符中间可以使用转义符“\”,如果字符内有大量特殊 ...
- Google Guava学习笔记——基础工具类针对Object类的使用
Guava 提供了一系列针对Object操作的方法. 1. toString方法 为了方便调试重写toString()方法是很有必要的,但写起来比较无聊,不管如何,Objects类提供了toStrin ...
- Beta版软件说明书
软件使用说明书 一. 软件概述 本软件面向广大简易图片使用者,旨在为用户提供简单方便的不对其他软件产生依赖的截图软件,可以脱机使用. 二. 运行环境 个人电脑,Windows7操作系统,. ...
- SQL Server 执行计划中的扫描方式举例说明
SQL Server 执行计划中的扫描方式举例说明 原文地址:http://www.cnblogs.com/zihunqingxin/p/3201155.html 1.执行计划使用方式 选中需要执行的 ...
- 【Ural】【1519】Formula 1
插头DP 本题为CDQ<基于连通性状态压缩的动态规划的……(我忘了)>里的例题!(嗯就是这样……) 先膜拜一下ccy大神……http://blog.sina.com.cn/s/blog_5 ...
- WPF 与Surface 2.0 SDK 亲密接触 - ScatterView 数据绑定篇
与我们常用的一些WPF 控件相同,ScatterView 控件也支持数据绑定功能.本篇将演示如何利用ScatterView 绑定Win7 系统中的样例图片,并且每张图片会以独立的ScatterView ...
- Swift 2.0 到底「新」在哪?
[编者按]2015年6月,一年一度的苹果 WWDC 大会如期而至,在大会上苹果发布了 Swift 2.0,引入了很多新的特性,以帮助开发者更快.更简单地构建应用.本篇文章作者是 Maxime defa ...
- poj 3903 Stock Exchange(最长上升子序列,模版题)
题目 #include<stdio.h> //最长上升子序列 nlogn //入口参数:数组名+数组长度,类型不限,结构体类型可以通过重载运算符实现 //数组下标从1号开始. int bs ...
- nested pop animation can result in corrupted navigation bar
nested pop animation can result in corrupted navigation barFinishing up a navigation transition in a ...