반응형
이메일 대해서 조건에 맞지 않을 시, 경고 문구와 테투리가 바뀌는 것을 봤을 것입니다.
그 방법에 대한 간단한 예제를 만들겠습니다.
<실행 화면> 이메일 형식이 아닐 시 경고 문구와 빨간색 테투리를 적용합니다.
알파벳,숫자를 입력하고 @그리고 알파벳,숫자를 입력하고 . 그리고 알파벳,숫자 형식일 경우
정상적인 이메일로 간주합니다.
우선 EditText의 배경을 만들겠습니다. 2가지 ( 흰색 배경, 빨간색 테두리 배경 )
white_edittext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version="1.0" encoding="utf-8"?>
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp">
<shape android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
|
같은 방법으로 red_edittext.xml을 만들어 줍니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version="1.0" encoding="utf-8"?>
<item
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp">
<shape android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#FF0000" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
|
이제 테스트를 위해 activiy_main.xml을 작성합니다.
activiy_main.xml
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
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DADAFF"
tools:context=".MainActivity">
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingHorizontal="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:layout_editor_absoluteX="140dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="이메일"
android:textColor="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="12dp"
android:background="@drawable/white_edittext"
android:hint="이메일을 입력해주세요."
android:paddingLeft="10dp"
android:textColor="#000000"
android:textColorHint="#A0A0A0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/tv_error_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_email"
tools:text="테스트 " />
|
이제 MainActivity.java를 작성하겠습니다.
editText에 addTextChangedListener 사용과
안드로이드에서 제공해주는 이메일 형식 체크를 이용하겠습니다.
( android.util.Patterns.EMAIL_ADDRESS.matcher(String email).matches() )
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
|
package com.example.ex_edittext_background;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText et_email;
TextView tv_error_email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_email=findViewById(R.id.et_email);
tv_error_email=findViewById(R.id.tv_error_email);
et_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) {
}
@Override
public void afterTextChanged(Editable s) {
tv_error_email.setText("이메일 형식으로 입력해주세요."); // 경고 메세지
et_email.setBackgroundResource(R.drawable.red_edittext); // 적색 테두리 적용
}
else{
tv_error_email.setText(""); //에러 메세지 제거
et_email.setBackgroundResource(R.drawable.white_edittext); //테투리 흰색으로 변경
}
}// afterTextChanged()..
});
}// onCreate()..
}// MainActivity Class..
|
<실행 화면> 이메일 형식이 아닐 시 경고 문구와 빨간색 테투리를 적용합니다.
알파벳,숫자를 입력하고 @그리고 알파벳,숫자를 입력하고 . 그리고 알파벳,숫자 형식일 경우
정상적인 이메일로 간주합니다.
<추가 자료>
안드로이드에서 제공하는 이메일 형식은 아래와 같습니다.
1
2
3
4
5
6
7
8
9
10
|
public static final Pattern EMAIL_ADDRESS
= Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
);
|
반응형
'안드로이드 > 개발자 일상' 카테고리의 다른 글
안드로이드 AlarmManager 1-1 (매일 반복적으로 알람 받기) (2) | 2020.04.02 |
---|---|
안드로이드 CircleProgressBar (원형 프로그래스 바, 라이브러리 사용) (13) | 2020.03.29 |
안드로이드 EditText (글자수 표시, 줄 바꿈 막기) (10) | 2020.03.28 |
안드로이드 실제 디바이스 사이즈 구하기 (xml에 dp값) (8) | 2020.03.28 |
안드로이드 Splash (로딩화면) (2) | 2020.03.27 |
댓글