안드로이드스튜디오 로그인 값 검사하기 ( 입력 이벤트 리스너 addTextChangedListener )

2019. 12. 19. 20:062020/Android App Develop

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.example.myapplication;
 
 
 
public class MainActivity extends AppCompatActivity {
 
 
    EditText TextInputEditText_email;
    EditText TextInputEditText_password;
    RelativeLayout RelativeLayout_loginButton;
 
// 전역변수 선언 ( DB가 없기때문에 변수를 통해 입력값과 비교할 것이다
    String emailOk = "eh1021@gmail.com";
    String passwordOk = "1234";
    String input_email=null, input_password=null;
 
// 변수와 입력값 비교 함수
    public boolean validation(){
        return input_email.equals(emailOk) && input_password.equals(passwordOk);
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
 
        TextInputEditText_email = findViewById(R.id.TextInputEditText_email);
        TextInputEditText_password =  findViewById(R.id.TextInputEditText_password);
        RelativeLayout_loginButton = findViewById(R.id.RelativeLayout_LoginButton);
 
 
// 로그인 버튼 비활성화
        RelativeLayout_loginButton.setEnabled(false);
 
// addTextChangesListener EditText요소에서 사용자의 입력을 감지하는 메서드 ( 입력 전, 동안, 후 3가지 내부 메서드 존재)
        TextInputEditText_email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("Texting on email part", s + " / " + count + " " + start + " , " + before );
// 로그를 확인하여 어떤 작동이 이루어지는 지 확인할 수 있음
 
                String email = TextInputEditText_email.getText().toString();
                String password = TextInputEditText_password.getText().toString();
                input_email = email;
                input_password = password;
 
// 입력된 값이 NULL이 아닐 때 입력값과 정해준 변수값이 같을 때 LOGIN버튼을 활성화한다
 
                if( input_email != null && input_password != null ) {
                    if (input_email.equals(emailOk) && input_password.equals(passwordOk)) {
 
                        RelativeLayout_loginButton.setEnabled(true);
                    }
                }
            }
 
            @Override
            public void afterTextChanged(Editable s) {
 
            }
        });
 
 
        TextInputEditText_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("Texting on email part", s + " / " + count + " " + start + " , " + before );
                String email = TextInputEditText_email.getText().toString();
                String password = TextInputEditText_password.getText().toString();
                input_email = email;
                input_password = password;
 
                if( input_email != null && input_password != null ) {
                    if (input_email.equals(emailOk) && input_password.equals(passwordOk)) {
 
                        RelativeLayout_loginButton.setEnabled(true);
                    }
                }
            }
 
 
            @Override
            public void afterTextChanged(Editable s) {
 
            }
        });
 
 
        RelativeLayout_loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
 
                String email = TextInputEditText_email.getText().toString();
                String password = TextInputEditText_password.getText().toString();
 
                Intent intent = new Intent(getBaseContext(),LoginResultActivity.class);
                intent.putExtra("email",email);
                intent.putExtra("password",password);
 
                startActivity(intent);
 
            }
        });
 
 
 
    }
 
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

<실행결과>