[转]android使用全局变量的两种方法

时间:2022-08-20 06:15:48

本文转自:http://blog.csdn.net/feiyangxiaomi/article/details/9966215

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

1.使用application来保存全局变量

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

1.1定义Data类继承Application Data.class

  1. import android.app.Application;
  2. publicclass Data extends Application{
  3. private String b;
  4. public String getB(){
  5. returnthis.b;
  6. }
  7. publicvoid setB(String c){
  8. this.b= c;
  9. }
  10. @Override
  11. publicvoid onCreate(){
  12. b = "hello";
  13. super.onCreate();
  14. }
  15. }
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

  1. android:name="com.animator1.Data" >
android:name="com.animator1.Data" >

1.3创建两个Activity

MainActivity.class

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

  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. @Override
  12. publicvoid onClick(View v) {
  13. // TODO Auto-generated method stub
  14. app.setB("hello");
  15. txtview.setText(app.getB());
  16. Intent intent = new Intent(secondActivity.this, MainActivity.class);
  17. startActivity(intent);
  18. }
  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运行程序 [转]android使用全局变量的两种方法   [转]android使用全局变量的两种方法

说明:程序首先进入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

  1. publicclass Data{
  2. privatestatic String a ="feiyangxiaomi";
  3. publicstatic String getA() {
  4. return a;
  5. }
  6. publicstaticvoid setA(String a) {
  7. Data.a = a;
  8. }
  9. }
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

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

  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. @Override
  11. publicvoid onClick(View v) {
  12. // TODO Auto-generated method stub
  13. Data.setA("feiyangxiaomi");
  14. Intent intent = new Intent(secondActivity.this, MainActivity.class);
  15. startActivity(intent);
  16. }
  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运行程序 [转]android使用全局变量的两种方法                     [转]android使用全局变量的两种方法

说明:程序首先进入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专门提供的应用入口,并且运行周期贯穿整个程序运行。