转:Android设置全局变量 - itwolf

时间:2024-03-08 22:51:37

转:Android设置全局变量

2015-07-31 09:32  itwolf  阅读(2724)  评论(0编辑  收藏  举报

声明:本文转自feiyangxiaomi的博客:http://blog.csdn.net/feiyangxiaomi/article/details/9966215仅供学习使用,转载请指明原作者。

文中介绍了两种在Android中使用全局变量的方法,其中第一中是android推荐的,但是只能用于继承Activity类使用,第二种是java中设置静态变量和方法适用于所有类。

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

1.1定义Data类继承Application Data.class

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

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

1.3创建两个Activity

MainActivity.class

 
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

 
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] view plaincopy
 
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] view plaincopy
 
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] view plaincopy
 
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……  循环