activity_main.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="com.text.weight.MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"
android:text="@string/word1"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_marginTop="95dp"
android:text="@string/word2" />
<RadioGroup
android:id="@+id/grop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/text2"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rbmale" />
<RadioButton
android:id="@+id/rButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rbfemale" />
</RadioGroup>
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_marginTop="90dp"
android:text="@string/word3"/>
<EditText
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/grop"
android:layout_alignBottom="@+id/text3"
android:hint="@string/input" >
<requestFocus/>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:text="@string/b3" />
</RelativeLayout>
activity_show.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:text="" />
</RelativeLayout>
MainActivity.java
package com.text.weight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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 Button button1;
private EditText test4;
private RadioGroup sex;
private RadioButton choose;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test4 = (EditText) findViewById(R.id.text4);
sex =(RadioGroup)findViewById(R.id.grop);
sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup grop, int checkedID) {
choose=(RadioButton) findViewById(sex.getCheckedRadioButtonId());
Sex=choose.getText().toString();
}
});
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new ClickHandler());
}
class ClickHandler implements OnClickListener{
public void onClick(View view)
{
Intent intent = new Intent();
intent.setClass(getApplicationContext(), ShowActivity.class);
Bundle bundle = new Bundle();
String High =test4.getText().toString();
bundle.putString("high", High);
bundle.putString("sex",Sex);
intent.putExtras(bundle);
startActivity(intent);
}
}}
ShowActivity.java
package com.text.weight;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ShowActivity extends Activity {
private TextView tvshow;
float weight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
float weight;
Bundle bundle=this.getIntent().getExtras();
String sex=bundle.getString("sex");
String high=bundle.getString("high");
float High=Float.valueOf(high).floatValue();
if(sex=="男")
weight=(float)((High-80)*0.7);
else
weight=(float)((High-70)*0.6);
tvshow=(TextView)findViewById(R.id.tvshow);
tvshow.setText("你是一位"+sex+"性\n你的身高是"+high+"厘米\n你的标准体重是"+weight+"公斤\n");
}
}
出现的问题:
数据类型的转换:bundle传送的是string,而需要的是float类型的数据,所以一开始的时候运行出来的结果是身高为0,体重为负数。