- package ray.work.bmicalc;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener{
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Button btn = (Button)findViewById(R.id.buttonCalc);
- //View btn = findViewById(R.id.buttonCalc);
- //btn.setOnClickListener(this);
- //btn = (Button)findViewById(R.id.buttonSugg);
- //btn.setOnClickListener(this);
- findViewById(R.id.buttonCalc).setOnClickListener(this);
- findViewById(R.id.buttonSugg).setOnClickListener(this);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- public void onClick(View v)
- {
- // TODO Auto-generated method stub
- //Toast.makeText(this,"hello",Toast.LENGTH_SHORT).show();
- try
- {
- float h = Float.parseFloat(((EditText)this.findViewById(R.id.heightText)).getText().toString());
- float w = Float.parseFloat(((EditText)this.findViewById(R.id.weightText)).getText().toString());
- boolean s = ((RadioButton)this.findViewById(R.id.selMale)).isChecked();
- BMIManager myBMI = new BMIManager(h,w,s);
- if(v.getId() == R.id.buttonCalc)
- {
- ((TextView)this.findViewById(R.id.suggestionStr)).setText("BMI="+myBMI.calculateBMI());
- }
- if(v.getId() == R.id.buttonSugg)
- {
- ((TextView)this.findViewById(R.id.suggestionStr)).setText(myBMI.suggestion());
- }
- }
- catch(Exception e)
- {
- Toast.makeText(this,this.getResources().getString(R.string.errInput),Toast.LENGTH_SHORT).show();
- }
- }
- }
複製代碼 |