• 使用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>
  • 相关阅读:
    常用256安全色
    PHP获取中英文混合字符串的字数
    windows 2012 443端口无法访问解决随记!
    SQL提取时间段内数据
    正则表达式匹配
    PclZip library能够压缩与解压缩Zip格式
    安装IIS或证书上服务提示安装程序无法复制文件 ftpsvc2.dl的解决办法
    tomcat
    版本控制gitlab
    rsync
  • 原文地址:https://www.cnblogs.com/kingshow123/p/application.html
Copyright © 2020-2023  润新知