android使用全域性變數的兩種方法
在我們使用android編寫程式的時候,少不了想利用全域性變數,但是面嚮物件語言和過程語言區別很大,不再是include就可以的。這裡我寫了使用全域性變數的兩種方法:
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
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
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
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專門提供的應用入口,並且執行週期貫穿整個程式執行。