如何在活动中访问String-array以进行用户名验证?

我想创建一个android studio登录页面,使用存储在数组列表中的多个帐户。如何验证写入的用户名和密码是否与存储在arraylist中的用户名和密码相同

继承我在res中的数组列表:

<string-array name="user">
    <item>admin</item>
    <item>jhon</item></string-array><string-array name="pass">
    <item>admin</item>
    <item>littlejhon</item></string-array>

这里是我的活动代码:

import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.
CardView;import android.view.View;import android.widget.EditText;import android.widget.Toast;    
import java.util.HashMap;import java.util.Objects;public class PassActivity extends AppCompatActivity {
    EditText username;
    EditText password;
    CardView card;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pass);

        final String[] use = getResources().getStringArray(R.array.user);
        final String[] pas = getResources().getStringArray(R.array.pass);

        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        card     = findViewById(R.id.card);

        card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Objects.equals(username.getText().toString(), use)
                    &&
                    Objects.equals(password.getText().toString(),pas))
                {
                    Toast.makeText(
                        PassActivity.this,
                        "You have Authenticated Successfully",
                        Toast.LENGTH_LONG)
                    .show();
                }
                else {
                    Toast.makeText(
                        PassActivity.this,
                        "Authentication Failed",
                        Toast.LENGTH_LONG)
                    .show();
                }
            }
        });
    }}

谢谢你的帮助,希望有人有这方面的经验。谢谢..


墨色风雨
浏览 586回答 3
3回答

德玛西亚99

尝试下面的代码,&nbsp;public&nbsp;void&nbsp;onClick(View&nbsp;view)&nbsp;{ &nbsp;&nbsp;final&nbsp;String[]&nbsp;UserArray=&nbsp;getResources().getStringArray(R.array.user); &nbsp;&nbsp;final&nbsp;String[]&nbsp;passArray=&nbsp;getResources().getStringArray(R.array.pas); &nbsp;&nbsp;String&nbsp;Username&nbsp;=&nbsp;username.getText().toString(); &nbsp;&nbsp;String&nbsp;Password&nbsp;=&nbsp;password.getText().toString(); &nbsp;&nbsp;if&nbsp;(Arrays.asList(UserArray).contains(Username)&nbsp;&&&nbsp;Arrays.asList(passArray).contains(Password&nbsp;))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;authenticate&nbsp;successfully &nbsp;&nbsp;}else{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;authentication&nbsp;failed &nbsp;&nbsp;&nbsp;}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java