Android开发之Intent的传值--Application
每当我们想要将输入的值传递到多个界面时,只是使用Intent传值的话,就会有一些的弊端。
下面我就以三个页面为例,进行简单的说明一下:
思路:
1.第一个页面是客户输入相关的信息。
2.将客户输入的信息的第一项(我这里设的是name),在第二个页面中进行显示。
3.在第二个页面中直接跳转到第三个页面中,显示客户输入的全部的信息。
首先,在工程中创建一个MyApplication类,类的创建如下:
package com.example.test;
import android.app.Application;
public class MyApplication extends Application{
private static MyApplication singleton;
private String id;
private String name;
private String age;
private String address;
private String email;
public static MyApplication getInstance(){
return singleton;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void onCreate(){
super.onCreate();
singleton = this;
}
}
上面创建一个用户的name,age,address,email信息。
在创建Application类时需要注意的是,这个类必须得在配置文件中进行配置才可以,要不然会抛出空异常错误。
具体实现的代码如下:
<application
android:allowBackup="true"
android:name="com.example.test.MyApplication" //特别注意
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.Activitytwo">
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
<activity
android:name="com.example.test.Activitythree">
<intent-filter >
<action android:name="android.intent.action.VIEW"/>
</intent-filter>
</activity>
</application>
接下来就是Activity中的代码的实现。在这里我就不写布局文件中的代码了,附上图,大家自己去写,也不是很难。
MainActivity的代码如下:
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private MyApplication myapp;
private EditText edtname;
private EditText edtage;
private EditText edtaddress;
private EditText edtemail;
private Button post;
private Button get;
private Button next;
private Button three;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myapp = MyApplication.getInstance();
edtname = (EditText)findViewById(R.id.name);
edtage = (EditText)findViewById(R.id.age);
edtaddress = (EditText)findViewById(R.id.place);
edtemail = (EditText)findViewById(R.id.email);
next =(Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
test();
Intent inten = new Intent(MainActivity.this,Activitytwo.class);
startActivity(inten);
}
});
public void test(){
myapp.setName(edtname.getText().toString());
myapp.setAge(edtage.getText().toString());
myapp.setAddress(edtaddress.getText().toString());
myapp.setEmail(edtemail.getText().toString());
}
}
Activitytwo代码实现:
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Activitytwo extends Activity{
private String showname;
private TextView show;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitytwo);
show = (TextView)findViewById(R.id.show);
send = (Button)findViewById(R.id.send);
showname = MyApplication.getInstance().getName();
show.setText(showname);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Activitytwo.this,Activitythree.class);
startActivity(intent);
finish();
}
});
}
}
Activitythree代码实现:
package com.example.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Activitythree extends Activity{
private TextView showname;
private TextView showage;
private TextView showplace;
private TextView showemail;
private Button finish;
private String name;
private String age;
private String place;
private String email;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activitythree);
showname = (TextView)findViewById(R.id.showname);
showage = (TextView)findViewById(R.id.showage);
showplace = (TextView)findViewById(R.id.showplace);
showemail = (TextView)findViewById(R.id.showemail);
finish = (Button)findViewById(R.id.finish);
name = MyApplication.getInstance().getName();
age = MyApplication.getInstance().getAge();
place = MyApplication.getInstance().getAddress();
email = MyApplication.getInstance().getEmail();
showname.setText(name);
showage.setText(age);
showplace.setText(place);
showemail.setText(email);
finish.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activitythree.this,MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
上述就是我的基本的代码的实现以及界面的设计,界面不美观只是为了实现功能而已。
Application类是实现多个Activity之间共享数据。
希望上面的说明能够让大家明白。
Android开发之Intent的传值--Application的更多相关文章
- Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面
现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...
- android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序
android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序 在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity ...
- Android开发之Intent略解
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...
- Android开发之Intent.Action
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的開始.比較经常使用. Input:nothing Out ...
- Android开发之Intent.Action 各种Action的常见作用
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...
- Android开发之Intent.Action Android中Intent的各种常见作用
1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始.比较常用. Input:nothing Outpu ...
- Android开发之旅: Intents和Intent Filters(理论部分)
引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...
- Android开发之旅5:应用程序基础及组件
引言 上篇Android开发之旅:应用程序基础及组件介绍了应用程序的基础知识及Android的四个组件,本篇将介绍如何激活组关闭组件等.本文的主题如下: 1.激活组件:意图(Intents) 1.1. ...
- Android开发之旅2:HelloWorld项目的目录结构
引言 前面Android开发之旅:环境搭建及HelloWorld,我们介绍了如何搭建Android开发环境及简单地建立一个HelloWorld项目,本篇将通过HelloWorld项目来介绍Androi ...
随机推荐
- 【HDOJ】4516 威威猫系列故事——因式分解
可解的算法太多了,采用的算法是试x的值.注意题目的输入x^3-2x^2不会写成x^3+-2x^2.一直RE在这儿. /* 4516 */ #include <iostream> #incl ...
- Android开发UI之控件-Android-PullToRefresh
下拉刷新,使用的是Android-PullToRefresh,Github--https://github.com/chrisbanes/Android-PullToRefresh PullToRef ...
- $destroy
ng-view 路由切换会触发 $destroy
- C# 线程更新UI
最方便的用法: private void ViewMsg(string msg) { /* control.Invoke(new SetControlTextDelegate((ct, ...
- C#替换双引号
context = context.Replace(@"""", "");
- LoadRunner调用Java程序—性能测试
为了充分利用LoadRunner的场景控制和分析器,帮助我们更好地控制脚本加载过程,从而展现更直观有效的场景分析图表.本次将重点讨论LoadRunner如何调用Java测试代码,完成压力测试. 通常我 ...
- 转自微软内部资料:编写高性能 Web 应用程序的 10 个技巧
编写高性能 Web 应用程序的 10 个技巧 转自微软资料数据层性能技巧 1 — 返回多个结果集技巧 2 — 分页的数据访问技巧 3 — 连接池技巧 4 — ASP.NET 缓存 API技巧 5 — ...
- HW输入字符串长度,字符串,计数m。从前往后计数,当数到m个元素时,m个元素出列,同时将该元素赋值给m,然后从下一个数计数循环,直到所有数字都出列,给定的数全部为大于0的数字。输出出队队列。
package huawei; import java.util.Scanner; public class 约瑟夫环 { private static class Node { public int ...
- 【Java基础】增强for循环要注意陷阱
什么是增强for循环 增强for循环是一种简单模式的for循环,为了方便数组和集合的遍历而存在. int[] arr = new int[]{1, 2, 3, 4, 5, 6}; for (int a ...
- HTTP 和 SOAP 标头 来传递用户名密码 验证webservice用户认证
支持自定义的 HTTP 和 SOAP 标头 注意:本主题中的内容适用于 Microsoft Office SharePoint Server 2007 SP1. 对于 Web 服务,您可以使用 HTT ...