找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 3398|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

安卓體重計(jì)算器java源程序 使用Intent在Activity間傳輸數(shù)據(jù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:856808 發(fā)表于 2020-12-15 20:25 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 dori 于 2020-12-18 23:10 編輯

完成一個(gè)體重計(jì)算器的應(yīng)用程序開發(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)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表