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值传递。的更多相关文章

  1. WP8:Unity3D之间的值传递

    在前面的讨论中,我们介绍了如何在Unity3D for WP8中使用高于.Net 3.5的第三方库,传送门:http://www.cnblogs.com/zhxilin/p/3311240.html ...

  2. [SpringMVC-值传递] 初始SpringMVC--SpringMVC中的值传递

    把页面中输入的值传递到后台以及后台向前台传递,有以下几种方式 这里以登录为例子,实现打印前端页面的值 1,新建一个控制器,根据不同的请求地址实现不同的请求方式 LoginController.java ...

  3. Android笔记(二十) Activity中的跳转和值传递

    我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...

  4. Java 为值传递而不是引用传递

    ——reference Java is Pass by Value and Not Pass by Reference 其实这个问题是一个非常初级的问题,相关的概念初学者早已掌握,但是时间长了还是容易 ...

  5. python 引用传递与值传递

    https://taizilongxu.gitbooks.io/stackoverflow-about-python/content/16/README.html 1.也就是如果传可变对象,就是引用传 ...

  6. java的值传递笔记

    1. 背景:开发小伙伴突然问我java是值传递还是引用传递,我说当然是值传递,只不过有时候传递一个对象时实际传递的是对象的地址值,所以让人容易产生一种引用传递的假象,貌似在李刚的疯狂java讲义有提到 ...

  7. Java 中的值传递和参数传递

    Java中没有指针,所以也没有引用传递了,仅仅有值传递不过可以通过对象的方式来实现引用传递 类似java没有多继承 但可以用多次implements 接口实现多继承的功能 值传递:方法调用时,实际参数 ...

  8. java是值传递还是引用传递

    首先写一个简便的Employee,以便测试使用. class Employee { private String name; public Employee(String name) { this.n ...

  9. Java中值传递和引用传递的概念

    很多书中都提到了在Java中只存在值传递,但是今天在一个NanoHTTPD的源码中看到这样一段: if (qmi >= 0) { decodeParms(uri.substring(qmi + ...

随机推荐

  1. Lammp安装过程

    -1     建设环境 创建mysql数据库的lvm环境                创建lv逻辑卷                新安装一个硬盘25G sdb                #fd ...

  2. 使用DBUtils小框架

    DBUtils对sqlserver好像支持不怎么好,经常出现问题 比如 三月 14, 2015 10:19:32 上午 com.mchange.v2.log.MLog 信息: MLog clients ...

  3. information_schema.engines学习

    当前mysql实例的存储引擎信息可以从information_schema.engines 中查询到 例子: mysql> select * from information_schema.en ...

  4. python操作redis-为元素排序

    #!/usr/bin/python #!coding:utf-8 import time import redis if __name__ == "__main__": try: ...

  5. 常用两种数据交换格式之XML和JSON的比较

    目前,在web开发领域,主要的数据交换格式有XML和JSON,对于XML相信每一个web developer都不会感到陌生: 相比之下,JSON可能对于一些新步入开发领域的新手会感到有些陌生,也可能你 ...

  6. 前端MVVM学习之KnockOut(二)

    现在开始学习Knockout并且做个简单的例子. Knockout是建立在以下三个核心功能之上的: 1.Observables and dependency tracking(属性监控与依赖跟踪) 2 ...

  7. C# System.Uri类_获取Url的各种属性_文件名_参数_域名_端口等等

    System.Uri类用于处理Uri地址信息,常用到它的地方有,相对Uri地址转绝对Uri地址,获取Uri的某部分信息等等,可以说是一个非常有用的类. 一.属性 AbsolutePath 获取 URI ...

  8. 报LinkageError的原因

    LinkageError是一个比较棘手的异常,准确的说它是一个Error而不是Exception.java api对它没有直接的解释,而是介绍了它的子类: Subclasses of LinkageE ...

  9. Linux系统编程(3)——文件与IO之fcntl函数

    linux文件I/O用:open.read.write.lseek以及close函数实现了文件的打开.读写等基本操作.fcntl函数可以根据文件描述词来操作文件. 用法: int fcntl(int ...

  10. apache http配置https

    <一,Lamp系统搭建> yum install httpd httpd-devel mysql mysql-server mysql-devel php php-mysql php-co ...