Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android activity 相互傳值

android activity 相互傳值

編輯:關於Android編程

1,傳一個值的情況:   Main.activity   Pass value:   Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("Main", "123"); startActivityForResult(intent, 0); Get value from other activity @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { Log.d("debug", "result=" + data.getStringExtra("second")); } super.onActivityResult(requestCode, resultCode, data); }   Test.activity Get value:   Intent intent = getIntent(); final String num = intent.getStringExtra("Main");   Then pass value to main activity Intent intent = new Intent(); intent.putExtra("second", "This is Second Activity's value!"); setResult(RESULT_OK, intent); finish();   如果傳多個值的情況使用 Bundle Pass value: Home Activity: Bundle bundle = new Bundle();   *字符、字符串、布爾、字節數組、浮點數等等,都可以傳*/ bundle.putString("Name", "feng88724"); bundle.putBoolean("Ismale", true);   /*把bundle對象assign給Intent*/ intent.putExtras(bundle);   Get value: Away Activity: /*獲取Intent中的Bundle對象*/ Bundle bundle = this.getIntent().getExtras();   *獲取Bundle中的數據,注意類型和key*/ String name = bundle.getString("Name"); boolean ismale = bundle.getBoolean("Ismale");  
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved