Android使用静态变量传递数据
原理:就是在接收端的Avtivity里面设置static的变量,在发送端这边改变静态变量的值,然后启动意图。
效果截图:
步骤一:activity_main.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/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="测试用静态变量static传递数据" /></RelativeLayout>
步骤二:activity_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>
步骤三:OtherActivity.java ,创建静态变量 pubic static stringname , public static int age
package com.android.myapp;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class OtherActivity extends Activity {private TextView textView ;//在接收端的Activity(OtherActivity.java)里面设置 Static 变量, 在发送端(MainActivity.java)改变静态变量的值public static String name ;public static int age ;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_other);textView = (TextView)findViewById(R.id.msg);textView.setText("Name-->>"+name+"\n"+"Age-->>"+age);}}
步骤四:MainActivity.java 在发送端(MainActivity.java)改变接收端OtherActivity.java中静态变量的值
package com.android.myapp;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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub //在发送端(MainActivity.java)修改接收端(OhterActivity.java)中静态变量的值 Intent intent = new Intent(); intent.setClass(MainActivity.this,OtherActivity.class); OtherActivity.name = "Nicole"; OtherActivity.age = 25; 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; } }
步骤五:在AndroidManifest.xml 中注册OtherActivity.java
在<application>.......</application> 内,加上
<activity android:name=".OtherActivity"></activity>
--------------------------------------------------------------------------------------------------------------
总结:
1. OtherActvity.java 中的 publicstatic 变量的值 可在其他Activity(eg:MainActivity.java)中改,引用方法:OtherActivity.name= "Nicole" .
2. 而在OtherActivity.java中引用时,直接 用 name = "Nicole" , 不要用this.name = "Nicole" . eg : 在OtherActivity.java 的onCreat()方法中,可设置OtherActivity的 name 为“jack”, 代码为 name="jack" , 不写 this.name = "jack", 因为 name 是 OtherActivity.java 的 一个static 变量,即静态变量。
更多相关文章
- Nginx系列教程(四)| 一文带你读懂Nginx的动静分离
- android编译时如何决定一个APK等Module是否被自动编译到system.i
- 深入理解AsyncChannel、Messenger
- 【Android(安卓)Studio安装部署系列】三十六、Android(安卓)stud
- Android(安卓)SDK下载配置教程
- Cocos2d-x 发布 Android
- Android中调用.so库操作步骤
- android sdk 编译相关
- AndroidStudio快捷键整理--2