博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]android使用全局变量的两种方法
阅读量:6513 次
发布时间:2019-06-24

本文共 6874 字,大约阅读时间需要 22 分钟。

本文转自:

在我们使用android编写程序的时候,少不了想利用全局变量,但是面向对象语言和过程语言区别很大,不再是include就可以的。这里我写了使用全局变量的两种方法:

 

1.使用application来保存全局变                                                             

这里没有太多理论性的东西,无非就是一些实际操作。

1.1定义Data类继承Application Data.class

[java]
  1. import android.app.Application; 
  2.  
  3. publicclass Data extends Application{ 
  4.     private String b; 
  5.      
  6.     public String getB(){ 
  7.         returnthis.b; 
  8.     } 
  9.     publicvoid setB(String c){ 
  10.         this.b= c; 
  11.     } 
  12.     @Override 
  13.     publicvoid onCreate(){ 
  14.         b = "hello"
  15.         super.onCreate(); 
  16.     } 
import android.app.Application;public class Data extends Application{	private String b;		public String getB(){		return this.b;	}	public void setB(String c){		this.b= c;	}	@Override	public void onCreate(){		b = "hello";		super.onCreate();	}}

1.2在manifest.xml中声明application

[java]
  1. android:name="com.animator1.Data"
android:name="com.animator1.Data" >

1.3创建两个Activity

MainActivity.class

[java]
  1. publicclass MainActivity extends Activity { 
  2.  
  3.     @Override 
  4.     protectedvoid onCreate(Bundle savedInstanceState) { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.main); 
  7.         Button btn = (Button) this.findViewById(R.id.button1); 
  8.         final TextView txtview = (TextView) this.findViewById(R.id.main); 
  9.         final Data app = (Data)getApplication(); 
  10.         txtview.setText(app.getB()); 
  11.         btn.setOnClickListener(new View.OnClickListener() { 
  12.              
  13.             @Override 
  14.             publicvoid onClick(View v) { 
  15.                 // TODO Auto-generated method stub 
  16.                 app.setB("no_hello"); 
  17.                 txtview.setText(app.getB()); 
  18.                 Intent intent = new Intent(MainActivity.this, secondActivity.class); 
  19.                 startActivity(intent); 
  20.             } 
  21.         }); 
  22.     } 
public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		Button btn = (Button) this.findViewById(R.id.button1);		final TextView txtview = (TextView) this.findViewById(R.id.main);		final Data app = (Data)getApplication();		txtview.setText(app.getB());		btn.setOnClickListener(new View.OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				app.setB("no_hello");				txtview.setText(app.getB());				Intent intent = new Intent(MainActivity.this, secondActivity.class);				startActivity(intent);			}		});	}}

secondActivity.class

[java]
  1. publicclass secondActivity extends Activity{ 
  2.     @Override 
  3.     protectedvoid onCreate(Bundle savedInstanceState){ 
  4.         super.onCreate(savedInstanceState); 
  5.         setContentView(R.layout.second); 
  6.         Button btn = (Button) this.findViewById(R.id.button2); 
  7.         final TextView txtview = (TextView) this.findViewById(R.id.second); 
  8.         final Data app = (Data)getApplication(); 
  9.         txtview.setText(app.getB()); 
  10.         btn.setOnClickListener(new View.OnClickListener() { 
  11.              
  12.             @Override 
  13.             publicvoid onClick(View v) { 
  14.                 // TODO Auto-generated method stub               
  15.                 app.setB("hello"); 
  16.                 txtview.setText(app.getB()); 
  17.                 Intent intent = new Intent(secondActivity.this, MainActivity.class); 
  18.                 startActivity(intent); 
  19.             } 
  20.         }); 
  21.     } 
public class secondActivity extends Activity{	@Override	protected void onCreate(Bundle savedInstanceState){		super.onCreate(savedInstanceState);		setContentView(R.layout.second);		Button btn = (Button) this.findViewById(R.id.button2);		final TextView txtview = (TextView) this.findViewById(R.id.second);		final Data app = (Data)getApplication();		txtview.setText(app.getB());		btn.setOnClickListener(new View.OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub								app.setB("hello");				txtview.setText(app.getB());				Intent intent = new Intent(secondActivity.this, MainActivity.class);				startActivity(intent);			}		});	}}

1.4运行程序   

说明:程序首先进入MainActivity.class中,获取Data中变量b的值,显示图1所示的界面hello,单击button后app.setB("no_hello"), 修改变量b的值。

           跳转到secondActivity.class,获取Data中变量b的值,显示图2所示的界面no_hello,单击button后app.setB("hello"), 修改变量b的值。

           跳转回MainActivity.class……  循环

2.使用普通的类Data.class来保存全局变                                                             

方法和上面差不多。

1.1 定义Data.class

