標(biāo)題: 安卓體重計(jì)算器java源程序 使用Intent在Activity間傳輸數(shù)據(jù) [打印本頁(yè)]

作者: dori    時(shí)間: 2020-12-15 20:25
標(biāo)題: 安卓體重計(jì)算器java源程序 使用Intent在Activity間傳輸數(shù)據(jù)
本帖最后由 dori 于 2020-12-18 23:10 編輯

完成一個(gè)體重計(jì)算器的應(yīng)用程序開(kāi)發(fā)。
           

MainActivity文件:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bn=findViewById(R.id.bn);
        final RadioGroup male=findViewById(R.id.male);

        //綁定監(jiān)視器
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et = findViewById(R.id.editText);
                Boolean sex = male.getCheckedRadioButtonId()==R.id.rb1?true:false;//true為男,false為女
                String height = et.getText().toString();
                //創(chuàng)建Intent
                Intent intent = new Intent(MainActivity.this, Activity.class);
                intent.putExtra("sex", sex);
                intent.putExtra("height", height);
                startActivity(intent);
            }
        });
    }
}
Activity.java文件:

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);

        TextView tv1 = findViewById(R.id.gender);
        TextView tv2 = findViewById(R.id.height);
        TextView tv3 = findViewById(R.id.weight);

        //獲得intent
        Intent intent = getIntent();
        //獲得性別和身高信息
        Boolean sex =intent.getBooleanExtra("sex", true);
        Integer height = Integer.parseInt(intent.getStringExtra("height"));
        if (sex) //男
        {
            tv1.setText("你是一名男性");
            tv2.setText("你的身高是" + height + "厘米");
            Double weight = (height - 80) * 0.7;
            tv3.setText("你的標(biāo)準(zhǔn)體重是" + weight + "公斤");
        } else {
            tv1.setText("你是一名女性");
            tv2.setText("你的身高是" + height + "厘米");
            Double weight = (height - 70) * 0.6;
            tv3.setText("你的標(biāo)準(zhǔn)體重是" + weight + "公斤");
        }
    }
}
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">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="-148dp">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="40dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="計(jì)算標(biāo)準(zhǔn)體重"
            android:textSize="30sp" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="73dp"
                android:layout_height="wrap_content"
                android:text="性別:"
                android:textSize="20sp" />

            <RadioGroup
                android:id="@+id/male"
                android:layout_width="337dp"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="142dp"
                    android:layout_height="match_parent"
                    android:text="男性"
                    android:textSize="20sp" />

                <RadioButton
                    android:id="@+id/rb2"
                    android:layout_width="147dp"
                    android:layout_height="wrap_content"
                    android:text="女性"
                    android:textSize="20sp" />

            </RadioGroup>
        </TableRow>

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

            <TextView
                android:id="@+id/height"
                android:layout_width="73dp"
                android:layout_height="wrap_content"
                android:text="身高:"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="175dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="number"
                android:textColor="#E91E63" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="72dp"
                android:layout_height="wrap_content"
                android:text="cm"
                android:textSize="20sp" />
        </TableRow>

        <TableRow
            android:layout_width="400dp"
            android:layout_height="60dp">

            <TextView
                android:id="@+id/textView6"
                android:layout_width="314dp"
                android:layout_height="wrap_content" />

            <Button
                android:id="@+id/bn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="計(jì)算"
                android:textSize="25sp" />
        </TableRow>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
result.xml文件:

<?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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView10"
        android:layout_width="match_parent"
        android:layout_height="158dp" />

    <TextView
        android:id="@+id/gender"
        android:layout_width="match_parent"
        android:layout_height="52dp" />

    <TextView
        android:id="@+id/height"
        android:layout_width="match_parent"
        android:layout_height="52dp" />

    <TextView
        android:id="@+id/weight"
        android:layout_width="match_parent"
        android:layout_height="52dp" />
</LinearLayout>

使用Intent在Activity間傳輸數(shù)據(jù).rar (8.52 MB, 下載次數(shù): 5)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1