实验二 任务2 标准体重计算器

本文介绍了一个简单的标准体重计算器应用,该应用使用RadioButton选择性别,并通过EditText输入身高,然后根据不同的公式计算标准体重。通过Intent和Bundle在两个Activity间传递数据。
摘要由CSDN通过智能技术生成



  实验二 任务2 标准体重计算器


实验结果


activity_calculator.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=".CalculatorActivity" >

    <RadioGroup
        android:id="@+id/SexGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:layout_centerHorizontal="true"  
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/Male"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/Male" />

        <RadioButton
            android:id="@+id/Female"
            android:layout_width="72dp"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/Female" />
</RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="39dp"
        android:text="@string/Sex"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/SexGroup"
        android:text="@string/Height"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/SexGroup"
        android:layout_alignRight="@+id/textView1"
        android:layout_alignTop="@+id/textView3"
        android:ems="10"
        android:inputType="number"
         android:textColorHint="#AAAAAA"
        android:hint="@string/unit"   >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        
        android:text="@string/Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="@string/LargeTextView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

activity_show.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" 
    android:background="@drawable/swbg">
    

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp"
        android:textAppearance="?android:attr/textAppearanceLarge"  />

</RelativeLayout>


CalculatorActivity.java

package com.test.standardweight;

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.EditText;
import android.widget.RadioButton;

public class CalculatorActivity extends Activity {
	    private Button button1;  
	    private EditText edittext;  
	@Override
	protected void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calculator);
		    button1 = (Button) findViewById(R.id.button1);  
	        edittext = (EditText) findViewById(R.id.editText1);  
	        button1.setOnClickListener(new OnClickListener() { 
	  
	            public void onClick(View v) {  
	                String number = edittext.getText().toString();  
	                int b = Integer.parseInt(number);  
	                double Mweight = (b - 80) * 0.7;  
	                double Fweight = (b - 70) * 0.6;  
	                Intent intent = new Intent(CalculatorActivity.this, SHOWActivity.class);  
	  
	                RadioButton radioButton1 = (RadioButton) findViewById(R.id.Male);  
	                RadioButton radioButton2 = (RadioButton) findViewById(R.id.Female);  
	                boolean isChecked1 = radioButton1.isChecked();  
	                boolean isChecked2 = radioButton2.isChecked();  
	                if (isChecked1) {  
	                    intent.putExtra("sex", "你是一名" + radioButton1.getText()  
	                            + "\n");  
	                    intent.putExtra("weight", "你的标准体重是" + Mweight + "千克\n");  
	                }  
	                if (isChecked2) {  
	                    intent.putExtra("sex", "你是一名" + radioButton2.getText()  
	                            + "\n");  
	                    intent.putExtra("weight", "你的标准体重是" + Fweight + "千克\n");  
	                }  
	  
	                intent.putExtra("height", "你的身高是" + number + "厘米" + "\n");  
	                startActivity(intent);  
	  
	            }  
	        });  
	}
	 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.calculator, menu);
		return true;
	}

}

SHOWActivity.java

package com.test.standardweight;

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

public class SHOWActivity extends Activity {
	 private TextView textview;  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show);
		textview = (TextView) findViewById(R.id.textView1);  
        Intent intent = getIntent();  
        String sex = intent.getStringExtra("sex");  
        String height = intent.getStringExtra("height");  
        String weight = intent.getStringExtra("weight");  
        textview.setText(sex + height + 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.show, menu);
		return true;
	}

}

1) 掌握RadioButton组件的使用

2) Intent的使用

3) 掌握如何在不同Activity之间通过Bundle传递数据





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值