标准体重计算器
实现目标:
如果是男性,标准体重=(身高-80)*0.7
如果是女性,标准体重=(身高-70)*0.6
运行效果图:
关键代码展示:
package cn.bzu.bztc.standardweight;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
/*
* This is a calculation of the standard weight of demo
* @date 2016-3-21
*/
public class MainActivity extends Activity
{
private Button btName;
private EditText etName;
private RadioButton rbw,rbm;
private double standardWeight;//标准体重
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
doEvent();
}
/*
* 初始化布局
*/
private void initView(){
rbm=(RadioButton)findViewById(R.id.rbm);
rbw=(RadioButton)findViewById(R.id.rbw);
btName=(Button)findViewById(R.id.btName);
etName=(EditText)findViewById(R.id.etName);
}
/*
* 事件处理
*/
public void doEvent(){
btName.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String heighString=etName.getText().toString();
if (TextUtils.isEmpty(heighString))
{
showToast("你的身高未填写");
}else {
if (rbm.isChecked()){
standardWeight = calWeight("man",heighString);
Intent manIntent =new Intent(MainActivity.this,ShowActivity.class);
/*
* 数据包Bundle,包含性别,身高,体重
*/
Bundle bundle=new Bundle();
bundle.putString("sex","男性");
bundle.putDouble("standardWeight", standardWeight);
bundle.putString("height", heighString);
manIntent.putExtra("bundle", bundle);
startActivity(manIntent);
}else if (rbw.isChecked()) {
//如果女性的radioButton被选中
standardWeight=calWeight("woman", heighString);
Intent womenIntent=new Intent(MainActivity.this,ShowActivity.class);
/*
* 数据包Bundle,包含性别,身高,体重
*/
Bundle bundle=new Bundle();
bundle.putString("sex", "女性");
bundle.putDouble("standardWeight", standardWeight);
bundle.putString("height", heighString);
womenIntent.putExtra("bundle", bundle);
startActivity(womenIntent);
}
{
}
}
}
});
}
/*
* 计算标准体重
* @param sex,height
* @return standardWeight
*/
private double calWeight(String sex,String height){
if (sex.equals("man"))
{
double manWeight=(Integer.parseInt(height)-80)*0.7;
return manWeight;
}else if (sex.equals("woman")) {
double womanWeight=(Integer.parseInt(height)-70)*0.6;
return womanWeight;
}
return 1.0;
}
/*
* 显示Toast
* @param msg
*/
private void showToast(String msg){
Toast.makeText(this, ""+msg,Toast.LENGTH_LONG).show();
}
}
页面展示类代码:
package cn.bzu.bztc.standardweight;
import android.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
/*
* 展示页面
*/
public class ShowActivity extends Activity
{
private TextView tvShow;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
tvShow=(TextView)findViewById(R.id.tvShow);
tvShow.setText("你是一位"+getIntent().getBundleExtra("bundle").get("sex")+"\n"+
"你的身高是"+getIntent().getBundleExtra("bundle").get("height")+"厘米"+"\n"+
"你的标准体重是"+getIntent().getBundleExtra("bundle").get("standardWeight")+"公斤"+"\n");
}
}
总结:1.在RadioButton组件使用过程中,通过查阅资料获得如何选中一个RadioButton,把其放在RadioGroup组件中,在其布局中把RadioGroup属性中设置orientation为horizontal,让其两个RadioButton在方向上水平。
2.Intent是Android中简单的消息传递框架,Intent起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
信使Intent——打开新的Activity,不传递参数:在一个Activity中可以使用系统提供的StartActivity(Intent intent)方法打开新的Actity,在打开新的Activity前,可以决定是否为新的Activity传递参数:
第一种:打开新的Activity,不传递参数
第二种:打开新的Activity,并传递若干个参数给它:
第一种写法:用于批量添加数据到Intent:
Intent intent=new Intent();
Bundle bundle=new Bundle();//携带数据
bundle.putString("name","名字");
intent.putExtras(bundle);//追加原有的数据
第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个添加到Intent
Intent intent =new Inten();
intent.putExtra("name","名字");
Intent 提供了各类常用类型重载后的putExtra()方法,如:putExtra(String name,String value)、putExtra(String name,long value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra方法传入的值都会存入Bundle对象,下面是Intent的putExtra(String name,String value)方法代码片段:
public class Intent implements Parcelable{
private Bundle mExtras;
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}