zoukankan      html  css  js  c++  java
  • Intent传值之通过Application传值

    传值第五种方式:
     *     程序的全局变量application
     *     特点:1.一个程序application对象只能有一个 
     *         2.application对象在程序启动时就创建 
     *         3.通常用来存放全局变量

    xml布局文件

    <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=".MainActivity" >
    
        <Button 
            android:id="@+id/btn01"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="跳转到第二个页面"/>
    
    </RelativeLayout>

    myApplication.java

    package com.example.day05_application_passvalue;
    
    import android.app.Application;
    import android.util.Log;
    /**
     * 
     * 
     * @author my
     *
     */
    //注意:需要在AndroidManifest.xml清单文件中注册自定义application
    public class myApplication extends Application{
    
        private String TAG = "MainActivity";
        private String text = "呵呵呵呵";  
        public String getTAG() {
            return TAG;
        }
        public void setTAG(String tAG) {
            TAG = tAG;
        }
        public String getText() {
            return text;
        }
        public void setText(String text) {
            this.text = text;
        }
        //重写onCreate()方法,application创建的时候调用
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i(TAG, "MainActivity-->myApplication");
        }
    }

    MainActivity.java

    package com.example.day05_application_passvalue;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Application;
    import android.content.Intent;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        private Button btn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn = (Button) findViewById(R.id.btn01);
            //获取Application对象
            myApplication  app = (myApplication)getApplication();
            //通过application对象获得值
            String text = app.getText();
            Log.i("MainActivity", text);
            //改变值
            app.setText("hahhah,值在Activity中被改变");
            /**
             * button点击事件,点击跳转到另一个页面
             */
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,Activity2.class);
                    startActivity(intent);
                }
            });
        }
    }

    Activity2.java

    package com.example.day05_application_passvalue;
    
    import android.app.Activity;
    import android.app.Application;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    
    public class Activity2 extends Activity{
        private TextView tv;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout1);
            tv = (TextView) findViewById(R.id.text01);
            Log.i("MainActivity", "MainActity-->Activity2");
            myApplication app = (myApplication) getApplication();
            tv.setText(app.getText());
        }
    
    }
  • 相关阅读:
    java Math类
    JAVA Date类与Calendar类【转】
    java Runtime类
    Java System类
    java 多线程
    java 包
    Java 内部类
    java 抽象类 以及模块方法设计模式,接口
    java 单例模式
    java 关于Java中静态代码块以及构造函数的执行先后顺序
  • 原文地址:https://www.cnblogs.com/fangg/p/5722947.html
Copyright © 2011-2022 走看看