运行效果图
MainActivity.java的全部代码
package cn.edu.example.b_2_2_tizhong;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity
{
private Button button1;
private EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.bu1);
edittext = (EditText) findViewById(R.id.editText1);
button1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String a = edittext.getText().toString();
int b = Integer.parseInt(a);
double b1 = (b - 80) * 0.7;
double b2 = (b - 70) * 0.6;
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
RadioButton rb1 = (RadioButton) findViewById(R.id.rB1);
RadioButton rb2 = (RadioButton) findViewById(R.id.rB2);
boolean isChecked1 = rb1.isChecked();
boolean isChecked2 = rb2.isChecked();
if (isChecked1)
{
intent.putExtra("xingbie", "你是一名" + rb1.getText() + "性" + "\n");
intent.putExtra("tizhong", "你的标准体重是" + b1 + "公斤");
}
if (isChecked2)
{
intent.putExtra("xingbie", "你是一名" + rb2.getText() + "性");
intent.putExtra("tizhong", "你的标准体重是" + b2 + "公斤");
}
intent.putExtra("shengao", "你的身高是" + a + "厘米" + "\n");
startActivity(intent);
}
});
}
}
新建class命名为TwoActivity.java,其代码为:
package cn.edu.example.b_2_2_tizhong;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class TwoActivity extends MainActivity
{
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
textView = (TextView) this.findViewById(R.id.tV2_1);
Intent intent = getIntent();
String xingbie = intent.getStringExtra("xingbie");
String shengao = intent.getStringExtra("shengao");
String tizhong = intent.getStringExtra("tizhong");
textView.setText(xingbie+shengao+tizhong);
}
}