桃词典 Peach Dictionary 简易英语词典app开发 安卓软件开发 Part 5

前文:

桃词典 Peach Dictionary 简易英语词典app开发 安卓软件开发 Part 4

导航:

桃词典 Peach Dictionary 简易英语词典app开发 安卓软件开发 The End 导航页及收尾工作 

3.实现登录功能

(1)新建一个暂时用来验证登录成功的empty activity,这里我命名为temp(后期将会替换)。

(2)添加一个矢量图,与前面的对应,用来显示/隐藏密码。

ic_baseline_visibility_off_24

代码:

MainActivity.java

 package com.example.peachdictionary;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.VideoView;

 public class MainActivity extends AppCompatActivity {
     private ImageButton btn_login;//登录按钮
     private String userName, psw, spPsw;//获取的用户名,密码,加密密码
     private EditText et_user_name, et_psw;//编辑框

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         //设置此界面为竖屏
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
         videobackground();

     }

     private void videobackground() {

         //视频背景
         final VideoView videoview = findViewById(R.id.video_background);
         final String videopath = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.background).toString();
         videoview.setVideoPath(videopath);
         videoview.start();
         videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
             @Override
             public void onPrepared(MediaPlayer mp) {
                 mp.start();
                 mp.setLooping(true);
             }
         });

         videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
             @Override
             public void onCompletion(MediaPlayer mediaPlayer) {
                 videoview.setVideoPath(videopath);
                 videoview.start();
             }
         });

         Button button = findViewById(R.id.button);
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent = new Intent(MainActivity.this, register.class);
                 startActivity(intent);
             }
         });

         EditText edPassword = findViewById(R.id.editTextTextPassword);
         ImageButton imageButton = findViewById(R.id.imageButton);

         //密码的隐藏和显示
         imageButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 TransformationMethod type = edPassword.getTransformationMethod();
                 if (PasswordTransformationMethod.getInstance().equals(type)) {
                     edPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                     imageButton.setImageResource(R.drawable.ic_baseline_visibility_off_24);
                 } else {
                     edPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
//            edPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                     imageButton.setImageResource(R.drawable.ic_baseline_visibility_24);
                 }
             }
         });

/*

         SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();

         SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);

         if(pref.getString("login",null)==null){
             editor.putString("login","1");

             editor.commit();

         }
         SharedPreferences.Editor editor2 = getSharedPreferences("data",MODE_PRIVATE).edit();

         SharedPreferences pref2 = getSharedPreferences("data",MODE_PRIVATE);
         if(pref2.getString("login","").equals("2")){
             startActivity(new Intent(MainActivity.this,temp.class));
             finish();
         }
 */

         et_user_name = findViewById(R.id.editTextTextPersonName5);
         et_psw = findViewById(R.id.editTextTextPassword);
         btn_login = findViewById(R.id.imageButton4);


         //登录按钮的点击事件
         btn_login.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //开始登录,获取用户名和密码 getText().toString().trim();
                 userName = et_user_name.getText().toString().trim();
                 psw = et_psw.getText().toString().trim();
                 //对当前用户输入的密码进行MD5加密再进行比对判断, MD5Utils.md5( ); psw 进行加密判断是否一致
                 String md5Psw = register.MD5Util.encrypt(psw);
                 // md5Psw ; spPsw 为 根据从SharedPreferences中用户名读取密码
                 // 定义方法 readPsw为了读取用户名,得到密码
                 spPsw = readPsw(userName);
                 // TextUtils.isEmpty
                 if (TextUtils.isEmpty(userName)) {
                     Toast.makeText(MainActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                     return;
                 } else if (TextUtils.isEmpty(psw)) {
                     Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                     return;
                     // md5Psw.equals(); 判断,输入的密码加密后,是否与保存在SharedPreferences中一致
                 } else if (md5Psw.equals(spPsw)) {

                     //editor2.putString("login","2");

                    // editor2.commit();


                     //一致登录成功
                     Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                     //保存登录状态,在界面保存登录的用户名 定义个方法 saveLoginStatus boolean 状态 , userName 用户名;
                     saveLoginStatus(true, userName);
                     //登录成功后关闭此页面进入主页
                     Intent data = new Intent();
                     //datad.putExtra( ); name , value ;
                     data.putExtra("isLogin", true);
                     //RESULT_OK为Activity系统常量,状态码为-1
                     // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
                     setResult(RESULT_OK, data);
                     //销毁登录界面
                     MainActivity.this.finish();
                     //跳转到主界面,登录成功的状态传递到 MainActivity 中
                     startActivity(new Intent(MainActivity.this, temp.class));
                     return;
                 } else if ((spPsw != null && !TextUtils.isEmpty(spPsw) && !md5Psw.equals(spPsw))) {
                     Toast.makeText(MainActivity.this, "输入的用户名和密码不一致", Toast.LENGTH_SHORT).show();
                     return;
                 } else {
                     Toast.makeText(MainActivity.this, "此用户名不存在", Toast.LENGTH_SHORT).show();
                 }

             }


         });
     }

     /**
      * 从SharedPreferences中根据用户名读取密码
      */
     private String readPsw(String userName) {
         //getSharedPreferences("loginInfo",MODE_PRIVATE);
         //"loginInfo",mode_private; MODE_PRIVATE表示可以继续写入
         SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
         //sp.getString() userName, "";
         return sp.getString(userName, "");
     }

     /**
      * 保存登录状态和登录用户名到SharedPreferences中
      */
     private void saveLoginStatus(boolean status, String userName) {
         //saveLoginStatus(true, userName);
         //loginInfo表示文件名  SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
         SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
         //获取编辑器
         SharedPreferences.Editor editor = sp.edit();
         //存入boolean类型的登录状态
         editor.putBoolean("isLogin", status);
         //存入登录状态时的用户名
         editor.putString("loginUserName", userName);
         //提交修改
         editor.commit();
     }

     /**
      * 注册成功的数据返回至此
      *
      * @param requestCode 请求码
      * @param resultCode  结果码
      * @param data        数据
      */

     @Override
     //显示数据, onActivityResult
     //startActivityForResult(intent, 1); 从注册界面中获取数据
     //int requestCode , int resultCode , Intent data
     // LoginActivity -> startActivityForResult -> onActivityResult();
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         //super.onActivityResult(requestCode, resultCode, data);
         super.onActivityResult(requestCode, resultCode, data);
         if (data != null) {
             //是获取注册界面回传过来的用户名
             // getExtra().getString("***");
             String userName = data.getStringExtra("userName");
             if (!TextUtils.isEmpty(userName)) {
                 //设置用户名到 et_user_name 控件
                 et_user_name.setText(userName);
                 //et_user_name控件的setSelection()方法来设置光标位置
                 et_user_name.setSelection(userName.length());
             }
         }


     }
 }

其中

/*

         SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();

         SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);

         if(pref.getString("login",null)==null){
             editor.putString("login","1");

             editor.commit();

         }
         SharedPreferences.Editor editor2 = getSharedPreferences("data",MODE_PRIVATE).edit();

         SharedPreferences pref2 = getSharedPreferences("data",MODE_PRIVATE);
         if(pref2.getString("login","").equals("2")){
             startActivity(new Intent(MainActivity.this,temp.class));
             finish();
         }
 */


                     //editor2.putString("login","2");

                    // editor2.commit();

 此段用于检测是否为第一次登录,当不是第一次登录时,会跳过登录界面,直接进入应用。不过感觉这种写法还是有缺陷,希望有大佬能够改进一下吧。

实现效果:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值