1、按照以下要求:
如果是男性,标准体重=(身高-80)*0.7
如果是女性,标准体重=(身高-70)*0.6
运行结果为:
阶段一:布局:
使用到的为线性布局:LinearLayout:
其中的属性为:
android:orientation="vertical"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bj"
android:orientation="vertical"
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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//wrap_content为包裹内容;fill_parent为填充整个父窗体
android:text="@string/Weight" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Sex" />
<RadioGroup
android:id="@+id/idSex"
android:layout_width="263dp"
android:layout_height="129dp" >
<RadioButton
android:id="@+id/idSex1"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:text="@string/Sex1" />
<RadioButton
android:id="@+id/idSex2"
android:layout_width="89dp"
android:layout_height="wrap_content"
android:text="@string/Sex2" />
</RadioGroup>
<TextView
android:id="@+id/idHeight"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:text="@string/Height" />
<EditText
android:id="@+id/shengao"
android:layout_width="254dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/caculation" />
</LinearLayout>
布局结果为:
阶段2、实现计算功能:
package cn.bzu.android.intent;
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;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
private EditText txtHight;
private Button clickButton;
private RadioGroup change;
private RadioButton rad1;
private RadioButton rad2;
private String sex;
private double i2;
private int i1;
//if(change!=null)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtHight=(EditText) findViewById(R.id.shengao);
clickButton=(Button) this.findViewById(R.id.button1);
change=(RadioGroup) findViewById(R.id.idSex);
rad1=(RadioButton) findViewById(R.id.idSex1);
rad2=(RadioButton) findViewById(R.id.idSex2);
//Intent intent
Intent intent=getIntent();
sex=intent.getStringExtra("Sex");
String Height=intent.getStringExtra("Height");
if(sex!=null && sex.equals("男") )
{
rad1.setChecked(true);
//rad1.setText(sex);
}
if(sex!=null && sex.equals("女") )
{
rad2.setChecked(true);
//rad2.setText(sex);
}
if(Height!=null)
{
txtHight.setText(Height);
}
change.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId==rad1.getId())
{
sex= rad1.getText().toString();
}
else
{
sex= rad2.getText().toString();
}
}
});
//button的clickListener的事件处理:
clickButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this, "你好",Toast.LENGTH_LONG).show();
String str=txtHight.getText().toString();
i1=Integer.parseInt(str);
if(sex.equals("男")){
i2= (i1-80)*0.7;
IntentBundle();
//Toast.makeText(MainActivity.this, "你是一名"+rad1.getText()+"性\n你的身高是"+str+"\n 你的标准体重为"+i2+"公斤",Toast.LENGTH_LONG ).show();
}
else
{
i2=(i1-70)*0.6;
IntentBundle();
//Toast.makeText(MainActivity.this, "你是一名"+rad2.getText()+"性\n 你的身高是"+str+"\n 你的标准体重为"+i2+"公斤",Toast.LENGTH_LONG ).show();
}
}
});
}
//自定义方法,进行两个应用程序之间的数据传递
private void IntentBundle(){
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString("Sex", sex);
bundle.putInt("Height", i1);
bundle.putDouble("Weight", i2);
intent.putExtras(bundle);
intent.setClass(MainActivity.this, SecondActivity.class);
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.main, menu);
return true;
}
}
阶段3、在SecondActivity中 进行数据的接受处理:
package cn.bzu.android.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView show;
private Button but;
private String sex;
private int height;
private Double weight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//数据的接受
show=(TextView) this.findViewById(R.id.show);
but=(Button) findViewById(R.id.button1);
Bundle bundle=this.getIntent().getExtras();
sex=bundle.getString("Sex");
height=bundle.getInt("Height");
weight=bundle.getDouble("Weight");
show.setText("您是一名"+sex+"\n您的身高为: "+height+"厘米\n您的标准体重为: "+weight+"公斤");
but.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
//Bundle bundle=new Bundle();
intent.setClass(SecondActivity.this, MainActivity.class);
intent.putExtra("Sex",sex);
intent.putExtra("Height",height+"");
startActivity(intent);
//finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
阶段4、设置应用程序标题:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.bzu.android.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="7" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/Weight"
android:theme="@style/AppTheme" >
<activity android:name="cn.bzu.android.intent.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/second" >
</activity>
</application>
</manifest>
运行结果为: