2010年6月6日 星期日

傳送資料到新的Activity去

傳送資料到新的Activity去

話先說在前頭


前面我們已經學會如何在目前的Activity開啟一個新的Activity了,現在我要學怎麼傳送資料到新的Activity去。

首先我們在上一篇的7~12中間加入8~11的程式。


    1.     private OnClickListener calcBMI = new OnClickListener() 
    2.     {
    3.         public void onClick(View v)
    4.         {
    5.           //Switch to report page
    6.           Intent intent = new Intent();
    7.           intent.setClass(Bmi2.this, Report.class);
    8.           Bundle bundle = new Bundle();
    9.           bundle.putString("KEY_HEIGHT", fieldHeight.getText().toString());
    10.           bundle.putString("KEY_WEIGHT", fieldWeight.getText().toString());
    11.           intent.putExtras(bundle);
    12.           startActivity(intent);
    13.         }
    14.     };



這邊程式很簡單,只是宣告一個 Bundle ,然後將參數塞入 Bundle,然後在將 Bundle 塞入 Intent 裡面去而已。接下來我們得在 Report 這個 Activity 裡面把剛剛塞入的參數給解出來。


    1.     Bundle bundle = this.getIntent().getExtras();      
    2.     double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;
    3.     double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));

這邊也很簡單,只是反向的把參數解出來;先從 getExtras() 取得 Bundle ,然後在利用識別字去取得對應的資料。其他的只是把計算的部分搬到 Report 來,並沒有什麼特別的程式。



Report 的全部程式如下:

    1. package com.demo.android.bmi2;
    2.  
    3. import java.text.DecimalFormat;
    4.  
    5. import android.app.Activity;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10. import android.widget.TextView;
    11.  
    12. public class Report extends Activity {
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState){
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.report);
    17.         findViews();
    18.         showResults();
    19.         setListensers();
    20.     }
    21.  
    22.     private Button button_back;
    23.     private TextView view_result;
    24.     private TextView view_suggest;
    25.     private OnClickListener backMain = new OnClickListener(){
    26.         public void onClick(View v){
    27.             Report.this.finish();
    28.         }
    29.     };
    30.    
    31.     private void setListensers() {
    32.         // TODO Auto-generated method stub
    33.         button_back.setOnClickListener(backMain);
    34.     }
    35.  
    36.    
    37.     private void showResults() {
    38.         // TODO Auto-generated method stub
    39.         DecimalFormat nf = new DecimalFormat("0.00");
    40.        
    41.         Bundle bundle = this.getIntent().getExtras();      
    42.         double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100;
    43.         double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
    44.         double bmi = weight / (height * height);
    45.        
    46.         view_result.setText("Your BMI is " + nf.format(bmi));
    47.  
    48.         if(bmi>25)
    49.         {
    50.           view_suggest.setText(R.string.advice_heavy);
    51.         }
    52.         else if(bmi<20)
    53.         {
    54.           view_suggest.setText(R.string.advice_light);
    55.         }
    56.         else
    57.         {
    58.           view_suggest.setText(R.string.advice_avg);
    59.         }
    60.     }
    61.  
    62.     private void findViews() {
    63.         // TODO Auto-generated method stub
    64.         button_back = (Button)findViewById(R.id.report_back);
    65.         view_result = (TextView)findViewById(R.id.result);
    66.         view_suggest = (TextView)findViewById(R.id.suggest);
    67.     }
    68. }

沒有留言:

張貼留言