Android開發循序漸進執行個體2–畫面間資料傳遞例子

來源:互聯網
上載者:User

在Android之中,畫面間的資料傳遞主要是通過Intent的putExtra方法來實現的,傳遞的資料結構就是一個Map資料(Name/Value對,特別注意不同的是必須為基礎資料型別 (Elementary Data Type)),下面將通過執行個體的方式來講述具體的實現方法。

本執行個體中主要講述如何將畫面一的資料傳遞到畫面二當中,在畫面二對畫面一傳遞迴來的資料進行一定的計算處理後然後將資料返回到畫面一當中。

1. 按照如下的配置以及在開發循序漸進執行個體1中描述的方法建立整個項目Base(特別需要注意在AndroidManifest.xml中加入對SecondActivity的配置,具體方法見執行個體1):
 project Name: ExampleTwo
 Platform: Android2.0;
 Application name: ExampleOne
 package name: com.example
 Activity: FirstActivity, SecondActivity
 Resource file: first.xml, second.xml
 first.xml 有二個Button:
 Id: @+id/ForwardByView
 Text: Forward to second view by view
        Id: @+id/ForwardByStatus
 Text: Forward to second view by status
 second.xml 有一個Button: BackButton
 Id: @+id/Back
 Text: Back to first view

2. 在FirstView中加入如下的代碼(對於重載onActivityResult可在該類上使用滑鼠右鍵菜單並且選擇Source->Override/Implement methods...在彈出框中選擇onActivityResult()並且點擊OK):
        private void forward_view_event_byview() {
  Button button = (Button) findViewById(R.id.ForwardView);
  button.setOnClickListener(forwardByviewbutton_listener);
 }
 
 private Button.OnClickListener forwardByviewbutton_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   Intent intent = new Intent();
   intent.setClass(FirstActivity.this, SecondActivity.class);
   intent.putExtra("Param", "action view");

   startActivityForResult(intent, 1);
  }
 };

 private void forward_view_event_bystatus() {
  Button button = (Button) findViewById(R.id.ForwardStatus);
  button.setOnClickListener(forwardBystatusbutton_listener);
 }

 private Button.OnClickListener forwardBystatusbutton_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   Intent intent = new Intent();
   intent.setClass(FirstActivity.this, SecondActivity.class);
   intent.putExtra("Param", "action status");

   startActivityForResult(intent, 2);
  }
 };
 
 private void showReturnValue(int requestCode, int resultCode, Intent data) {
  Bundle extras = data.getExtras();
  if (extras != null) {
   String returnValue = extras.getString("ReturnValue");
   setTitle(returnValue + " ["+ String.valueOf(requestCode) + "-" + String.valueOf(resultCode) + "]");
  }
 }
 
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  showReturnValue(requestCode, resultCode, data);
 }

   最後在onCreate()結束處插入如下代碼調用:
    forward_view_event_byview();
    forward_view_event_bystatus();

3. 在SecondView中加入如下代碼:
 private void show_input_values() {
  Bundle extras = getIntent().getExtras();
  if (extras != null) {
   String inputValue = extras.getString("Param");
   setTitle(inputValue);
  }
 }

 private void back_view_event() {
  Button button = (Button) findViewById(R.id.Back);
  button.setOnClickListener(back_listener);
 }
 
 private Button.OnClickListener back_listener = new Button.OnClickListener() {
  public void onClick(View v) {
   Bundle retBundle = new Bundle();
   retBundle.putString("ReturnValue", "String comes from the second view");
   Intent intent = new Intent();
   intent.putExtras(retBundle);
   setResult(RESULT_OK, intent);
   finish();
  }
 };

並且在onCreate()函數的末尾處插入如下的代碼調用:
 show_input_values();
 back_view_event();

總結:本執行個體展示了如何將FirstView中名為"Param"的參數值傳入到SecondView中,並且展示了2種RequestCode下傳送的方法,在SecondView中展示了如何將FirstView傳送的值進行顯示(基本處理),然後在返回時如何將名為"ReturnValue"的參數值返回到FirstView中,並且將其顯示出來。

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/jackxinxu2100/archive/2010/01/26/5258330.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.