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 + ...
随机推荐
- mysql之主从复制
原理--> 在数据库层面,复制语句或者行,因为在数据库层面,故只有主服务器生成,并放到二进制日志里面,才能复制给从服务器. 原理--> mysql的主从复制基于异步,主要有三个进程执行,分 ...
- 网关协议学习:CGI、FastCGI、WSGI、uWSGI
一直对这四者的概念和区别很模糊,现在就特意梳理一下它们的关系与区别. CGI CGI即通用网关接口(Common Gateway Interface),是外部应用程序(CGI程序)与Web服务器之间的 ...
- centos 6.5 安装 redis
下载软件: wget wget http://download.redis.io/releases/redis-2.8.7.tar.gz 2.解压软件并编译安装: tar -zxvf redis-2. ...
- 是否需要手动执行DataContext的Dispose方法?
我们知道DataContext实现了IDisposable接口.在C#中,凡是实现了IDisposable接口的类,都推荐的使用using语句.如下: using (DataContext db = ...
- mvc 解决StyleBundle中 图片绝对路径 装换成相对路径的问题 CssRewriteUrlTransform
问题 解决办法
- Unique Binary Search Trees 解答
Question Given n, how many structurally unique BST's (binary search trees) that store values 1...n? ...
- 给Visual Studio更替皮肤和背景图
给Visual Studio更换皮肤和背景图 1.先安装更换皮肤的插件 VS菜单栏里面找到:工具>扩展和更新>联机>搜索: Theme Editor 下载并安装: 安装后先不着 ...
- 第一个Spring MVC程序
最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...
- iOS 字体设置
使用无衬线字体 body { font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif; } iOS 4 ...
- LDAP启动cacao提示Invalid file permission
问题处理步骤: 1.LDAP实例停止 2.DSCC控制台启动,提示cacao已停止…… 3.启动caocaoroot@rusky bin]# ./cacaoadm startInvalid file ...