[java]
  1. publicclass Data{ 
  2.     privatestatic String a ="feiyangxiaomi"
  3.      
  4.     publicstatic String getA() { 
  5.         return a; 
  6.     } 
  7.      
  8.     publicstaticvoid setA(String a) { 
  9.         Data.a = a; 
  10.     } 
public class Data{	private static String a ="feiyangxiaomi";		public static String getA() {		return a;	}		public static void setA(String a) {		Data.a = a;	}}

1.2创建两个Activity

MainActivity.class

[java]
  1. publicclass MainActivity extends Activity { 
  2.  
  3.     @Override 
  4.     protectedvoid onCreate(Bundle savedInstanceState) { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.main); 
  7.         Button btn = (Button) this.findViewById(R.id.button1); 
  8.         final TextView txtview = (TextView) this.findViewById(R.id.main); 
  9.         txtview.setText(Data.getA()); 
  10.         btn.setOnClickListener(new View.OnClickListener() { 
  11.              
  12.             @Override 
  13.             publicvoid onClick(View v) { 
  14.                 // TODO Auto-generated method stub 
  15.                 Data.setA("no_feiyangxiaomi"); 
  16.                 Intent intent = new Intent(MainActivity.this, secondActivity.class); 
  17.                 startActivity(intent); 
  18.             } 
  19.         }); 
  20.     } 
public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		Button btn = (Button) this.findViewById(R.id.button1);		final TextView txtview = (TextView) this.findViewById(R.id.main);		txtview.setText(Data.getA());		btn.setOnClickListener(new View.OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub				Data.setA("no_feiyangxiaomi");				Intent intent = new Intent(MainActivity.this, secondActivity.class);				startActivity(intent);			}		});	}}

secondActivity.class

[java]
  1. publicclass secondActivity extends Activity{ 
  2.     @Override 
  3.     protectedvoid onCreate(Bundle savedInstanceState){ 
  4.         super.onCreate(savedInstanceState); 
  5.         setContentView(R.layout.second); 
  6.         Button btn = (Button) this.findViewById(R.id.button2); 
  7.         final TextView txtview = (TextView) this.findViewById(R.id.second); 
  8.         txtview.setText(Data.getA()); 
  9.         btn.setOnClickListener(new View.OnClickListener() { 
  10.              
  11.             @Override 
  12.             publicvoid onClick(View v) { 
  13.                 // TODO Auto-generated method stub               
  14.                 Data.setA("feiyangxiaomi"); 
  15.                 Intent intent = new Intent(secondActivity.this, MainActivity.class); 
  16.                 startActivity(intent); 
  17.             } 
  18.         }); 
  19.     } 
public class secondActivity extends Activity{	@Override	protected void onCreate(Bundle savedInstanceState){		super.onCreate(savedInstanceState);		setContentView(R.layout.second);		Button btn = (Button) this.findViewById(R.id.button2);		final TextView txtview = (TextView) this.findViewById(R.id.second);		txtview.setText(Data.getA());		btn.setOnClickListener(new View.OnClickListener() {						@Override			public void onClick(View v) {				// TODO Auto-generated method stub								Data.setA("feiyangxiaomi");				Intent intent = new Intent(secondActivity.this, MainActivity.class);				startActivity(intent);			}		});	}}

1.3运行程序                     

说明:程序首先进入MainActivity.class中,获取Data中变量b的值,显示图1所示的界面feiyangxiaomi,单击button后app.setB("no_feiyangxiaomi"), 修改变量b的值。

           跳转到secondActivity.class,获取Data中变量b的值,显示图2所示的界面no_feiyangxiaomi,单击button后app.setB("feiyangxiaomi"), 修改变量b的值。

           跳转回MainActivity.class……  循环

3.总结                                                                                       

    两种方法我测试的都可以的,大多数人应该更会偏向于第一种,毕竟Application是android专门提供的应用入口,并且运行周期贯穿整个程序运行。

 

 

你可能感兴趣的文章
数据库设计 Step by Step (6) —— 提取业务规则
查看>>
Redis客户端redisson实战
查看>>
连接到 JasperReports Server
查看>>
java处理高并发高负载类网站问题
查看>>
使用C#生成随机密码(纯数字或字母)和随机卡号(数字与字母组合)
查看>>
CAS服务器端集群
查看>>
设计模式 之 访问者模式
查看>>
JAVA Collections框架
查看>>
更改Windwos server 2003 域用户密码策略默认配置
查看>>
进制转换
查看>>
html与html5的一些区别
查看>>
ASCII码
查看>>
java常用四种排序源代码
查看>>
win7 下硬盘安装Redhat7
查看>>
Redis 分布式锁的正确实现方式
查看>>
mysqldump 备份命令使用中的一些经验总结
查看>>
Linux下MySql安装配置方法总结
查看>>
本IT博客用于域名投资、互联网、资源下载等相关干货收藏和学习
查看>>
ArrayList底层实现
查看>>
【转载】Java程序设计入门 (二)
查看>>