WeightTest

来源:互联网 发布:淘宝搜索词查询软件 编辑:程序博客网 时间:2024/06/10 02:05

1.运行效果图

2.实验代码

<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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".FirstActivity" >    <TextView        android:id="@+id/input"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true"        android:layout_marginTop="50dp"        android:text="计算你的标准体重" />    <RadioGroup        android:id="@+id/sex"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/input"        android:layout_marginLeft="90dp"        android:layout_marginTop="140dp"        android:orientation="horizontal" >    </RadioGroup>    <TextView        android:id="@+id/sex"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/input"        android:layout_marginTop="69dp"        android:text="性别:" />    <Button        android:id="@+id/btClick"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/height"        android:layout_centerHorizontal="true"        android:layout_marginTop="46dp"        android:onClick="caculate"        android:text="计算" />    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/height"        android:layout_alignBottom="@+id/height"        android:layout_toRightOf="@+id/sex"        android:ems="10" />    <RadioButton        android:id="@+id/man"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/sex"        android:layout_alignBottom="@+id/sex"        android:layout_toLeftOf="@+id/btClick"        android:text="男" />    <RadioButton        android:id="@+id/woman"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/man"        android:layout_alignBottom="@+id/man"        android:layout_alignRight="@+id/input"        android:text="女" />    <TextView        android:id="@+id/height"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/sex"        android:layout_centerVertical="true"        android:text="身高:" /></RelativeLayout>


 

package cn.edu.bzu.weighttest;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.RadioButton;import android.widget.TextView;public class FirstActivity extends Activity {private RadioButton man;private RadioButton woman;private TextView height;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_first);                        man=(RadioButton)findViewById(R.id.man);               woman=(RadioButton)findViewById(R.id.woman);        height=(TextView)findViewById(R.id.editText1);    }public void caculate(View view){String sex;double Height= Double.parseDouble(height.getText().toString());if(man.isChecked())sex=man.getText().toString();elsesex=woman.getText().toString();Intent intent=new Intent();intent.setClass(FirstActivity.this, SecondActivity.class);Bundle bundle=new Bundle();bundle.putString("sex", sex);bundle.putDouble("Height",Height);intent.putExtras(bundle);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.first, menu);        return true;    }    }


 

package cn.edu.bzu.weighttest;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class SecondActivity extends Activity {private TextView showtall;String sex;double Height;double weight;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);getDate();initViews();bindData();}private void getDate() {Bundle bundle = getIntent().getExtras();sex = bundle.getString("sex");Height = bundle.getDouble("Height");if (sex.equals("男"))weight = (Height - 80) * 0.7;elseweight = (Height - 70) * 0.6;}private void initViews() {showtall = (TextView) findViewById(R.id.showtall);}private void bindData() {showtall.setText("你是一位" + sex + "性" + "\n你的身高是" + Height + "\n你的标准体重是"+ weight + "千克");}@Overridepublic 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;}}


 

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cn.edu.bzu.weighttest"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="18" />      <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>          <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>         <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="cn.edu.bzu.weighttest.FirstActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity            android:name="cn.edu.bzu.weighttest.SecondActivity"            android:label="@string/title_activity_second" >        </activity>    </application></manifest>


3.实验结果和问题

     在本次实验中,了解了RadioButton的使用,再写的过程中,有很多要定义的,而且在第一个活动中的布局总是乱。在运行中,效果图中第一个Activity跳不到第二个,就停止了,让同学帮忙看了一下,解决了问题。感觉学到很多,受益匪浅。

0 0
原创粉丝点击