一、运行效果图
二、核心代码
1.MainActivity中的代码:
<pre class="java" name="code"> private RadioButton chose1;
private RadioButton chose2;
private EditText height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Group=(RadioGroup)findViewById(R.id.Radiogroup); */
chose1 = (RadioButton) findViewById(R.id.Button1);
chose2 = (RadioButton) findViewById(R.id.Button2);
height = (EditText) findViewById(R.id.editText1);
// Clickbutton=(Button)findViewById(R.id.Clickbutton);
/*
* Group.setOnCheckedChangeListener(new
* RadioGroup.OnCheckedChangeListener(){
*
* private String Sex;
*
* @Override public void onCheckedChanged(RadioGroup arg0, int a) { //
* TODO Auto-generated method stub switch(a){ case R.id.Button1:
* Sex="男";break; case R.id.Button2: Sex="女";break; default: break; } }
*
* } ); Clickbutton.setOnClickListener(new OnClickListener(){ private
* String sex;
*
* public void onClick(View view){ double
* tall=Double.parseDouble(height.getText().toString()); Intent
* intent=new Intent(); intent.setClass(getApplicationContext(),
* SecondActivity.class);
*
* Bundle bundle=new Bundle(); bundle.putString("sex",sex);
* bundle.putDouble("tall",tall); intent.putExtras(bundle);
* startActivity(intent); } });
*/
}
public void computing(View view) {
String sex;
if (chose1.isChecked())
sex = chose1.getText().toString();
else
sex = chose2.getText().toString();
double tall = Double.parseDouble(height.getText().toString());
Intent intent = new Intent();
intent.setClass(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("sex", sex);
bundle.putDouble("tall", tall);
intent.putExtras(bundle);
startActivity(intent);
}
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;
}
}
2.ShowActivity中的代码:
private TextView showResult;
String sex;
// private String height;
double tall;
double weight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
/*
* showResult = (TextView) findViewById(R.id.textView1); intent =
* getIntent(); Bundle bundle = intent.getExtras(); sex =
* bundle.getString("sex"); height = bundle.getString("height"); double
* weight = bundle.getDouble("weight"); showResult.setTag("您是一位" + sex +
* ",\n您的身高为" + height + "厘米\n逼得标准体重为" + weight + "公斤");
*/
getDate();
initViews();
bindData();
}
private void getDate() {
// TODO Auto-generated method stub
Bundle bundle = getIntent().getExtras();
sex = bundle.getString("sex");
tall = bundle.getDouble("tall");
if (sex.equals("男")) {
weight = (tall - 80) * 0.7;
} else {
weight = (tall - 70) * 0.6;
}
}
private void initViews() {
// TODO Auto-generated method stub
showResult = (TextView) findViewById(R.id.showResult);
}
private void bindData() {
// TODO Auto-generated method stub
showResult.setText("您是一位" + sex + ",\n您的身高为" + tall + "厘米\n逼得标准体重为"
+ weight + "kg");
}
/*
* public void back(View view) { Intent intent = new Intent();
* intent.putExtra("Sex", sex);
* System.out.println(intent.getStringExtra("Sex"));
* intent.putExtra("height", height); intent.setClass(this,
* MainActivity.class); startActivity(intent); }
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
3.activity_main.xml中的代码 :
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/height"
android:layout_toRightOf="@+id/height"
android:ems="5"
android:inputType="text" />
<Button
android:id="@+id/Clickbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="computing"
android:text="@string/click" />
<RadioGroup
android:id="@+id/Radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_alignRight="@+id/Clickbutton"
android:layout_marginRight="20dp" >
<RadioButton
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chose2" />
<RadioButton
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chose1" />
</RadioGroup>
<TextView
android:id="@+id/height"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Clickbutton"
android:layout_alignLeft="@+id/sex"
android:layout_marginBottom="28dp"
android:text="@string/height" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Radiogroup"
android:layout_centerHorizontal="true"
android:text="@string/computing" />
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/Radiogroup"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:layout_toLeftOf="@+id/textView1"
android:text="@string/sex" />
三、遇到的问题
刚开始做的时候程序总是不能运行,异常退出,logcat中显示出空指针的问题