• 5、android使用意图传递数据之全局变量传递


    实例:

    1、layout的代码

      activity_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>

      activity_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="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />

    </LinearLayout>



    2、src下的代码

      MainActivity.java

    public class MainActivity extends Activity {
    private Button button;
    private MyApplication myApplication;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button=(Button) this.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    myApplication=(MyApplication)getApplication();
    myApplication.setName("李四");
    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;
    }

    }

      OtherActivity.java

    public class OtherActivity extends Activity {
    private MyApplication myApplication;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_other);
    textView=(TextView) this.findViewById(R.id.msg);
    myApplication=(MyApplication) getApplication();
    textView.setText("myApplication--->"+myApplication.getName());
    }
    }

      MyApplication.java

    public class MyApplication extends Application{
    private String name;

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }
    @Override
    public void onCreate() {
    super.onCreate();
    this.setName("张三");
    }
    }



    3、AndroidManifest.xml

      

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.jc.application"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

    <application
    android:name="com.jc.application.MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
    android:name="com.jc.application.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="com.jc.application.OtherActivity">

    </activity>
    </application>

    </manifest>

  • 相关阅读:
    九个PHP有用功能
    ajax的上拉功能
    jquery 插件
    任务08(全体)——第一次半月学习总结
    任务02——安装 Intellj IDEA,编写一个简易四则运算小程序,并将代码提交到 GitHub
    任务01——谈谈对参与工作室的预期
    任务08
    任务03
    任务02
    任务01
  • 原文地址:https://www.cnblogs.com/RocketMan/p/5328910.html
Copyright © 2020-2023  润新知