The problem is when i login with login activity template I can use random username and password so I want to add a valid username and password for login
LOGIN FORM STATE
class LoginFormState {
@Nullable
private Integer usernameError;
@Nullable
private Integer passwordError;
private boolean isDataValid;
LoginFormState(@Nullable Integer usernameError, @Nullable Integer passwordError) {
this.usernameError = usernameError;
this.passwordError = passwordError;
this.isDataValid = false;
}
LoginFormState(boolean isDataValid) {
this.usernameError = null;
this.passwordError = null;
this.isDataValid = isDataValid;
}
@Nullable
Integer getUsernameError() {
return usernameError;
}
@Nullable
Integer getPasswordError() {
return passwordError;
}
boolean isDataValid() {
return isDataValid;
}
}
First you have to define two TextView asking username and password of the user. The password TextView must have inputType set to password. Its syntax is given below −
<EditText
android:id = "@+id/editText2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:inputType = "textPassword" />
<EditText
android:id = "@+id/editText1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>
Define a button with login text and set its onClick Property. After that define the function mentioned in the onClick property in the java file
<Button
android:id = "@+id/button1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:onClick = "login"
android:text = "@string/Login"
/>
In the java file, inside the method of onClick get the username and passwords text using getText() and toString() method and match it with the text using equals() function.
EditText username = (EditText)findViewById(R.id.editText1);
EditText password = (EditText)findViewById(R.id.editText2);
public void login(View view){
if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){
//correcct password
}else{
//wrong password
}
The last thing you need to do is to provide a security mechanism, so that unwanted attempts should be avoided. For this initialize a variable and on each false attempt, decrement it. And when it reaches to 0, disable the login button.
enter code here
int counter = 3;
counter--;
if(counter==0){
//disble the button, close the application e.t.c
}
Try this
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String user_name = edt_username.getText().toString();
String user_pass = edt_userpass.getText().toString();
if(user_name.equals("test") && user_pass.equals("test")){
// now login successfully
}
else{
// failed to login
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.