下面是一个简单的计算标准体重的实例,选择自己的性别,再输入自己的身高,点击Button就能在Toast显示自己的标准体重,看看自己的体重有没有符合标准哦。
计算标准体重的方法:
男性:(身高cm-80)×70﹪=标准体重 女性:(身高cm-70)×60﹪=标准体重
BMIActivity.java
packagecom.lingdududu.bmi; importjava.text.DecimalFormat; importjava.text.NumberFormat; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.RadioButton; importandroid.widget.Toast; /* *@authorlingdududu*该程序的功能是用户选择自己的性别和输入自己的身高,然后点击按钮,就能在Toast显示出自己的标准体重 */ publicclassBMIActivityextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ privateButtoncountButton; privateEditTextheighText; privateRadioButtonmaleBtn,femaleBtn; Stringsex=""; doubleheight; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); //调用创建视图的函数 creadView(); //调用性别选择的函数 sexChoose(); //调用Button注册监听器的函数 setListener(); } //响应Button事件的函数 privatevoidsetListener(){ countButton.setOnClickListener(countListner); } privateOnClickListenercountListner=newOnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub Toast.makeText(BMIActivity.this,"你是一位"+sexChoose()+"\n" +"你的身高为"+Double.parseDouble(heighText.getText().toString())+"cm" +"\n你的标准体重为"+getWeight(sexChoose(),height)+"kg",Toast.LENGTH_LONG) .show(); } }; //性别选择的函数 privateStringsexChoose(){ if(maleBtn.isChecked()){ sex="男性"; } elseif(femaleBtn.isChecked()){ sex="女性"; } returnsex; } //创建视图的函数 publicvoidcreadView(){ //txt=(TextView)findViewById(R.id.txt); countButton=(Button)findViewById(R.id.btn); heighText=(EditText)findViewById(R.id.etx); maleBtn=(RadioButton)findViewById(R.id.male); femaleBtn=(RadioButton)findViewById(R.id.female); //txt.setBackgroundResource(R.drawable.bg); } //标准体重格式化输出的函数 privateStringformat(doublenum){ NumberFormatformatter=newDecimalFormat("0.00"); Stringstr=formatter.format(num); returnstr; } //得到标准体重的函数 privateStringgetWeight(Stringsex,doubleheight){ height=Double.parseDouble(heighText.getText().toString()); Stringweight=""; if(sex.equals("男性")){ weight=format((height-80)*0.7); } else{ weight=format((height-70)*0.6); } returnweight; } }
main.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/pic" > <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" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/sex" /> <RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <RadioButton android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> <TextView android:layout_width="fill_parent" android:layout_height="26px" android:text="@string/heigh" /> <EditText android:id="@+id/etx" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/count" /> </LinearLayout>
效果图:
本文转自 lingdududu 51CTO博客,原文链接:
/liangruijun/700077