MainActivity.java
package com.example.a;
import javax.security.auth.PrivateCredentialPermission;
import android.app.Activity;
import android.content.DialogInterface;
//import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener; /*与"import android.content.DialogInterface.OnClickListener;"冲突*/
public class MainActivity extends Activity {
EditText shengao,tizhong;
Button jisuan,quxiao;
double height=0;
double weight=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
shengao=(EditText)findViewById(R.id.shengao);
tizhong=(EditText)findViewById(R.id.tizhong);
jisuan=(Button)findViewById(R.id.jisuan);
quxiao=(Button)findViewById(R.id.quxiao);
jisuan.setOnClickListener(new View.OnClickListener() { //导入"import android.view.View.OnClickListener;"后无需加"View"
@Override
public void onClick(View v) {
try{
if("".equals(shengao.getText().toString()))
//if("".equals(shengao.getText().toString().trim()))
//if(shengao.getText()==null || "".equals(shengao.getText().toString().trim()))
{
shengao.setError("身高不得为空");
}
if("".equals(shengao.getText().toString().trim()))
{
tizhong.setError("体重不能为空");
}
if("".equals(shengao.getText().toString()))
//if("".equals(shengao.getText().toString().trim()))
//if(shengao.getText()!=null && "".equals(shengao.getText().toString().trim()))
{
height=Double.parseDouble(shengao.getText().toString());
}
if(tizhong.getText()!=null && "".equals(tizhong.getText().toString().trim()))
{
weight=Double.parseDouble(tizhong.getText().toString());
}
//double bmi=weight*10000/height/height;
double bmi=weight/(height*height);
//Toast.makeText(MainActivity.this, ""+bmi, 10000);
if(bmi<18)
{
Toast.makeText(MainActivity.this, "您身材偏瘦, 请加强营养",3000).show();
}
else if(bmi<=25)
{
Toast.makeText(MainActivity.this, "您身材标准, 请继续保持",3000).show();
}
else
{
Toast.makeText(MainActivity.this, "您身材偏胖, 请加强锻炼",3000).show();
}
}
catch(NumberFormatException e)
{
Toast.makeText(MainActivity.this, "身高或体重必须是数字", 3000).show();
e.printStackTrace();
}
}
});
quxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shengao.setText("");
tizhong.setText("");
}
});
}
}
fragment_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高" />
<EditText
android:id="@+id/shengao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体重" />
<EditText
android:id="@+id/tizhong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/jisuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算" />
<Button
android:id="@+id/quxiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>