标准体重计算器

                     (标准体重计算器)效果图

     

一. 在res文件中,values文件中string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">"标准体重计算器"</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="lable_biaoti">计算你的标准体重</string>
    <string name="lable_xingbie">性别</string>
    <string name="radio_nan">男</string>
    <string name="radio_nu">女</string>
    <string name="lable_shengao">身高(单位:CM)</string>
    <string name="button_main">计算</string>
    <string name="button_show">返回上一页</string>
    <string name="title_activity_show">ShowActivity</string>

</resources>

二. 在res.文件中,layout文件中activity_main.xml

<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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="47dp"
        android:text="@string/button_main" />

   

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/radiogroupe"
        android:text="@string/lable_biaoti" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radiogroupe"
        android:layout_marginTop="44dp"
        android:text="@string/lable_shengao" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:ems="10" >
    </EditText>

    <RadioGroup
        android:id="@+id/radiogroupe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
         <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:layout_toLeftOf="@+id/editText1"
        android:text="@string/lable_xingbie" />

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView1"
            android:layout_alignTop="@+id/textView2"
            android:text="@string/radio_nan" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="40dp"
            android:text="@string/radio_nu" />
    </RadioGroup>

</RelativeLayout>

  注:在这个布局文件中有一个   <RadioGroup >  </RadioGroup> 这个控件主要是把 两个<RadioButton> </RadioButton>包围起来,意思就是这样能实现两个控件的单项选                 择。

三. 在res.文件中,layout文件中activity_show.xml,这个布局文件就是在前面activity_main.xml跳转过来以后显示前面所带过来的数据。

<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=".ShowActivity" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="165dp"
        android:text="@string/button_show" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="39dp"
        android:text="TextView" />

</RelativeLayout>

四. src文件中,MainActivity.java

package cn.edu.bzu.tizhongjisuan;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.MenuItemCompat.OnActionExpandListener;
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;

public class MainActivity extends Activity implements OnClickListener {
	protected int code ;
	private RadioButton radioButton1, radioButton2;
	private EditText editText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button bu = (Button) findViewById(R.id.button1);
		bu.findViewById(R.id.button1).setOnClickListener(this);

		radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
		radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
		editText = (EditText) findViewById(R.id.editText1);

	}

	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.button1) {
			double height = Double.parseDouble(editText.getText().toString());
			String sex = "";
			RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
			if (rb1.isChecked()) {
				sex = "M";
			} else {
				sex = "F";
			}
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, ShowActivity.class);
			Bundle bundle = new Bundle();
			bundle.putDouble("height", height);
			bundle.putString("sex", sex);
			intent.putExtras(bundle);
			startActivityForResult(intent, code);
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case RESULT_OK:
			Bundle bunde = data.getExtras();
			String sex = bunde.getString("sex");
			double height = bunde.getDouble("height");
			editText.setText("" + height);
			if (sex.equals("M")) {
				radioButton1.setChecked(true);
			} else {
				radioButton2.setChecked(false);
			}
			break;
		default:
			break;
		}
	}
}

五 . src文件中,ShowActivity.java

package cn.edu.bzu.tizhongjisuan;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.security.auth.PrivateCredentialPermission;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.format.Formatter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ShowActivity extends Activity implements OnClickListener {
	private Intent intent;
	private Bundle bundle;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		Button b1 = (Button) findViewById(R.id.button2);
		findViewById(R.id.button2).setOnClickListener(this);
		bundle = this.getIntent().getExtras();
		String sex = bundle.getString("sex");
		double height = bundle.getDouble("height");
		String sexText = "";
		if (sex.equals("M")) {
			sexText = "男性";
		} else {
			sexText = "女性";
		}
		String weight = this.getWeight(sex, height);
		TextView tv1 = (TextView) findViewById(R.id.textView4);
		tv1.setText("你是一位" + sexText + "\n你的身高是" + height + "厘米\n你的标准体重是"
				+ weight + "公斤");

	}

	private String format(double num) {
		NumberFormat formatter = new DecimalFormat("0.00");
		String s = formatter.format(num);
		return s;
	}

	// 计算体重和身高的公式
	private String getWeight(String sex, double height) {
		String weight = "";
		if (sex.equals("M")) {
			weight = format((height - 80) * 0.7);
		} else {
			weight = format((height - 70) * 0.6);
		}
		return weight;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if (id == R.id.button2) {
			ShowActivity.this.setResult(RESULT_OK, intent);
			ShowActivity.this.finish();
		}
	}
}

六. 在AndroidManifest.xml中声明。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.bzu.tizhongjisuan"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="ShowActivity"
            android:label="@string/title_activity_show" >
        </activity>
    </application>

</manifest>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值