<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                1、全局對象是Activity之間傳遞數據的一種比較實用的方式,比如在JavaWeb中有四個作用域,這四個作用域從小到大分別是Page、Request、Session和Application,其中Application域在應用程序的任何地方都可以使用和訪問,除非是Web服務器停止。Android中的全局對象非常類似于JavaWeb中的Application域,只要Android應用程序不清除內存,全局對象就可以一直訪問~ 2、新建一個Android項目:“android_app”,進入“main.xml”添加一個Button,代碼如下: ~~~ <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="使用Application傳遞數據" /> ~~~ 3、在當前目錄下創建一個“other.xml”,添加一個TextView,代碼如下: ~~~ <TextText android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextText> ~~~ 4、在當前包下新建一個類“OtherActivity”,并使其繼承Activity;添加一個“onCreate”方法,代碼如下: ~~~ package com.android.app; import android.app.Activity; import android.os.Bundle; public class OtherActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); } } ~~~ 5、為了能在程序中使用全局變量,需要在當前包下新建一個普通類“MyApp”,使其繼承“Application”,隨后聲明一個“name”成員并提供構造方法和添加“onCreate”代碼如下: ~~~ package com.android.app; import android.app.Application; public class MyApp extends Application { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); setName("張三"); } } ~~~ 6、進入“Main.java”,添加Button和MyApp成員,隨后為Button設置一個點擊的事件,代碼如下: ~~~ package com.android.app; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class Main extends Activity { private Button button; private MyApp myApp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button); // 為button注冊一個點擊事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub myApp = (MyApp) getApplication(); myApp.setName("Jack");// 修改之后的名字 Intent intent = new Intent(Main.this, OtherActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } ~~~ 7、進入“OtherActivity.java”中,聲明一個MyApp成員,并獲取myApp,代碼如下: ~~~ package com.android.app; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity { private MyApp myApp; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); textView = (TextView) this.findViewById(R.id.msg); myApp = (MyApp) getApplication(); textView.setText("-appname-->>" + myApp.getName()); } } ~~~ 8、修改清單文件“AndroidManifest.xml”,在“Application”標簽中新加入一個屬性“name”,再加入一個“Activity”,代碼如下: ~~~ <application android:name=".MyApp"//后添加的屬性 android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android.app.Main" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".OtherActivity" >//后添加的標簽 </activity> </application> ~~~ 9、運行一下,得到結果: ![](https://box.kancloud.cn/2016-08-23_57bc06b1c376a.jpg) ![](https://box.kancloud.cn/2016-08-23_57bc06b22d397.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看