Application值传递。
1、layout下面的布局
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: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="com.example.app.androidvaluespass.MainActivity"> <Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="值传递"/> </RelativeLayout>
values.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"> <EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Msg"/> </LinearLayout>
2、类及启动项
MainActivity
package com.example.app.androidvaluespass; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity { private Button btn;
private MyApp myApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.btnSubmit);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myApp = (MyApp)getApplication();
myApp.setName("赵信!");//修改后的名称
Intent intent = new Intent(MainActivity.this,ValuesActivity.class);
startActivity(intent);
}
});
}
}
定义一个Application类 MyApp
package com.example.app.androidvaluespass; import android.app.Application; /**
* Created by honghong on 2016/6/18.
*/
public class MyApp extends Application { public String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public void onCreate() {
super.onCreate();
setName("张三");
}
}
定义一个接收值的类
package com.example.app.androidvaluespass; import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; /**
* Created by honghong on 2016/6/18.
*/
public class ValuesActivity extends Activity { private MyApp myApp;
private TextView editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.values);
editText = (TextView)this.findViewById(R.id.Msg);
myApp = (MyApp)getApplication();
editText.setText(myApp.getName());
}
}
最后要在AndroidManifest.xml 文件中配置一下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app.androidvaluespass"> <application
android:name=".MyApp" // ← 继承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=".ValuesActivity"></activity> // 这个类调用Application类ValuesActivity
</application> </manifest>
Application值传递。的更多相关文章
- WP8:Unity3D之间的值传递
在前面的讨论中,我们介绍了如何在Unity3D for WP8中使用高于.Net 3.5的第三方库,传送门:http://www.cnblogs.com/zhxilin/p/3311240.html ...
- [SpringMVC-值传递] 初始SpringMVC--SpringMVC中的值传递
把页面中输入的值传递到后台以及后台向前台传递,有以下几种方式 这里以登录为例子,实现打印前端页面的值 1,新建一个控制器,根据不同的请求地址实现不同的请求方式 LoginController.java ...
- Android笔记(二十) Activity中的跳转和值传递
我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...
- Java 为值传递而不是引用传递
——reference Java is Pass by Value and Not Pass by Reference 其实这个问题是一个非常初级的问题,相关的概念初学者早已掌握,但是时间长了还是容易 ...
- python 引用传递与值传递
https://taizilongxu.gitbooks.io/stackoverflow-about-python/content/16/README.html 1.也就是如果传可变对象,就是引用传 ...
- java的值传递笔记
1. 背景:开发小伙伴突然问我java是值传递还是引用传递,我说当然是值传递,只不过有时候传递一个对象时实际传递的是对象的地址值,所以让人容易产生一种引用传递的假象,貌似在李刚的疯狂java讲义有提到 ...
- Java 中的值传递和参数传递
Java中没有指针,所以也没有引用传递了,仅仅有值传递不过可以通过对象的方式来实现引用传递 类似java没有多继承 但可以用多次implements 接口实现多继承的功能 值传递:方法调用时,实际参数 ...
- java是值传递还是引用传递
首先写一个简便的Employee,以便测试使用. class Employee { private String name; public Employee(String name) { this.n ...
- Java中值传递和引用传递的概念
很多书中都提到了在Java中只存在值传递,但是今天在一个NanoHTTPD的源码中看到这样一段: if (qmi >= 0) { decodeParms(uri.substring(qmi + ...
随机推荐
- oracle安装报错检查操作系统版本: 必须是5.1 or 5.2。实际为 6.1未通过
oracle安装时报错,提示:操作系统版本: 必须是5.1 or 5.2.实际为 6.1未通过 , 解决方案 这里只认证5.1.5.2的OS版本,但是我的win server 2008系统版本为6.1 ...
- NET Core开发-获取所有注入(DI)服务
NET Core开发-获取所有注入(DI)服务 获取ASP.NET Core中所有注入(DI)服务,在ASP.NET Core中加入了Dependency Injection依赖注入. 我们在Cont ...
- information_schema.columns 学习
每一个表中的每一列都会在information_schema.columns表中对应一行 1.informaiton_schema.columns 常用列: 1.table_catalog :不管是t ...
- iOS开发之OC篇-响应式编程Reactive Cocoa
一.Reactive Cocoa 介绍 Reactive Cocoa 是 iOS 开发的一个 "重量级" 框架 高大上的概念:响应式编程 核心概念:信号 Signal 官方网站:h ...
- Asp.net web服务处理程序(第六篇)
四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...
- C# ADO基础 SqlHelper
class SqlHelper { //这个是将连接数据库的字符串写到配置文件中的 private static string connStr = ConfigurationManager.Conne ...
- 转:ReportViewer控件使用方法
a. ReportViewer关联Report1.rdlc的简单呈现b. 对带有报表参数的Report1.rdlc的呈现c. 利用程式生成的DataSet 填充报表d. 调用存储过程 生成DataSe ...
- HTML5学习摘录
设计原理 不是规范里都包含什么,而是规范里为什么会包含它们,以及在设计这个规范的时候,设计者们是怎么看待这些东西的. 发展史:HTML2.0——>HTML3.2——>HTML4.0.1—— ...
- Activiti 5.16用户手册
From :http://www.mossle.com/docs/activiti/ Table of Contents 1. 简介 协议 下载 源码 必要的软件 JDK 6+ Eclipse Ind ...
- What is NicEdit?
NicEdit - WYSIWYG Content Editor, Inline Rich Text Application What is NicEdit? NicEdit is a Light ...