컴퓨터/이론: 안드로이드

[2017.11.04] 68. TextWatcher와 AlertDialog 실습

heepie 2017. 11. 4. 12:57

도입

이번 포스팅에서는 EditText의 입력을 실시간으로 확인하는 TextWatcher Interface와 사용자에게 Popup를 띄워주는 AlertDialog를 실습할 예정이다.


TextWatcher는 로그인 기능 구현 시, 실시간으로 입력을 확인해 조건에 맞지 않는 입력을 하면 버튼을 클릭 할 수 없게 하는 방법 중 하나로 사용할 수 있다.

Dialog는 사용자에게 Info를 알리거나 선택을 요청할 때 사용할 수 있다.


실습

MainActivity.class

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
public class MainActivity extends AppCompatActivity {
    private final String TAG = getClass().getSimpleName();
 
    private EditText editText;
    private TextView textRealTime;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }
 
    private void initView() {
        editText = (EditText) findViewById(R.id.editText);
        textRealTime = (TextView) findViewById(R.id.textRealTime);
    }
 
    private void initListener() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d(TAG, "beforeTextChanged: " + "s=" + s + 
                                                " start=" + start + 
                                                " count=" + count + 
                                                " after=" + after);
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d(TAG, "onTextChanged: " + "s=" + s + 
                                                " start=" + start + 
                                                " before=" + before + 
                                                " count=" + count);
                textRealTime.setText(s.toString());
            }
            @Override
            public void afterTextChanged(Editable s) {
                Log.d(TAG, "afterTextChanged: " + "s=" + s);
            }
        });
    }
 
    public void showDialog(View view) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
 
        // setCancelable false 이면 사용자의 버튼 선택을 통해 Dialog 처리        
        // true라면 백그라운드 터치만으로 Dialog 종료 처리        
        dialogBuilder.setTitle("Dialog Title")
                     .setMessage("This is the Message")
                     .setPositiveButton("PositiveBtn"new DialogInterface.OnClickListener() {
                         @Override
                        public void onClick(DialogInterface dialogInterface, int which) {
                            Toast.makeText(MainActivity.this
                                                    "PositiveBtn", Toast.LENGTH_SHORT).show();
                            dialogInterface.dismiss();
                        }
                      })
                     .setNegativeButton("NegativeBtn"new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int which) {
                                Toast.makeText(MainActivity.this
                                                    "NegativeBtn", Toast.LENGTH_SHORT).show();
                            dialogInterface.dismiss();
                        }
                      })
                     .setCancelable(false);
 
        AlertDialog dialog = dialogBuilder.create();
        dialog.show();
    }
}
cs



스크린 샷



#TextWatcher #AlerDialog #Dialog