身高体重计量器

<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="plain">
 

activity_show.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" 
    android:background="@android:color/holo_green_light">
    
<TextView 
    android:id="@+id/tvResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10dip"
    android:textSize="20dip"/>
</LinearLayout>

activity_main.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" >
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dip"
        android:gravity="center_horizontal"
        >
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="个人标准身高计算器"
            android:textSize="22dip"
            android:textStyle="bold"
            ></TextView>
        
    </LinearLayout>


    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dip"
        >
        
        <TextView 
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:text="请输入你的身高:"
            android:layout_marginLeft="5dip"/>
        <EditText 
            android:layout_width="150dip"
            android:id="@+id/etWeight"
            android:layout_height="wrap_content"
            android:inputType="number"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cm"/>
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView 
            android:layout_width="120dip"
            android:layout_height="wrap_content"
            android:text="请选择你的性别:"
            android:layout_marginLeft="5dip"/>
        <CheckBox 
            android:id="@+id/chMan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"/>
        <CheckBox 
            android:id="@+id/chWoman"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"/>
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal">
        
        <Button
            android:layout_marginTop="20dip" 
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:id="@+id/buCalculator"
            android:text="运算"/>
    </LinearLayout>
   
</LinearLayout>
MainActivity

package com.example.heightcalculatorpro;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button buCalculator;
	
	private CheckBox chWoman;
	private CheckBox chMan;
	private EditText etWeight;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buCalculator = (Button) findViewById(R.id.buCalculator);
      
        chWoman = (CheckBox) findViewById(R.id.chWoman);
        chMan = (CheckBox) findViewById(R.id.chMan);
        etWeight = (EditText) findViewById(R.id.etWeight);
        
        buCalculator.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				result();
				
			}
		});
        
    }
    
    public void result(){
    	double high = Double.parseDouble(etWeight.getText().toString());
    	double standardWeight = 0;
    	String sex = null;
    	if(!etWeight.getText().toString().equals("")){
    		if(chMan.isChecked()){
        		standardWeight = (high-80)*0.7;
        		sex = "男";
        	}else if(chWoman.isChecked()){
        		standardWeight =(high-70)*0.6;
        		sex = "女";
        	}else{
        		Toast.makeText(MainActivity.this, "请选择性别", Toast.LENGTH_LONG);
        	}
    	}else{
    		Toast.makeText(MainActivity.this, "请输入身高", Toast.LENGTH_LONG);
    	}
    	
    	Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putDouble("high", high);
		bundle.putDouble("standardWeight",standardWeight);
		bundle.putString("sex",sex);
		intent.putExtras(bundle);
		intent.setClass(this, ShowActivity.class);
		startActivity(intent);
    	
    }

    
    
}

ShowActivity

package com.example.heightcalculatorpro;

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

public class ShowActivity extends Activity{
	private TextView tvResult;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		
		Bundle bundle = getIntent().getExtras();
		String sex = bundle.getString("sex");
		double high = bundle.getDouble("high");
		double standardWeight = bundle.getDouble("standardWeight");
		
		tvResult = (TextView) findViewById(R.id.tvResult);
		tvResult.setText("你是一位"+sex+"性\n你的身高是"+high+"\n你的标准体重是"+Math.abs(standardWeight));
	}

}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值