2021年3月7日 体温上报app开发01

时间:大约4个多小时

代码:400行左右

博客:1

知识点: haredPreferences

体温上报APP的登录、注册页面和功能和haredPreferences的使用:

package bjfu.it.sun.tem;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import bjfu.it.sun.tem.model.dao.UserAccountDao;
import bjfu.it.sun.tem.util.SPDDataUtils;

//登录界面
public class dengActivity extends AppCompatActivity implements View.OnClickListener  {
    private String val;//用来做接收


    private Button btn_deng,btn_cancel;
    private EditText et_phone;
    private UserAccountDao userAccountDao ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deng);
        val=getIntent() .getStringExtra("flag") ;//接收完成

        initView();//初始化控件

    }


    //加载初始化数据
    private void initView() {
        btn_deng=findViewById(R.id.deng_submit) ;
        et_phone=findViewById(R.id.deng_edit_phone) ;
        btn_cancel=findViewById(R.id.deng_cancel) ;
        btn_deng.setOnClickListener(this);
        btn_cancel.setOnClickListener(this) ;
    }

    //登录页面的逻辑处理
    private void deng(){
        userAccountDao=new UserAccountDao(this) ;
        //1 获取输入的手机号
        final String dengphone=et_phone.getText() .toString() ;

        //2 对输入的进行校验(判空)
        if(TextUtils .isEmpty(dengphone) ){
            showMSg("请输入手机号");
        }
        //3 登录逻辑处理
        if(userAccountDao.getAccountByPhone(dengphone) !=null){
            boolean flag= SPDDataUtils.saveUserInfo(this,dengphone) ;
            if(flag ){
                showMSg("登录成功");
                Intent i=new Intent(this,LoginActivity .class) ;
                startActivity(i);
            }else{
                showMSg("登录失败");
            }
        }else{
            showMSg("请先注册");
            Intent i=new Intent(this,zhuceActivity .class) ;
            startActivity(i);
        }
    }
    //点击事件处理
    @Override
    public void onClick(View v) {
        Intent i;
        switch (v.getId() ){
            case R.id.deng_submit ://登录
                deng();
                //chaAndSaveData();//检查登录数据并进行保存

                break;
            case R.id.deng_cancel :
                finish() ;
        }
    }

    /**
     * 检查登录数据并进行保存
     */
    /*
    private void chaAndSaveData() {
        String phone = et_phone.getText().toString().trim();
        //处理
        if (TextUtils.isEmpty(phone)) {
            showMSg("请输入电话信息") ;
        }else{
            boolean flag=SPDDataUtils .saveUserInfo(this,phone) ;
            if(flag ){
                showMSg("保存数据成功");
            }else{
                showMSg("保存数据失败");
            }
        }
    }
    */

    //显示信息的方法
    private void showMSg(String msg){
        Toast .makeText(this,msg,Toast.LENGTH_SHORT ).show() ;
    }
}

登录功能:

package bjfu.it.sun.tem;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import bjfu.it.sun.tem.model.dao.UserAccountDao;
import bjfu.it.sun.tem.util.SPDDataUtils;

//登录界面
public class dengActivity extends AppCompatActivity implements View.OnClickListener  {
    private String val;//用来做接收


    private Button btn_deng,btn_cancel;
    private EditText et_phone;
    private UserAccountDao userAccountDao ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deng);
        val=getIntent() .getStringExtra("flag") ;//接收完成

        initView();//初始化控件

    }


    //加载初始化数据
    private void initView() {
        btn_deng=findViewById(R.id.deng_submit) ;
        et_phone=findViewById(R.id.deng_edit_phone) ;
        btn_cancel=findViewById(R.id.deng_cancel) ;
        btn_deng.setOnClickListener(this);
        btn_cancel.setOnClickListener(this) ;
    }

    //登录页面的逻辑处理
    private void deng(){
        userAccountDao=new UserAccountDao(this) ;
        //1 获取输入的手机号
        final String dengphone=et_phone.getText() .toString() ;

        //2 对输入的进行校验(判空)
        if(TextUtils .isEmpty(dengphone) ){
            showMSg("请输入手机号");
        }
        //3 登录逻辑处理
        if(userAccountDao.getAccountByPhone(dengphone) !=null){
            boolean flag= SPDDataUtils.saveUserInfo(this,dengphone) ;
            if(flag ){
                showMSg("登录成功");
                Intent i=new Intent(this,LoginActivity .class) ;
                startActivity(i);
            }else{
                showMSg("登录失败");
            }
        }else{
            showMSg("请先注册");
            Intent i=new Intent(this,zhuceActivity .class) ;
            startActivity(i);
        }
    }
    //点击事件处理
    @Override
    public void onClick(View v) {
        Intent i;
        switch (v.getId() ){
            case R.id.deng_submit ://登录
                deng();
                //chaAndSaveData();//检查登录数据并进行保存

                break;
            case R.id.deng_cancel :
                finish() ;
        }
    }

    /**
     * 检查登录数据并进行保存
     */
    /*
    private void chaAndSaveData() {
        String phone = et_phone.getText().toString().trim();
        //处理
        if (TextUtils.isEmpty(phone)) {
            showMSg("请输入电话信息") ;
        }else{
            boolean flag=SPDDataUtils .saveUserInfo(this,phone) ;
            if(flag ){
                showMSg("保存数据成功");
            }else{
                showMSg("保存数据失败");
            }
        }
    }
    */

    //显示信息的方法
    private void showMSg(String msg){
        Toast .makeText(this,msg,Toast.LENGTH_SHORT ).show() ;
    }
}

