标准体重测量器

1.运行效果图:

                       

2.代码:

package cn.edu.bzu.healthybody;

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

//import android.view.MenuItem;

public class HBActivity extends Activity {
	//private Button countButton;
	private EditText heightText;
	private RadioButton sexbtn;
	private RadioGroup MFbtn;
	private Button count;
	String sex = "";
	double weight;
	double Height;
	String height;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hb);
		
		MFbtn = (RadioGroup) findViewById(R.id.radioGroup1);
		count = (Button) findViewById(R.id.btn);
		
		MFbtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				sexbtn = (RadioButton)findViewById(MFbtn.getCheckedRadioButtonId());
				sex = sexbtn.getText().toString();
			}
		});
				
		heightText = (EditText) findViewById(R.id.etx);
		count.setOnClickListener(new tocount());
		
	}

	class tocount implements OnClickListener{

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			height = heightText.getText().toString();
			double Height= Double.valueOf(height).doubleValue();
			getWeight(sex,Height);
			
			Intent intent = new Intent();
			intent.setClass(getApplicationContext(), RSActivity.class);
			Bundle bundle = new Bundle();
			bundle.putDouble("height", Height);
			bundle.putString("sex", sex);
			bundle.putDouble("weight", weight);
			intent.putExtras(bundle);
			startActivity(intent);		
		}
	}

	private double getWeight(String sex, double height) {
		if (sex.equals("男性")) {
			weight = ((height - 80) * 0.7);
		} else {
			weight = ((height - 70) * 0.6);
		}
		return weight;
	}
	

}


</pre><pre name="code" class="java">package cn.edu.bzu.healthybody;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class RSActivity extends Activity {

	private TextView rs;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rs);
		Bundle bundle = getIntent().getExtras();
		double height = bundle.getDouble("height");
		String sex = bundle.getString("sex");
		double weight = bundle.getDouble("weight");
		

		rs=(TextView) findViewById(R.id.rs);
		rs.setText("你是一位"+sex+"\n"+"你的身高是"+height+"厘米\n"+"你的标准体重是"+weight+"公斤");
	}
	


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.r, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/p2"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.healthybody.HBActivity" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/hello"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/Female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/lady" />

        <RadioButton
            android:id="@+id/Male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gentleman" />
    </RadioGroup>

    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="45dp"
        android:onClick="tocount"
        android:text="@string/count" />

    <EditText
        android:id="@+id/etx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="36dp"
        android:ems="10"
        android:hint="@string/hint" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/etx"
        android:layout_alignLeft="@+id/etx"
        android:layout_marginBottom="52dp"
        android:text="@string/height"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/radioGroup1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_marginBottom="30dp"
        android:text="@string/sex"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.bzu.healthybody.RSActivity" >

    <TextView
        android:id="@+id/rs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

3.存在的问题:

<1>因为没有把两个RadioButton拖到RadioGroup里面,所以男性和女性可以双选,导致程序异常终止。后来拖进去以后,又出现了新的问题,显示RadioButton cannot be cast to android.widget.Spinner,后来经过refresh project,和cleannig Eclipse 的project,实现了性别的单选。

<2>得到运算结果以后,程序不能跳转到结果页面。修改Intent的函数,修改参数类型以后,可以实现界面的跳转。

<3>组件在手机环境下比例失衡,修改掉bottom的参数以后,对其他参数也进行了一系列的修改调试,得到适当的测试结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值