实验二 Intent 简单标准体重计算器

一、实验目的

(1)掌握RadioButton组件的使用

(2)Intent的使用

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

二、实验内容

    编写一个简单标准体重计算器应用,实现标准体重的计算。用户可选择性别,填写身高,点击计算后会在另一个界面显示用户的性别、身高、标准体重。

三、实验结果图

实验二 Intent 简单标准体重计算器_第1张图片实验二 Intent 简单标准体重计算器_第2张图片

四、实验代码

MainActivity:

package com.test.weightcalculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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;
import android.widget.RadioGroup;

public class MainActivity extends Activity {
	
	String Sex;
	private RadioGroup sex;
	private EditText height;
	private RadioButton sex_choose;
	private Button calculator;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sex = (RadioGroup) findViewById(R.id.rg);
		
		sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				sex_choose = (RadioButton)findViewById(sex.getCheckedRadioButtonId());
				Sex = sex_choose.getText().toString();
			}
		});
		
		height = (EditText) findViewById(R.id.et1);
		calculator = (Button) findViewById(R.id.btn1);
		calculator.setOnClickListener(new ClickHandler());
		
	}

	class ClickHandler implements OnClickListener
	{
		@Override
		public void onClick(View view)
		{
			String Height = height.getText().toString();
			Intent intent=new Intent();
			intent.setClass(getApplicationContext(), CalculatorActivity.class);			
			Bundle bundle=new Bundle();
			bundle.putString("height", Height);
			bundle.putString("sex", Sex);
			intent.putExtras(bundle);
			startActivity(intent);			
		}
		
	}
	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;
	}
}
CalculatorActivity:

package com.test.weightcalculator;

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

public class CalculatorActivity extends Activity {

	double weight;
	private TextView tvSec;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_calculator);
		tvSec=(TextView) findViewById(R.id.tvSec);		
		Bundle bundle=getIntent().getExtras();
	        String height=bundle.getString("height");
		String sex=bundle.getString("sex");
		double Height=Double.valueOf(height).doubleValue();
		if(sex=="女性")
			weight = (Height - 70) * 0.6;
		else
			weight = (Height - 80) * 0.7;
		tvSec.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.calculator, 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);
	}
}
activity_main.xml:



    

    

    

        

        
    

    
activity_calculator.xml:



    

五、存在的问题

    EditText设置为只能写入数字,但是,在利用bundle传递数据时,不知道该怎么从中提取数字数据,所以在第二个Activity中用了String转换double的办法。



PS:以上的所有方法会在自学笔记中详细介绍

你可能感兴趣的:(实验课)