我想创建一个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();
}
}
});
}}谢谢你的帮助,希望有人有这方面的经验。谢谢..
3 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
尝试下面的代码,
public void onClick(View view) { final String[] UserArray= getResources().getStringArray(R.array.user); final String[] passArray= getResources().getStringArray(R.array.pas); String Username = username.getText().toString(); String Password = password.getText().toString(); if (Arrays.asList(UserArray).contains(Username) && Arrays.asList(passArray).contains(Password )) { // authenticate successfully }else{ // authentication failed }}
添加回答
举报
0/150
提交
取消