Android通过全局变量传递数据

来源:互联网 发布:手机淘宝不能分期付款 编辑:程序博客网 时间:2024/05/17 03:35

在Activity之间数据传递中还有一种比较实用的方式,就是全局对象,实用J2EE的读者来说都知道Java Web的四个作用域,这四个作用域从小到大分别是Page、Request、Session和Application,其中Application域在应用程序的任何地方都可以使用和访问,除非是Web服务器停止,Android中的全局对象非常类似于Java Web中的Application域,除非是Android应用程序清除内存,否则全局对象将一直可以访问。

实例:

1、  在layout文件夹下新建other.xml并且修改main.xml文件。如下:

在main.xml【Graphical Layout】界面上拖动Button按钮。并修改程序代码:

<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=".Main" >    <Button        android:id = "@+id/button"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/Application_global" /></RelativeLayout>

在other.xml【Graphical Layout】界面上拖动TextView文本显示组件。并修改程序代码:

other.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/msg"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/TextView_other" /></LinearLayout>

在res->values下的strings.xml文件中添加代码,防止出现警告:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">MyApp</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string><string name="Application_global">使用Application传递数据</string><string name="TextView_other">TextView</string></resources>

2、新建并修改MyApp.java、OtherActivity.java、Main.java文件。

MyApp.java

package com.Android.myapp;import android.app.Application;//声明类MyApp,继承自Applicationpublic class MyApp extends Application {public String name;public String getName() {return name;}public void setName(String name) {this.name = name;}//当应用程序启动时,方法onCreate被调用@Overridepublic void onCreate() { // TODO Auto-generated method stubsuper.onCreate();setName("盛大游戏");}}

OtherActivity.java

package com.Android.myapp;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;//声明类OtherActivity,继承自Activitypublic class OtherActivity extends Activity {private MyApp myapp;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.other);textView = (TextView)this.findViewById(R.id.msg);myapp = (MyApp)getApplication();textView.setText("游戏制作商->"+myapp.getName());}}

Main.java

package com.Android.myapp;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.app.Activity;import android.content.Intent;public class Main extends Activity {private Button button; //添加Button对象private MyApp myapp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button)this.findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmyapp = (MyApp)getApplication();myapp.setName("腾讯游戏"); //修改名称//声明一个意图Intent intent = new Intent(Main.this,OtherActivity.class);//启动意图startActivity(intent);}});}}

3、修改Androidmanifest.xml文件,红色加粗为修改的部分。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.Android.myapp"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:name=".MyApp"        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.Android.myapp.Main"            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 = ".OtherActivity"></activity>    </application></manifest>

注:如果还有不明之处,请搜索老罗视频教程《android使用意图传递数据之全局变量传递》。

显示结果:

0 0
原创粉丝点击