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.Toast;public class BMIActivity extends Activity {
/** Called when the activity is first created. */
public String str1;
public String str2;
public Double heigth;
public Double weigth;
public Double result;
public Toast toast;
private EditText edittext1;
private EditText edittext2;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edittext1 = (EditText)findViewById(R.id.editheigth);
edittext2 = (EditText)findViewById(R.id.editwigth);
button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new KeyListen());
}
class KeyListen implements OnClickListener{ public void onClick(View e) {
if(e.getId() == R.id.btn){
str1 = edittext1.getText().toString();
str2 = edittext2.getText().toString();
heigth = Double.parseDouble(str1);
weigth = Double.parseDouble(str2);
result = weigth/(weigth*heigth);
toast = new Toast(BMIActivity.this);
toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_SHORT);
toast.show();
}
}
}
}
package com.java.wjd;<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/widget46"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/widget50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入身高"
>
</TextView>
<EditText
android:id="@+id/editheigth"
android:layout_width="95px"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<TextView
android:id="@+id/widget51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入体重"
>
</TextView>
<EditText
android:id="@+id/editwigth"
android:layout_width="97px"
android:layout_height="wrap_content"
android:textSize="18sp"
>
</EditText>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
>
</Button>
</LinearLayout>
这个程序错哪啊
//toast.show();
……
toast = new Toast(BMIActivity.this);
toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_SHORT);
toast.show();
直接用这句代替就可以了
Toast.makeText(BMIActivity.this, result.toString(), Toast.LENGTH_SHORT).show();