一.项目运行介绍
二.具体实现
BMI.java
package com.example.fittnnessapp.controller;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import com.example.fittnnessapp.R;
public class BMI extends AppCompatActivity {
private TextView bmi_txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
bmi_txt = findViewById(R.id.bmi_txt);
SharedPreferences sharedPreferences = getSharedPreferences("weight_pref",MODE_PRIVATE);
String bmi = sharedPreferences.getString("bmi","");
bmi_txt.setText("BMI = " + bmi);
}
}
这是一个Android应用程序中的BMI
(Body Mass Index,身体质量指数)活动的Java源代码,它用于显示用户的BMI值。下面是对代码的详细解释:
-
导入必要的类和包:
import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.TextView; import com.example.fittnnessapp.R;
这些导入语句用于引入Android应用程序所需的类和包。
-
创建
BMI
类并扩展AppCompatActivity
:public class BMI extends AppCompatActivity {
这是一个名为
BMI
的Java类,它扩展了AppCompatActivity
类,这是Android应用程序中活动的基本类。 -
声明类的私有成员变量:
private TextView bmi_txt;
在
BMI
类中,声明了一个名为bmi_txt
的私有成员变量,它是一个TextView
,用于在界面上显示BMI值。 -
在
onCreate
方法中设置布局和初始化界面元素:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bmi); bmi_txt = findViewById(R.id.bmi_txt); }
onCreate
方法是Android活动的生命周期方法之一,在活动创建时被调用。setContentView(R.layout.activity_bmi)
用于设置活动的布局,R.layout.activity_bmi
是XML布局文件的引用,它定义了界面的外观和布局。bmi_txt = findViewById(R.id.bmi_txt)
用于初始化bmi_txt
,它通过findViewById
方法从布局中找到与R.id.bmi_txt
关联的TextView
。
-
使用
SharedPreferences
获取用户的BMI值:SharedPreferences sharedPreferences = getSharedPreferences("weight_pref",MODE_PRIVATE); String bmi = sharedPreferences.getString("bmi","");
SharedPreferences
用于在Android应用程序中存储小量的数据,这里用于获取用户的BMI值。getSharedPreferences("weight_pref", MODE_PRIVATE)
获取名为 “weight_pref” 的共享偏好设置。“weight_pref” 是一个标识,用于唯一标识这些偏好设置。getString("bmi", "")
从共享偏好设置中获取名为 “bmi” 的字符串值,如果没有找到该值,则返回空字符串。
-
将BMI值显示在界面上:
扫描二维码关注公众号,回复: 16856335 查看本文章bmi_txt.setText("BMI = " + bmi);
这一行代码将获取的BMI值与字符串 "BMI = " 组合,并将结果设置为
bmi_txt
的文本内容,从而在界面上显示用户的BMI值。
总的来说,这段代码用于创建一个显示用户BMI值的Android应用程序界面。它首先获取存储在共享偏好设置中的BMI值,然后将其显示在界面上的 TextView
中。
sginup.java
package com.example.fittnnessapp.controller;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import com.example.fittnnessapp.R;
public class signUp extends AppCompatActivity {
private EditText userName;
private EditText userEmail;
private EditText userPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
userName = findViewById(R.id.et_newUserName);
userEmail = findViewById(R.id.et_userEmail);
userPass = findViewById(R.id.et_newUserPass);
}
//ON click
public void signUp(View view) {
SharedPreferences sharedPreferences = getSharedPreferences("signup_pref",MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("email_signup",userEmail.getText().toString());
editor.putString("username_signup",userName.getText().toString());
editor.putString("pass_signup",userPass.getText().toString());
editor.apply();
Intent intent = new Intent(this,home.class);
startActivity(intent);
}
}
这是一个Android应用程序中的signUp
(注册)活动的Java源代码,它用于用户注册并将注册信息保存在SharedPreferences中。下面是对代码的详细解释:
-
导入必要的类和包:
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.View; import android.widget.EditText; import com.example.fittnnessapp.R;
这些导入语句用于引入Android应用程序所需的类和包。
-
创建
signUp
类并扩展AppCompatActivity
:public class signUp extends AppCompatActivity {
这是一个名为
signUp
的Java类,它扩展了AppCompatActivity
类,这是Android应用程序中活动的基本类。 -
声明类的私有成员变量:
private EditText userName; private EditText userEmail; private EditText userPass;
在
signUp
类中,声明了三个私有成员变量,分别是userName
(用户姓名)、userEmail
(用户电子邮件)和userPass
(用户密码),它们用于在用户注册时输入相应的信息。 -
在
onCreate
方法中设置布局和初始化界面元素:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_up); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); userName = findViewById(R.id.et_newUserName); userEmail = findViewById(R.id.et_userEmail); userPass = findViewById(R.id.et_newUserPass); }
onCreate
方法是Android活动的生命周期方法之一,在活动创建时被调用。setContentView(R.layout.activity_sign_up)
用于设置活动的布局,R.layout.activity_sign_up
是XML布局文件的引用,它定义了注册界面的外观和布局。setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
用于强制活动保持纵向(竖屏)方向。
-
在
signUp
方法中处理用户注册:// ON click public void signUp(View view) { SharedPreferences sharedPreferences = getSharedPreferences("signup_pref",MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("email_signup",userEmail.getText().toString()); editor.putString("username_signup",userName.getText().toString()); editor.putString("pass_signup",userPass.getText().toString()); editor.apply(); Intent intent = new Intent(this,home.class); startActivity(intent); }
signUp
方法是一个由按钮点击事件触发的方法,它用于处理用户的注册操作。getSharedPreferences("signup_pref",MODE_PRIVATE)
获取名为 “signup_pref” 的共享偏好设置。“signup_pref” 是一个标识,用于唯一标识这些偏好设置。SharedPreferences.Editor editor = sharedPreferences.edit()
创建一个编辑器以编辑共享偏好设置。editor.putString("email_signup",userEmail.getText().toString())
将用户输入的电子邮件地址存储在共享偏好设置中,键为 “email_signup”。editor.putString("username_signup",userName.getText().toString())
将用户输入的用户名存储在共享偏好设置中,键为 “username_signup”。editor.putString("pass_signup",userPass.getText().toString())
将用户输入的密码存储在共享偏好设置中,键为 “pass_signup”。editor.apply()
应用编辑器中的更改,将用户注册信息保存到SharedPreferences中。Intent intent = new Intent(this,home.class)
创建一个意图,用于将用户从注册页面导航到主页(home
活动)。startActivity(intent)
启动主页活动,显示注册后的用户主页。
总的来说,这段代码用于创建一个用户注册界面,用户可以在该界面上输入用户名、电子邮件和密码,并通过按钮点击事件将注册信息保存在SharedPreferences中。注册成功后,用户将被导航到应用程序的主页。
activity_sign_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="144dp"
android:layout_height="142dp"
app:srcCompat="@drawable/profile" />
<EditText
android:id="@+id/et_userEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Enter your email"
android:inputType="textEmailAddress"
android:padding="15dp" />
<EditText
android:id="@+id/et_newUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Enter new user name"
android:inputType="textPersonName"
android:padding="15dp" />
<EditText
android:id="@+id/et_newUserPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:ems="10"
android:hint="Enter new password"
android:inputType="textPassword"
android:padding="15dp" />
<Button
android:id="@+id/btn_signUp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Sign up"
android:onClick="signUp"/>
</LinearLayout>
三.项目源码
链接:https://pan.baidu.com/s/16uNi9gpME_XkGQ6E7NjpiA?pwd=nljy
提取码:nljy
(可做实验报告,代码讲解等…)
请私信作者或
(v)15135757306