登录页面:

<?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="wrap_content"
    android:layout_gravity="center"
    tools:context=".dengActivity">

    <LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/deng_phone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/deng_phone"
                android:textColor="#2779AF"
                android:textSize="30sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/deng_edit_phone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="@string/deng_edit_phone"
                android:inputType="textPersonName" />
        </LinearLayout>

        <Button
            android:id="@+id/deng_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/deng_bt_submit"
            app:iconTint="#4E1111" />

        <Button
            android:id="@+id/deng_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/deng_cancle" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

注册页面:

<?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=".zhuceActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/zhuce_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/zhuce_name"
                android:textSize="30sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/zhuce_edit_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="@string/zhuce_edit_hint"
                android:inputType="textPersonName" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="?android:attr/windowSwipeToDismiss"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/zhuce_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/zhuce_id"
                android:textSize="30sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/zhuce_edit_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="@string/zhuce_edit_id"
                android:inputType="textPersonName" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/zhuce_phone"
                android:layout_width="61dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/zhuce_phone"
                android:textSize="30sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/zhuce_edit_phone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="@string/zhuce_edit_phone"
                android:inputType="textPersonName" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/zhuce_class"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/zhuce_class"
                android:textSize="30sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/zhuce_edit_class"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:hint="@string/zhuce_edit_class"
                android:inputType="textPersonName" />

        </LinearLayout>

        <Button
            android:id="@+id/zhuce_zhu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/zhuce_bt_submit" />

        <Button
            android:id="@+id/zhuce_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/zhuce_cancle" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

SPDDataUtils:

package bjfu.it.sun.tem.util;

import android.content.Context;
import android.content.SharedPreferences;

//使用haredPreferences实现数据的存取的工具类
public class SPDDataUtils {
    private static final String mFileName="mydata";//文件名称
/**
 * 保存用户信息
 * @param context  上下文信息
 * @param phone 电话
 * @return boolean 结果
 */

//存放数据的方法
    public static boolean saveUserInfo(Context context ,String phone){
        boolean flag =false ;
        SharedPreferences sp=context .getSharedPreferences(mFileName,Context .MODE_PRIVATE) ;//Context .MODE_PRIVATE以私有方式打开
        SharedPreferences.Editor editor =sp.edit() ;
        editor.putString("phone",phone) ;//放进去
        editor.commit() ;//执行事务处理

        flag=true;
        return flag;

    }


    /**
     *获取用户信息的方法
     * @param context 调用的上下文
     * @return Userinfo 的实例
     */
    public static String getUserinfo(Context  context ){

        SharedPreferences sp=context .getSharedPreferences(mFileName,Context.MODE_PRIVATE );
        String uname=sp.getString("phone",null) ;

        return uname;
    }

}
package bjfu.it.sun.tem.util;

import android.content.Context;
import android.content.SharedPreferences;

//使用haredPreferences实现数据的存取的工具类
public class SPDDataUtils {
private static final String mFileName="mydata";//文件名称
/**
* 保存用户信息
* @param context 上下文信息
* @param phone 电话
* @return boolean 结果
*/

//存放数据的方法
public static boolean saveUserInfo(Context context ,String phone){
boolean flag =false ;
SharedPreferences sp=context .getSharedPreferences(mFileName,Context .MODE_PRIVATE) ;//Context .MODE_PRIVATE以私有方式打开
SharedPreferences.Editor editor =sp.edit() ;
editor.putString("phone",phone) ;//放进去
editor.commit() ;//执行事务处理

flag=true;
return flag;

}


/**
*获取用户信息的方法
* @param context 调用的上下文
* @return Userinfo 的实例
*/
public static String getUserinfo(Context context ){

SharedPreferences sp=context .getSharedPreferences(mFileName,Context.MODE_PRIVATE );
String uname=sp.getString("phone",null) ;

return uname;
}

}
原文地址:https://www.cnblogs.com/j-y-s/p/14902434.html