一 实验结果图
二 关键代码
<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=".MawActivity" >
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/hello"
android:textSize="16px" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</RadioGroup>
<EditText
android:id="@+id/etx"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn"
android:layout_alignLeft="@+id/male"
android:layout_marginBottom="37dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etx"
android:layout_alignParentBottom="true"
android:layout_marginBottom="57dp"
android:text="@string/count" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="53dp"
android:text="@string/sex" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="女" />
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/female"
android:text="男" />
<TextView
android:layout_width="fill_parent"
android:layout_height="26px"
android:layout_alignLeft="@+id/etx"
android:layout_centerVertical="true"
android:text="@string/heigh" />
</RelativeLayout>
package com.example.manandwoman;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.Activity;
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.Toast;
public class MawActivity extends Activity {
private Button countButton;
private EditText heighText;
private RadioButton maleBtn, femaleBtn;
String sex = "";
double height;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maw);
creadView();
sexChoose();
setListener();
}
private void setListener() {
countButton.setOnClickListener(countListner);
}
private OnClickListener countListner = new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MawActivity.this, "你是一位"+sexChoose()+"\n"
+"你的身高为"+Double.parseDouble(heighText.getText().toString())+"cm"
+"\n你的标准体重为"+getWeight(sexChoose(), height)+"kg", Toast.LENGTH_LONG)
.show();
}
};
private String sexChoose(){
if (maleBtn.isChecked()) {
sex = "男性";
}
else if(femaleBtn.isChecked()){
sex = "女性";
}
return sex;
}
public void creadView(){
countButton=(Button)findViewById(R.id.btn);
heighText=(EditText)findViewById(R.id.etx);
maleBtn=(RadioButton)findViewById(R.id.male);
femaleBtn=(RadioButton)findViewById(R.id.female);
}
private String format(double num) {
NumberFormat formatter = new DecimalFormat("0.00");
String str = formatter.format(num);
return str;
}
private String getWeight(String sex, double height) {
height = Double.parseDouble(heighText.getText().toString());
String weight = "";
if (sex.equals("男性")) {
weight =format((height - 80) * 0.7);
}
else {
weight = format((height - 70) * 0.6);
}
return weight;
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Manandwoman</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="hello">标准体重计算器</string>
<string name="sex">选择性别</string>
<string name="heigh">输入身高</string>
<string name="count">计算</string>
</resources>
三 遇到的问题
R.id.