zoukankan      html  css  js  c++  java
  • 使用Application传递数据

        Android里面的Application和javaweb里面的Application相似!在一个Activity中设置好值后,在其他Activity中可以取出来!

    一、创建一个工程:android_application

    二、改写main.xml文件

    <LinearLayout 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=".MainActivity" >
    
       <Button 
           android:id="@+id/button"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:text="使用Application传递数据"
           />
    
    </LinearLayout>

    三、新增一个other.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"
        android:orientation="vertical" >
        
        <TextView 
            android:id="@+id/msg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
    
    </LinearLayout>
    View Code

    四、MainActivity中代码

    package com.eoe.android_app;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
        
        private Button button;
        private MyApp myApp;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            button = (Button)this.findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    myApp = (MyApp)getApplication();
                    myApp.setName("Android show");
                    Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                    startActivity(intent);
                }
            });
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }
    View Code

    五、OtherActivity中代码

    package com.eoe.android_app;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class OtherActivity extends Activity {
        private MyApp myApp;
        private TextView textView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.other);
            textView = (TextView)this.findViewById(R.id.msg);
            myApp = (MyApp)getApplication();
            textView.setText("-->" + myApp.getName());
        }
    }
    View Code

    六、MyApp继承Application

    package com.eoe.android_app;
    
    import android.app.Application;
    
    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() {
            // TODO Auto-generated method stub
            super.onCreate();
            setName("android");
        }
    }
    View Code

    七、AndroidMainfest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.eoe.android_app"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
        <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.eoe.android_app.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=".OtherActivity"
                
                ></activity>
        </application>
    
    </manifest>
  • 相关阅读:
    (转)spring学习之@ModelAttribute运用详解
    (转)Spring3MVC 在JSP中使用@ModelAttribute
    (转)如何在maven的pom.xml中添加本地jar包
    (转)linux中项目部署和日志查看
    (转)Schema
    (转)xml
    (转)Dom4J解析
    判断一个请求是否为Ajax请求
    spring mvc中拦截器配置mvc:interceptors
    Freemarker自定义方法
  • 原文地址:https://www.cnblogs.com/kingshow123/p/application.html
Copyright © 2011-2022 走看看