1、layout下面的布局

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context="com.example.app.androidvaluespass.MainActivity">
  11.  
  12. <Button
  13. android:id="@+id/btnSubmit"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="值传递"/>
  17.  
  18. </RelativeLayout>

values.xml用于接收值

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <EditText
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/Msg"/>
  10.  
  11. </LinearLayout>

2、类及启动项

MainActivity

  1. package com.example.app.androidvaluespass;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends Activity {
  10.  
  11. private Button btn;
  12. private MyApp myApp;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. btn = (Button)this.findViewById(R.id.btnSubmit);
  18. btn.setOnClickListener(new View.OnClickListener() {
  19. @Override
  20. public void onClick(View view) {
  21. myApp = (MyApp)getApplication();
  22. myApp.setName("赵信!");//修改后的名称
  23. Intent intent = new Intent(MainActivity.this,ValuesActivity.class);
  24. startActivity(intent);
  25. }
  26. });
  27. }
  28. }

定义一个Application类 MyApp

  1. package com.example.app.androidvaluespass;
  2.  
  3. import android.app.Application;
  4.  
  5. /**
  6. * Created by honghong on 2016/6/18.
  7. */
  8. public class MyApp extends Application {
  9.  
  10. public String name;
  11.  
  12. public String getName() {
  13. return name;
  14. }
  15.  
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19.  
  20. @Override
  21. public void onCreate() {
  22. super.onCreate();
  23. setName("张三");
  24. }
  25. }

定义一个接收值的类

  1. package com.example.app.androidvaluespass;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6.  
  7. /**
  8. * Created by honghong on 2016/6/18.
  9. */
  10. public class ValuesActivity extends Activity {
  11.  
  12. private MyApp myApp;
  13. private TextView editText;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.values);
  18. editText = (TextView)this.findViewById(R.id.Msg);
  19. myApp = (MyApp)getApplication();
  20. editText.setText(myApp.getName());
  21. }
  22. }

最后要在AndroidManifest.xml 文件中配置一下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.app.androidvaluespass">
  4.  
  5. <application
  6. android:name=".MyApp" // ← 继承Application类的名称,这个一定要对应。
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity">
  13. <intent-filter>
  14. <action android:name="android.intent.action.MAIN" />
  15.  
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity android:name=".ValuesActivity"></activity> // 这个类调用Application类ValuesActivity
  20. </application>
  21.  
  22. </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. mysql之主从复制

    原理--> 在数据库层面,复制语句或者行,因为在数据库层面,故只有主服务器生成,并放到二进制日志里面,才能复制给从服务器. 原理--> mysql的主从复制基于异步,主要有三个进程执行,分 ...

  2. 网关协议学习:CGI、FastCGI、WSGI、uWSGI

    一直对这四者的概念和区别很模糊,现在就特意梳理一下它们的关系与区别. CGI CGI即通用网关接口(Common Gateway Interface),是外部应用程序(CGI程序)与Web服务器之间的 ...

  3. centos 6.5 安装 redis

    下载软件: wget wget http://download.redis.io/releases/redis-2.8.7.tar.gz 2.解压软件并编译安装: tar -zxvf redis-2. ...

  4. 是否需要手动执行DataContext的Dispose方法?

    我们知道DataContext实现了IDisposable接口.在C#中,凡是实现了IDisposable接口的类,都推荐的使用using语句.如下: using (DataContext db = ...

  5. mvc 解决StyleBundle中 图片绝对路径 装换成相对路径的问题 CssRewriteUrlTransform

    问题 解决办法

  6. Unique Binary Search Trees 解答

    Question Given n, how many structurally unique BST's (binary search trees) that store values 1...n? ...

  7. 给Visual Studio更替皮肤和背景图

    给Visual Studio更换皮肤和背景图 1.先安装更换皮肤的插件  VS菜单栏里面找到:工具>扩展和更新>联机>搜索: Theme Editor   下载并安装: 安装后先不着 ...

  8. 第一个Spring MVC程序

    最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...

  9. iOS 字体设置

    使用无衬线字体 body {     font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; }  iOS 4 ...

  10. LDAP启动cacao提示Invalid file permission

    问题处理步骤: 1.LDAP实例停止 2.DSCC控制台启动,提示cacao已停止…… 3.启动caocaoroot@rusky bin]# ./cacaoadm startInvalid file ...