Android开发:标准身高计算器应用的开发实例,跟上例不一样
(2014-04-06 12:45:40)
标签:
it |
主程序:
package com.leansmall.heightcalculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private Button calculatorButton;
private EditText weightEditText;
private RadioButton manRadioButton;
private RadioButton womanRadioButton;
private TextView resultTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calculatorButton=(Button)findViewById(R.id.btGetResult);
weightEditText=(EditText)findViewById(R.id.etWeight);
manRadioButton=(RadioButton)findViewById(R.id.rbMan);
womanRadioButton=(RadioButton)findViewById(R.id.rbWoman);
}
protected void OnStart() {
registerEvent();
}
private void registerEvent() {
calculatorButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!weightEditText.getText().toString().trim().equals(""))
{
if(manRadioButton.isChecked()||womanRadioButton.isChecked())
{
Double
weight=Double.parseDouble(weightEditText.getText().toString());
StringBuffer sb=new StringBuffer();
sb.append("------评估结果------\n");
if(manRadioButton.isChecked())
{
sb.append("男性标准身高:");
double result=evaluateHeight(weight,"男");
sb.append((int)result+"厘米");
}
if(womanRadioButton.isChecked())
{
sb.append("女性标准身高:");
double result=evaluateHeight(weight,"女");
sb.append((int)result+"厘米");
}
resultTextView.setText(sb.toString());
}
else
{
showMessage("请选择性别");
}
}
else
{
showMessage("请输入体重");
}
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it
is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar
will
// automatically handle clicks on the Home/Up button, so
long
// as you specify a parent activity in
AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
mail.xml文件: