<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                1、Intent(意圖):Intent是一種運行時綁定機制(runtime binding),用于在兩個Activity之間傳遞數據。Intent也具有媒體中介的作用,實現調用者與被調用者之間的解耦; 2、Intent傳遞數據通用方式:(1)在Main.java中構造一個Intent;(2)然后調用startActivity(intent)將構造的Intent傳入;(3)系統根據Intent中的描述,到Manifest.xml尋找滿足此要求的Activity(4)系統會調用這個Activity,傳入Intent,執行相應的操作~ 3、新建Android項目”android_intent“,創建Activity名為”Main“; 4、在“res/layout/main.xml”中添加一個”Button“,完成布局文件的編寫,代碼如下: ~~~ <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="測試使用Intent傳遞數據" > </Button> ~~~ 5、新建“OtherActivity.java”,并在“Superclass”中選擇“android.app.Activity”(繼承Activity),并在其中添加onCreate()方法(代碼在步驟9); 6、接著聲明一個布局文件,在“res/layout”中新建“other.xml”,將其代碼修改如下: ~~~ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/msg" android:layout_width="fill_parent" android:layout_height="fill_parent" > </TextView> </LinearLayout> ~~~ 7、打開“src”中的“Main.java”,在Main類中添加代碼以構造Intent并啟動Intent,添加后代碼如下: ~~~ public class Main extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// 加載布局文件 button = (Button) this.findViewById(R.id.button);//根據Id查找視圖 button.setOnClickListener(new View.OnClickListener() {//點擊,啟動點擊事件,在其中新建意圖 @Override public void onClick(View arg0) { // TODO Auto-generated method stub //由Main.this傳入OtherActivity.class,表示從當前意圖傳入目標意圖。 Intent intent = new Intent(Main.this, OtherActivity.class); // 在意圖中傳遞數據,將數據臨時存儲在內存中 intent.putExtra("name", "張三"); intent.putExtra("age", 23); intent.putExtra("address", "北京"); // 啟動意圖 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; } } ~~~ 8、打開“AndroidManifest.xml”,在“Application”標簽下添加一個“Activity”標簽(以使系統在傳入Intent后能查找到名為“OtherActivity”的活動),代碼如下: ~~~ <activity android:name=".OtherActivity" >//用“.”表示當前這個類 </activity> ~~~ 9、在“OtherActivity.java”中添加一個“getIntent”方法,獲取意圖,隨后調用get方法獲取數據,再將獲取的信息放入“TextView”中。完整代碼如下: ~~~ public class OtherActivity extends Activity { private TextView textview; public OtherActivity() { // TODO Auto-generated constructor stub } @Override protected void onCreate(Bundle savedInstanceState) {//創建Activity // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other);// 加載布局文件(必須!) textview = (TextView) this.findViewById(R.id.msg); //使用getIntent方法獲取意圖 Intent intent = getIntent(); //使用get方法取出Intent中的數據 int age = intent.getIntExtra("age", 0); String name = intent.getStringExtra("name"); String address = intent.getStringExtra("address"); textview.setText("age--->>" + age + "\n" + "name--->>" + name + "\n" + "address--->>" + address + "\n"); } } ~~~ 10、右擊“src”下的包,選擇“Run as”中的“Android Application”來運行程序,結果截圖如下: ![](https://box.kancloud.cn/2016-08-23_57bc06b032dd4.jpg) 點擊“Button”顯示: ![](https://box.kancloud.cn/2016-08-23_57bc06b065d99.jpg) Ps:第一個Android程序,見笑~
                  <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>

                              哎呀哎呀视频在线观看