如何在 activity 中访问字符串数组以进行用户名身份验证?
How to access a String-array for username authentification in an activity?
我想创建一个 android 工作室登录页面,使用数组列表中存储的多个帐户。如何验证写入的用户名和密码是否与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>
这是我的 activity 代码:
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();
}
}
});
}
}
感谢您帮助我,希望有人对此有一些经验。谢谢..
检查数组中的每一对:
public void onClick(View view) {
boolean found = false;
String myUsername = username.getText().toString();
String myPassword = password.getText().toString();
for (int i = 0; i < use.length; i++) {
if (use[i].equals(myUsername) && pas[i].equals(myPassword)) {
Toast.makeText(PassActivity.this,"You have Authenticated Successfully",Toast.LENGTH_LONG).show();
found = true;
break;
}
}
if (!found) {
Toast.makeText(PassActivity.this,"Authentication Failed",Toast.LENGTH_LONG).show();
}
}
试试这个:
String[] usernames = getStringArray(R.array.usernames);
EditText editText = (EditText)findViewById(R.id.edittext);
String candidate = editText.getText().toString();
boolean submit_authenticate = isAuthenticated(candidate, usernames);
if(submit_authenticate){
Toast.makeText(PassActivity.this,"You have Authenticated Successfully",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(PassActivity.this,"Authentication Failed",Toast.LENGTH_LONG).show();
}
public boolean isAuthenticated(String candidate, String[] usernames) {
for(String username : usernames) {
if(candidate.equals(username)) {
return true;
}
}
return false;
}
试试下面的代码,
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
}
}
我想创建一个 android 工作室登录页面,使用数组列表中存储的多个帐户。如何验证写入的用户名和密码是否与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>
这是我的 activity 代码:
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();
}
}
});
}
}
感谢您帮助我,希望有人对此有一些经验。谢谢..
检查数组中的每一对:
public void onClick(View view) {
boolean found = false;
String myUsername = username.getText().toString();
String myPassword = password.getText().toString();
for (int i = 0; i < use.length; i++) {
if (use[i].equals(myUsername) && pas[i].equals(myPassword)) {
Toast.makeText(PassActivity.this,"You have Authenticated Successfully",Toast.LENGTH_LONG).show();
found = true;
break;
}
}
if (!found) {
Toast.makeText(PassActivity.this,"Authentication Failed",Toast.LENGTH_LONG).show();
}
}
试试这个:
String[] usernames = getStringArray(R.array.usernames);
EditText editText = (EditText)findViewById(R.id.edittext);
String candidate = editText.getText().toString();
boolean submit_authenticate = isAuthenticated(candidate, usernames);
if(submit_authenticate){
Toast.makeText(PassActivity.this,"You have Authenticated Successfully",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(PassActivity.this,"Authentication Failed",Toast.LENGTH_LONG).show();
}
public boolean isAuthenticated(String candidate, String[] usernames) {
for(String username : usernames) {
if(candidate.equals(username)) {
return true;
}
}
return false;
}
试试下面的代码,
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
}
}