根据父母计算子女身高(用方法)

来源:互联网 发布:淘宝自定义模块间隙 编辑:程序博客网 时间:2024/04/29 02:24
布局篇
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"    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="com.example.height.MainActivity"    android:orientation="vertical">    <EditText        android:id="@+id/et_FatherHeight"        android:hint="请输入父亲身高:(cm)"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <EditText        android:id="@+id/et_MotherHeight"        android:hint="请输入母亲身高:(cm)"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:id="@+id/et_countHeight"        android:text="开始计算"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>
Java代码篇(调用方法 MainActivity)
package com.example.height;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity {        private String father;        private String mother;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);     //实例化控件        final EditText fatherheight= (EditText) findViewById(R.id.et_FatherHeight);        final EditText motherheight= (EditText) findViewById(R.id.et_MotherHeight);        Button countheight= (Button) findViewById(R.id.et_countHeight);        //给按钮添加事件        countheight.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                father=  fatherheight.getText().toString();                mother=motherheight.getText().toString();               intiHeight();            }        });    }    public void intiHeight(){        if(TextUtils.isEmpty(father)||TextUtils.isEmpty(mother)){            Toast.makeText(MainActivity.this,"请输入完整的父母身高!",Toast.LENGTH_SHORT).show();        }        try {            //获取输入内容            int f=Integer.parseInt(father);            int m=Integer.parseInt(mother);            int sonheight= (int) ((f+m*1.08)/2);            Toast.makeText(MainActivity.this,"儿子身高为:"+sonheight+"cm",Toast.LENGTH_LONG).show();        }catch (Exception e){            e.printStackTrace();            Toast.makeText(MainActivity.this,"身高必须是数字!",Toast.LENGTH_SHORT).show();        }    }}
设置权限篇(mainfest)
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.height">    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"        android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
0 0
原创粉丝点击