TQC-104 二個activity
這是TQC的104題,
程式碼為自行撰寫,
僅供參考。
最主要練習二個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;
private Button b1;
private String _sex;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findview();
setlisten();
}
public void findview()
{
b1 = (Button) findViewById(R.id.button1);
rb1=(RadioButton)findViewById(R.id.radioButton1);
rb2=(RadioButton)findViewById(R.id.radioButton2);
etheight=(EditText)findViewById(R.id.editText1);
etweight=(EditText)findViewById(R.id.editText2);
}
public void setlisten()
{
rb1.setOnClickListener(new RadioButton.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
_sex="M";
rb2.setChecked(false);
}
});
rb2.setOnClickListener(new RadioButton.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
_sex="G";
rb1.setChecked(false);
}
});
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//從輸入介面中取出了的身高、體重值,要將身高、體重值傳送給 child_Activity 後作計算
//這些附加在 Intent 上的訊息都儲存在 Bundle 物件中
//透過「intent.putExtras(bundle)」敘述,將「bundle」 物件附加在 Intent 上,隨著 Intent 送出而送出
Bundle bundle=new Bundle();
bundle.putString("Sex", _sex.toString());
bundle.putString("height", etheight.getText().toString());
bundle.putString("weight", etweight.getText().toString());
Intent intent=new Intent();
intent.setClass(GDD01.this, GDD01_child.class);
intent.putExtras(bundle);
startActivity(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;
}
}
}
package COM.TQC.GDD01 ;
import java.text.DecimalFormat;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GDD01_child extends Activity
{
Bundle bundle;
Intent intent;
double _d1;
String _s1,_s2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
TextView tvBMI=(TextView) findViewById(R.id.tvBMI);
TextView tvAdvice=(TextView) findViewById(R.id.tvAdvice);
intent=this.getIntent();
bundle = intent.getExtras();
String Sex = bundle.getString("Sex");
String _s1=bundle.getString("height");
String _s2=bundle.getString("weight");
tvBMI.setText(_s1.toString());
double height = Double.parseDouble(_s1);
height=height/100;
double weight = Double.parseDouble(_s2);
//double height = 170;
//double weight = 75;
String BMI_result = this.getBMI(height,weight);
String BMI_advice = this.getAdvice(Sex,height,weight);
tvBMI.setText(BMI_result);
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)
{
return "";
}
//取得BMI值
private String getBMI (double height, double weight)
{
_d1=weight / (height * height);
return String.valueOf(_d1);
}
//依BMI值取得建議
private String getAdvice (String Sex, double height, double weight)
{
if(_d1>25)
{
_s1="您該少吃些,並多多運動";
}
else if(_d1<20)
{
_s1="您該多吃點";
}
else
{
_s1="體型很棒喔";
}
return _s1.toString();
}
}
自我LV~