본문 바로가기
안드로이드/개발자 일상

안드로이드 EditText (글자수 표시, 줄 바꿈 막기)

by 차누감 2020. 3. 28.

EditText에 리스너( addTextChangedListener )를 추가하여 입력된 글자 개수를 표시합니다. 

해당 정보는 안드로이드 개발자 사이트를 참고 부탁드립니다.

https://developer.android.com/reference/android/text/TextWatcher

 

TextWatcher  |  Android 개발자  |  Android Developers

TextWatcher public interface TextWatcher implements NoCopySpan Known indirect subclasses AbsListView Base class that can be used to implement virtualized lists of items.  ExpandableListView A view that shows items in a vertically scrolling two-level list. 

developer.android.com

<실행 화면> 입력할 때마다 글자 수를 알 수 있으며, 지워도 바로 글자 수가 바뀝니다. 그리고 Enter키는 적용 안됩니다.


이제 예제를 작성하겠습니다. 

 

activity_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
<?xml version="1.0" encoding="utf-8"?>
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#DADAFF"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="8dp"
        android:text="ID :"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@+id/et_input"
        app:layout_constraintEnd_toStartOf="@+id/et_input"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/et_input" />
 
    <EditText
        android:id="@+id/et_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:hint="아이디를 입력해주세요."
        app:layout_constraintEnd_toStartOf="@+id/tv_input_length"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />
 
    <TextView
        android:id="@+id/tv_input_length"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="4dp"
        android:text="0글자"
        android:layout_marginHorizontal="8dp"
        app:layout_constraintBottom_toBottomOf="@+id/et_input"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/et_input" />
 
 
 

이제 MainActivity.java를 작성하겠습니다.

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
package com.example.ex_edittext_changetext;
 
 
 
public class MainActivity extends AppCompatActivity {
 
    EditText et_input;
    TextView tv_input_length;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        et_input=findViewById(R.id.et_input);
        tv_input_length=findViewById(R.id.tv_input_length);
 
        //EditText 리스너 (입력시 반응)
        et_input.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_input_length.setText(s.length()+"글자");   //글자수 TextView에 보여주기.
            }
        });
 
        //EditText Enter key 방지
        et_input.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode==event.KEYCODE_ENTER) return true;
                return false;
            }
        });
 
    }// onCreate()..
}// MainActivityc Class..
 
 

 

<실행 화면> 입력할 때마다 글자 수를 알 수 있으며, 지워도 바로 글자 수가 바뀝니다. 그리고 Enter키는 적용 안됩니다.

 

댓글