2012年2月5日 星期日

[TQC+ Android] 1-4 計算BMI值 From Activity to another , and back. Use startActivityForResult





在輸入性別 , 身高及體重後 , 按下計算按鈕 , 在Activity 中運用Intent傳遞資訊並啟動另一個Activity , 並計算BMI值 , 這題重點在兩個Activity的資料傳遞 , 記得要在AndroidManifest.xml 宣告另一支Activity, 以下是程式碼。


package COM.TQC.GDD01;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class GDD01 extends Activity 
{
  private EditText etheight , etweight;
  private RadioButton rb1 , rb2; 
  final int RequestCode = 0;
    
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.main);
    
    etheight = (EditText) findViewById(R.id.editText1);
    etweight = (EditText) findViewById(R.id.editText2);
    
    rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb1.setChecked(true);
    
    rb2 = (RadioButton) findViewById(R.id.radioButton2);
    
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {       
       Bundle bundle = new Bundle();
       if(rb1.isChecked()) bundle.putString("Sex", "M");
       else bundle.putString("Sex", "F");
       bundle.putDouble("height", Double.parseDouble(etheight.getText().toString()));
       bundle.putDouble("weight", Double.parseDouble(etweight.getText().toString()));
          
       Intent intent = new Intent();
       intent.setClass(GDD01.this, GDD01_child.class);
       intent.putExtras(bundle);
       GDD01.this.startActivityForResult(intent, RequestCode);
       
     /*
           從輸入介面中取出了的身高、體重值,要將身高、體重值傳送給 child_Activity 後作計算       
                           這些附加在 Intent 上的訊息都儲存在 Bundle 物件中    
          透過「intent.putExtras(bundle)」敘述,將「bundle」 物件附加在 Intent 上,
          隨著 Intent 送出而送出
     */
     
      }
    });
  } 
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data)
  {
    switch (resultCode)
    { 
      case RESULT_OK:
    
        Bundle bundle = data.getExtras();
        String Sex = bundle.getString("Sex");
        double height = bundle.getDouble("height");
        double weight = bundle.getDouble("weight");
        
        etheight.setText("" + height);
        etweight.setText("" + weight);
        if(Sex.equals("M"))
        {
          rb1.setChecked(true);
        }else
        {
          rb2.setChecked(true);
        }
        break;       
      
      default: 
        break; 
     } 
   } 
}

另一個Activity

package COM.TQC.GDD01 ;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class GDD01_child extends Activity 
{
  Bundle bundle;
  Intent intent; 
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);    
    setContentView(R.layout.mylayout);
    
    intent=this.getIntent();
    bundle = intent.getExtras();    
   
    String Sex = bundle.getString("Sex");
    double height = bundle.getDouble("height");
    double weight = bundle.getDouble("weight");
      
    
    String BMI_result = this.getBMI(height,weight);
    String BMI_advice = this.getAdvice(Sex,height,weight);
    
    TextView tvBMI = (TextView) findViewById(R.id.tvBMI);
    tvBMI.setText("BMI 結果" + BMI_result);
    TextView tvAdvice = (TextView) findViewById(R.id.tvAdvice);
    tvAdvice.setText(BMI_advice);    
  
    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new Button.OnClickListener()
    {
      public void onClick(View v)
      {          
      
       GDD01_child.this.setResult(RESULT_OK, intent);
       GDD01_child.this.finish();
      }
    }); 
    
  }
  
  //BMI值格式化
  private String format(double num)
  {
      NumberFormat NF = new DecimalFormat(".##");
   return NF.format(num);
   
  }
  //取得BMI值
  private String getBMI (double height, double weight)
  {
   double BMI = weight / Math.pow(height, 2);
   return format(BMI);
  }
  //依BMI值取得建議
  private String getAdvice (String Sex, double height, double weight)
  {
  double doubleBMI = Double.parseDouble(getBMI(height,weight));
  String Advice;
  
  if(Sex.equals("m"))
   {
    if(doubleBMI>25) Advice = (String) getText(R.string.advice_heavy);
    else if(doubleBMI<20) Advice = (String) getText(R.string.advice_light);
    else Advice = (String) getText(R.string.advice_average);
   }
   else
   {
    if(doubleBMI>22) Advice = (String) getText(R.string.advice_heavy);
    else if(doubleBMI<18) Advice = (String) getText(R.string.advice_light);
    else Advice = (String) getText(R.string.advice_average);    
   } 
  
  return Advice;  
  }
  
}

main.xml


      
      
      
     
      
      

mylayout.xml
    
    
    
    
  


最後是AndroidManifest.xml

    
        
            
                
                
            
        
        
    
    



P.S. 題目中所要求的Variable和Method皆會保留 , 也會根據題目所要求的流程去實作 , 縱使題目要求繞遠路....



沒有留言:

張貼留言

Google Analytics