身份验证,登录名和密码

时间:2020-02-20 17:38:46

标签: android

我在Activity上有一个简单的登录名,其中包含两个文本和一个按钮,一个用于电子邮件,另一个用于密码。

按下登录按钮后,将启动一个名为 profile 的新Activity。我对大量信息进行了硬编码以使其与输入匹配,以便该意图启动第二个Activity。效果很好。

但是,当我按下电话上的“后退”按钮时,它将返回到登录Activity。但是当我再次按下登录按钮时,即使更改输入,它也会显示相同的结果:

如果我输入ArrayList中已经存在的电子邮件和密码,它将启动第二个Activity。现在,如果通过按电话上的“后退”按钮返回登录名Activity,则电子邮件和密码文本框仍保留这些值。现在,如果我删除密码并按登录,它会显示相同的结果,与我登录的配置文件相同,但是由于密码不正确,它必须阻止我登录

注意:硬编码的信息与一个自定义类一起存储在Arraylist中,该自定义类只包含5个基本信息字符串。而且,该类实现了Serializable,因此可以通过Intent传递此自定义类的对象。

登录Activity

package just.innovates.isra;

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

import androidx.appcompat.app.AppCompatActivity;

import com.hanks.htextview.base.HTextView;

import java.util.ArrayList;

public class LoginActivity extends AppCompatActivity {
    public static final String EXTRA_LOGIN = "just.innovates.isra.EXTRA_LOGIN";

    private ArrayList<Profile> arrayList = new ArrayList<>();
    private Profile profile;
    private EditText editTextEmail;
    private EditText editTextPassword;

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

