标准体重测试App,使用android studio,有重置和页面跳转
图片:
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="420dp"
android:layout_height="900dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#9999ff">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="标准体重计算"
android:textSize="60dp"
android:paddingTop="70dp"
android:paddingLeft="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/te3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textSize="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#87CEFA"
android:orientation="horizontal">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="30dp"
android:paddingTop="80dp"
android:text="性别:"
android:textColor="#ffffff"
android:textSize="40dp" />
<RadioGroup
android:id="@+id/Button4"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="男"
android:textSize="20dp"
android:textColor="#ffffff"/>
<RadioButton
android:id="@+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="女"
android:layout_weight="1"
android:textSize="20dp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#87CEFA">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="30dp"
android:text="身高:"
android:textColor="#ffffff"
android:textSize="40dp" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="输入你的身高"
android:textColor="#ADADAD"
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#87CEFA">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:text="计算" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="100dp"
android:text="重置" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2.layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="420dp"
android:layout_height="900dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#9999ff">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="结果显示"
android:textSize="60dp"
android:paddingTop="70dp"
android:paddingLeft="90dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textSize="3dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#87CEFA"
android:orientation="horizontal">
<TextView
android:id="@+id/JieGuo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="140dp"
android:layout_marginTop="100dp"
android:textSize="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#87CEFA">
<Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="返回" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3.MainAcitivity.java
public class MainActivity extends AppCompatActivity {
private Button button,btn;//2
private EditText heightText;
private RadioGroup rg;
private RadioButton rb0, rb1;
private String se = "b";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button2);
btn=(Button)findViewById(R.id.button10);
rg=(RadioGroup)this.findViewById(R.id.Button4);
heightText=(EditText)this.findViewById(R.id.editText);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId)
{
case R.id.radioButton4:
se="男";break;
case R.id.radioButton5:
se="女";break;
}
}
});
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//显示方式声明Intent,直接启动SecondActivity
String ht=heightText.getText().toString();
Intent intent=new Intent();
intent.setClass(MainActivity.this, Two.class);
//设置发送数据Activity和接收数据Activity
//ActivityFirst为发送数据的Activity,ActivitySecond.为接收数据的Activity
Bundle bundle = new Bundle(); //实例化一个Bundle对象,对象的名字叫做bundle
bundle.putString("xingbie", se);
bundle.putString("ht", ht);
intent.putExtras(bundle);
startActivity(intent);
}
});
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
heightText.setText(null);
rg.clearCheck();
Toast.makeText(getApplicationContext(),"重置成功",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getExtras().getString("result");//得到新Activity 关闭后返回的数据
}
}
4.Two
public class Two extends AppCompatActivity {
private TextView textview;
private Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
textview=(TextView)this.findViewById(R.id.JieGuo);
button=(Button) this.findViewById(R.id.back);
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String sex=bundle.getString("xingbie");
String height=bundle.getString("ht");
if(sex.equals("男"))
{
double tiZhong=((Integer.parseInt(height))-80)*0.7;
textview.setText("您是一名男性\n身高为:"+height+"\n标准体重为:"+tiZhong+"公斤。");
}
else if(sex.equals("女"))
{
double tiZhong=((Integer.parseInt(height))-70)*0.6;
textview.setText("您是一名女性\n身高为:"+height+"\n标准体重为:"+tiZhong+"公斤。");
}
else
{
textview.setText("没有数据");
}
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Two.this, MainActivity.class);
startActivity(intent);
Two.this.finish();
}
});
}
}