android新視窗以及傳值

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   使用   sp   for   strong   

1,建立一個activity,如Activity2,在資訊清單檔AndroidManifest.xml 中 application節點中 增加一個新表單:

 

.................

</activity>
<activity android:name=".ortherActivity" android:label="新視窗"/>
</application>

2,在主程式中:

      Intent intent = new Intent(MainActivity.this, Activity2.class);

       //開啟新視窗。參數:主視窗,被調用視窗

     

      Bundle bundle = new Bundle();//通過Bundle實現資料的傳遞:
      bundle.putString("key1", "value1");// key1為名,value1為值
      bundle.putString("key2", "value2");
      bundle.putInt("keyInt1", 100);
      intent.putExtras(bundle); // 傳資料

 

      如果不需要子視窗回傳資料:
      startActivity(intent);

     

      如果需要子視窗回傳資料:

      (1),startActivityForResult(intent, requestCode);

       此處要注意,requestCode是子視窗的id標誌,而且必須大於1,否則回呼函數onActivityResult不響應!  

      (2),實現回呼函數onActivityResult:

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  .....

 }

     主窗只有一個回呼函數,如果調用了幾個子視窗,就需通過requestCode來區分。而resultCode則是子視窗的傳回值,可以用於後續的判斷。

3,在新視窗中仍然使用Bundle取得資料:
  Bundle bundle = this.getIntent().getExtras();
  String s1 = bundle.getString("key1");
  String s2 = bundle.getString("key2");
  int i=bundle.getInt("keyInt1");

 

4,如何返回到主視窗?

    Intent intent=new Intent();   
    Bundle bunle=new Bundle();   
    bunle.putString("key1", "這是子視窗的返回資料.");  
    intent.putExtras(bunle);
    //SubActivity.this.setResult(RESULT_OK, intent);
    //SubActivity.this.finish();
    setResult(RESULT_OK, intent);  //設定返回結果
    finish(); //關閉子視窗,否則資料無法返回

5,參考

   http://blog.sina.com.cn/s/blog_63d409eb01017dnn.html

 

轉自:http://blog.sina.com.cn/s/blog_3ecf39a70101axmo.html

android新視窗以及傳值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.