根據身高計算體重

never123450發表於2014-07-02

fragment_main.xml

<LinearLayout 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="com.xwy.jisuan.MainActivity$PlaceholderFragment"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="20px"
        android:text="計算您的標準體重" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別:" />

	    <RadioGroup
	        android:id="@+id/sex"
	        android:orientation="horizontal"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content" >
	
	        <RadioButton
	            android:id="@+id/radio0"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:checked="true"
	            android:text="男" />
	
	        <RadioButton
	            android:id="@+id/radio1"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"
	            android:text="女" />
	    </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高:" />

        <EditText
            android:id="@+id/stature"
            android:minWidth="100px"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </EditText>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cm" />

    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="確定" />
    

</LinearLayout>

result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10px"
        android:text="性別" />

    <TextView
        android:id="@+id/stature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10px"
        android:text="身高" />

    <TextView
        android:id="@+id/weight"
        android:padding="10px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="標準體重" />
</LinearLayout>

MianActivity.java

package com.xwy.jisuan;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		        Info info=new Info();	//例項化一個儲存輸入基本資訊的物件
		        
		        if("".equals(((EditText)findViewById(R.id.stature)).getText().toString())){
		        	Toast.makeText(MainActivity.this, "請輸入您的身高,否則不能計算!", Toast.LENGTH_SHORT).show();
		        	return;
		        }
		        int stature=Integer.parseInt(((EditText)findViewById(R.id.stature)).getText().toString());
		        RadioGroup sex=(RadioGroup)findViewById(R.id.sex);	//獲取設定性別的單選按鈕組
		        //獲取單選按鈕組的值
		        for(int i=0;i<sex.getChildCount();i++){
					RadioButton r=(RadioButton)sex.getChildAt(i);		//根據索引值獲取單選按鈕
					if(r.isChecked()){							//判斷單選按鈕是否被選中
						info.setSex(r.getText().toString());		//獲取被選中的單選按鈕的值
						break;								//跳出for迴圈
					}
				}

		        info.setStature(stature);	//設定身高
		        Bundle bundle=new Bundle();	//例項化一個Bundle物件
		        bundle.putSerializable("info", info);	//將輸入的基本資訊儲存到Bundle物件中
		        Intent intent=new Intent(MainActivity.this,ResultActivity.class);
		        intent.putExtras(bundle);	//將bundle儲存到Intent物件中
		        startActivity(intent);	//啟動intent對應的Activity				
			}
		});
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, 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);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			return rootView;
		}
	}

}

Info.java

package com.xwy.jisuan;
import java.io.Serializable;

public class Info implements Serializable {

	private static final long serialVersionUID = 1L;
	private String sex="";		//性別
	private int stature=0;	//身高
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getStature() {
		return stature;
	}
	public void setStature(int stature) {
		this.stature = stature;
	}
}
ResultActivity.java
package com.xwy.jisuan;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class ResultActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result);		//設定該Activity使用的佈局
		TextView sex=(TextView)findViewById(R.id.sex);	//獲取顯示性別的文字框
		TextView stature=(TextView)findViewById(R.id.stature);	//獲取顯示身高的文字框
		TextView weight=(TextView)findViewById(R.id.weight);	//獲取顯示標準體重的文字框
		Intent intent=getIntent();	//獲取Intent物件
		Bundle bundle=intent.getExtras();	//獲取傳遞的資料包
		Info info=(Info)bundle.getSerializable("info");	//獲取一個可序列化的info物件
		sex.setText("您是一位"+info.getSex()+"士");	//獲取性別並顯示到相應文字框中
		stature.setText("您的身高是"+info.getStature()+"釐米");	//獲取身高並顯示到相應文字框中
		
		weight.setText("您的標準體重是"+getWeight(info.getSex(),info.getStature())+"公斤");	//顯示計算後的標準體重
	}
	/**
	 * 功能:計算標準體重
	 * @param sex
	 * @param stature
	 * @return
	 */
	private String getWeight(String sex,float stature){
		String weight="";	//儲存體重
		NumberFormat format=new DecimalFormat();
		
		if(sex.equals("男")){	//計算男士標準體重
			weight=format.format((stature-80)*0.7);
		}else{		//計算女士標準體重
			weight=format.format((stature-70)*0.6);
		}
		return weight;
	}
}

在Mainfest中配置

 <activity
            android:label="顯示結果"
            android:icon="@drawable/ic_launcher"
            android:name=".ResultActivity"></activity>

相關文章