自己做的一个安卓小应用

适合安卓初学者

身高计算器

下载链接

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.lee.height.MainActivity">

    <LinearLayout
        android:layout_width="344dp"
        android:layout_height="495dp"
        android:orientation="vertical"
        android:weightSum="1"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="0dp">

        <EditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text=""
            tools:layout_editor_absoluteX="-56dp"
            tools:layout_editor_absoluteY="117dp" />

        <EditText
            android:id="@+id/et2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="" />

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/rbboy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男" />

            <RadioButton
                android:id="@+id/rbgirl"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女" />

        </RadioGroup>

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="计算" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>
import android.icu.text.DecimalFormat;
import android.icu.text.NumberFormat;
import android.os.Build;
import android.app.Activity;
import android.renderscript.Double2;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText et1,et2;
    private Button button;
    private RadioButton rbboy,rbgirl;
    String sex = " ";
    double dadayheight,momheight;




    @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        createView();//创建视图函数
        sexchoose();
        setListener();


    }

    public void createView(){
        button = (Button)findViewById(R.id.button);
        et1 = (EditText)findViewById(R.id.et1);
        et2 = (EditText)findViewById(R.id.et2);
        rbboy = (RadioButton)findViewById(R.id.rbboy);
        rbgirl = (RadioButton)findViewById(R.id.rbgirl);//拿控件
    }

   private String sexchoose(){
       if(rbboy.isChecked()){
           sex = "男性";
       }else if(rbgirl.isChecked()){
           sex = "女性";
       }
       return sex;
   }//性别选择

    @RequiresApi(api = Build.VERSION_CODES.N)
    private String calculate(double num){
        NumberFormat cal = new DecimalFormat("0.00");
        String str = cal.format(num);
        return str;
    }//身高格式化输出函数

    private String getHeight(String sex,double daddyheight,double momheight){
        dadayheight = Double.parseDouble(et1.getText().toString());
        momheight = Double.parseDouble(et2.getText().toString());
        String height = " ";
        if(sex.equals("男性")){
            height = calculate(((dadayheight+momheight)/2)*1.08);
        }
        else{
            height = calculate((daddyheight*0.92+momheight)/2);
        }
        return height;
    }//身高计算函数

    private void setListener(){
        button.setOnClickListener(countListener);

    }

    private OnClickListener countListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"身高"+getHeight(sexchoose(),dadayheight,momheight)+"厘米",Toast.LENGTH_LONG).show();
        }
    };

}


猜你喜欢

转载自blog.csdn.net/weixin_40567229/article/details/78498599