前言
當程式越寫越大的時候你會發現,很多Activity都會用到相同的變數,當時剛接觸Android時間也沒很久,很直覺得就用Bundle把變數帶到下一個Activity,但這種做法會造成一些多餘的程式碼,因為不一定每個Activity都會用到,但你還是必須要寫Bundle。後來去Google一下發現有一個不錯的方法,就是把這類的變數儲存在Application當做共用變數(Global Variable)。
Global Variable
會需要用到Global Variable的情況很多,例如登錄的一些Information,這些相關的變數都能用這方法來實作,但在這邊提醒一下當User把應用程式關掉時Global Variable的相關資料也會被清除,所以可以搭配Shared Preferences或SQLite來使用,過一陣子會補上SQLite的教學。
1.建立共用變數類別
建立一個空的JAVA文件,名稱隨便(範例命名為GlobalVariable),此類別必須繼承Application,接著設定類別成員,也就是自己需要的共用變數名稱。例如建立一個User Name、Age的共用變數。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import android.app.Application; public class GlobalVariable extends Application { private String Name; //User 名稱 private int Age; //User 年紀 //修改 變數値 public void setName(String name){ this.Name = name; } public void setAge(int age){ this.Age = age; } //取得 變數值 public String getName() { return Name; } public int getAge(){ return Age; } } |
2. 設定AndroidManifest
在 AndroidManifest.xml 的 application 標籤底下新增一個屬性android:name,指定剛剛建立的類別名稱android:name=”.GlobalVariable”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<application android:name=".GlobalVariable" <!--新增--> android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> |
3. 儲存、取得共用變數
操作非常簡單,當你有需要時就建立GlobalVariable類別,進行變數值的操作(修改、取得),如此就不需要在每個Activity寫Bundle帶到下一個Activity。
1 2 3 4 5 6 7 8 9 10 |
//建立共用變數類別 GlobalVariable gv = (GlobalVariable)getApplicationContext(); // 修改 變數值 gv.setName("Devin"); gv.setAge(20); // 取得 變數值 String name = gv.getName(); int age = gv.getAge(); |
4. 實際演練
簡單實作一個範例提供大家學習用法,MainActivity(起始頁)提供輸入姓名、年紀 ,送出後儲存共用變數並切換到下一頁,Activity2(第二頁)取得共用變數並顯示出來。
請先完成上方1、2的說明再來操作實際演練。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" 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" android:orientation="vertical"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="姓名:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView4" android:layout_weight="1" android:textSize="18sp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:ems="10" android:id="@+id/etName" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="年紀:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_weight="1" android:textSize="18sp" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:id="@+id/etAge" android:layout_weight="1" android:inputType="number" /> </LinearLayout> <Button android:text="送出" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnOK" android:textSize="18sp" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnOK = (Button)findViewById(R.id.btnOK); btnOK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText etName = (EditText)findViewById(R.id.etName); EditText etAge = (EditText)findViewById(R.id.etAge); GlobalVariable gv = (GlobalVariable)getApplicationContext(); gv.setName(etName.getText().toString().trim()); gv.setAge(Integer.parseInt(etAge.getText().toString().trim())); Intent intent = new Intent(MainActivity.this,Activity2.class); startActivity(intent); } }); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_2" 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"> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="17dp" android:layout_marginStart="17dp" android:layout_marginTop="19dp" android:id="@+id/textView" android:textSize="24sp" /> </RelativeLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class Activity2 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); GlobalVariable gv = (GlobalVariable)getApplicationContext(); TextView tv = (TextView)findViewById(R.id.textView); tv.setText("我是:"+gv.getName()+" 我目前"+gv.getAge()+"歲"); } } |
Exp Up
ListView教學 從4到5– 開發Android 一定會用到ListView
想問這個和STATIC 分別?
STATIC是JAVA常用在全域的靜態變數,但extends Application的方法更符合Android架構底下。