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. re模块

    Python 的 re 模块(Regular Expression 正则表达式)提供各种正则表达式的匹配操作,和 Perl 脚本的正则表达式功能类似,使用这一内嵌于 Python 的语言工具,尽管不能 ...

  2. windows环境下搭建Cocos2d-X开发环境

    最近终于有时间可心搞搞自己的东西了,呵呵,那就开始做个手机小游戏给孩子玩吧. 首先必须选定开发的框架,移动终端开源的游戏框架貌似不多,找来找去也就这个了,名字简单Cocos2d-X,是Cocos2d国 ...

  3. 主要协议SCSI、FC、iSCSI

    一.SCSI SCSI是小型计算机系统接口(Small Computer System Interface)的简称,于1979首次提出,是为小型机研制的一种接口技术,现在已完全普及到了小型机,高低端服 ...

  4. arcgis api for silverlight

    原文 http://blog.sina.com.cn/s/blog_4638cf7b0100wntt.html arcgis api for silverlight(1) (2011-09-21 09 ...

  5. Java Socket 简单梳理

    Sockets let you send raw streams of bytes back and forth between two computers, giving you fairly lo ...

  6. mysql 只给更新表的某个字段的授权

    mysql> create view v_Procuct as select sn,name from Product; Query OK, 0 rows affected (0.01 sec) ...

  7. POJ1094 Sorting It All Out(拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30110   Accepted: 10 ...

  8. Zookeeper 3、Zookeeper工作原理(详细)

    1.Zookeeper的角色 » 领导者(leader),负责进行投票的发起和决议,更新系统状态 » 学习者(learner),包括跟随者(follower)和观察者(observer),follow ...

  9. UNIX下解压缩文件

    用法示例以Sun Solaris (其他unix如linux.aix大体相同)为例=========================================================== ...

  10. c#语句:分支

    六.分支:(一)if (表达式){}说明:1.表达式,就是用来返回bool形的表达式.2.if的小括号后面千万不要加分号 (二)if(表达式){}else{} 例:1.输入年龄,大于等于18显示成年, ...