使用Application传递数据

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

一、创建一个工程:android_application

二、改写main.xml文件

使用Application传递数据
<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>
使用Application传递数据

三、新增一个other.xml文件

使用Application传递数据
<?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中代码

使用Application传递数据
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中代码

使用Application传递数据
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

使用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

使用Application传递数据
<?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>
使用Application传递数据

使用Application传递数据

上一篇:PhotoShop 移动工具详解


下一篇:CSAPP lab1出现的小问题