        findViewById(R.id.tv_login_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

        editTextEmail = findViewById(R.id.et_login_email);
        editTextPassword = findViewById(R.id.et_login_password);

        arrayList.add(new Profile("Alaa", "Zarnaji", "aLaaZ@gmail.com", "alaa", "01145874123", R.drawable.alaa));
        arrayList.add(new Profile("Ola", "Ahmed", "olaA@gmail.com", "ola", "0123654585", R.drawable.ola));
        arrayList.add(new Profile("Muhammad", "Magdy", "magdyM@gmail.com", "magdy", "01014525874", R.drawable.magdy));
        arrayList.add(new Profile("Isra", "Ahmed", "israA@gmail.com", "isra", "01145214587", R.drawable.isra));

        findViewById(R.id.btn_login_sign_in).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getProfile();
                if (profile != null) {
                    Intent intent = new Intent(LoginActivity.this, ProfileActivity.class);
                    intent.putExtra(EXTRA_LOGIN, profile);
                    startActivity(intent);
                } else {
                    Toast.makeText(LoginActivity.this, "Wrong Email or password", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void getProfile() {
        String email = editTextEmail.getText().toString();
        String password = editTextPassword.getText().toString();

        if (!email.trim().isEmpty() || !password.trim().isEmpty()) {
            if (email.equals("alaaZ@gmail.com") && password.equals("alaa")) {
                profile = arrayList.get(0);
            } else if (email.equals("olaA@gmail.com") && password.equals("ola")) {
                profile = arrayList.get(1);
            } else if (email.equals("magdyMX@gmail.com") && password.equals("magdy")) {
                profile = arrayList.get(2);
            } else if (email.equals("israAh@gmail.com") && password.equals("isra")) {
                profile = arrayList.get(3);
            }
        }
    }
}

个人资料Activity

package just.innovates.isra;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class ProfileActivity extends AppCompatActivity {
    private TextView textViewNameFirst;
    private TextView textViewNameLast;
    private TextView textViewEmail;
    private TextView textViewPassword;
    private TextView textViewPhone;
    private ImageView imageView;
    private Profile profile;

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

        Intent intent = getIntent();
        if (intent.hasExtra(RegisterActivity.EXTRA_PROFILE)) {
            profile = (Profile) intent.getSerializableExtra(RegisterActivity.EXTRA_PROFILE);
        }else if (intent.hasExtra(LoginActivity.EXTRA_LOGIN)){
            profile = (Profile) intent.getSerializableExtra(LoginActivity.EXTRA_LOGIN);
        }

        setUI();
        assert profile != null;
        if (profile.isImage()) {
            imageView.setImageResource(profile.getImageURL());
        } else {
            imageView.setVisibility(View.GONE);
        }

        textViewNameFirst.setText(profile.getFirstName());
        textViewNameLast.setText(profile.getSecondName());
        textViewEmail.setText(profile.getEmail());
        textViewPassword.setText(profile.getPassword());
        textViewPhone.setText(profile.getPhone());
    }

    private void setUI() {
        textViewNameFirst = findViewById(R.id.nameFirst);
        textViewNameLast = findViewById(R.id.nameLast);
        textViewEmail = findViewById(R.id.email);
        textViewPassword = findViewById(R.id.password);
        textViewPhone = findViewById(R.id.phone);
        imageView = findViewById(R.id.image);
    }
}

1 个答案:

答案 0 :(得分:1)

当您输入ArrayList中包含的电子邮件和密码,并通过登录登录启动SecondActivity时,let onLoadPassed = false; let videoLoaded = false; let clipSource = 'https://mysite/myVideo.mp4'; preload(); // Make sure the video clip is fully loaded function preload(){ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Preload = factory()); }(this, (function () { 'use strict'; function preloadOne(url, done) { const xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'blob'; xhr.onprogress = event => { if (!event.lengthComputable) return false let item = this.getItemByUrl(event.target.responseURL); item.completion = parseInt((event.loaded / event.total) * 100); item.downloaded = event.loaded; item.total = event.total; this.updateProgressBar(item); }; xhr.onload = event => { let type = event.target.response.type; let blob = new Blob([event.target.response], { type: type }); let url = URL.createObjectURL(blob); let responseURL = event.target.responseURL; let item = this.getItemByUrl(responseURL); item.blobUrl = url; item.fileName = responseURL.substring(responseURL.lastIndexOf('/') + 1); item.type = type; item.size = blob.size; done(item); }; xhr.onerror = event => { console.log('Error has happend so we restart the video preloading..'); preload(); }; xhr.onloadend = function() { if(xhr.status == 404){ console.log('404 not found'); onLoadPassed = false; } else { console.log('File exist'); onLoadPassed = true; } } xhr.send(); } function updateProgressBar(item) { var sumCompletion = 0; var maxCompletion = this.status.length * 100; for (var itemStatus of this.status) { if (itemStatus.completion) { sumCompletion += itemStatus.completion; } } var totalCompletion = parseInt((sumCompletion / maxCompletion) * 100); if (!isNaN(totalCompletion)) { this.onprogress({ progress: totalCompletion, item: item }); } } function getItemByUrl(rawUrl) { for (var item of this.status) { if (item.url == rawUrl) return item } } function fetch(list) { return new Promise((resolve, reject) => { this.loaded = list.length; for (let item of list) { this.status.push({ url: item }); this.preloadOne(item, item => { this.onfetched(item); this.loaded--; if (this.loaded == 0) { this.oncomplete(this.status); resolve(this.status); } }); } }) } function Preload() { return { status: [], loaded: false, onprogress: () => {}, oncomplete: () => {}, onfetched: () => {}, fetch, updateProgressBar, preloadOne, getItemByUrl } } return Preload; }))); const preload = Preload(); preload.fetch([ clipSource ]).then(items => { // Fired when we are sure that video clip has finished loading completely let check = setInterval(passedFunc, 50); function passedFunc() { if(onLoadPassed === true){ videoLoaded = true; clearInterval(check); console.log('videoLoaded: ' + videoLoaded); }; } }); };对象将存储这些值。然后您按回去,删除密码,然后再次按登录,您所做的所有检查就是确定profile对象的值...实际上不干扰登录过程。因此,当您返回并更改值时,您的配置文件仍保留先前使用的数组中的旧值。当没有任何条件匹配时,您应该添加另一个else语句,并将您的个人资料设置为空,以便侦听器中的检查显示吐司。

因此,在所有其他if语句之后,在您的profile方法中添加此最终else语句

getProfile()

或者当两次都为空或字段不匹配时,也不必编写两次else条件,而只需在每次调用else { profile = null; } 时将其设置为null。 方法应该像这样

getProfile()