OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.res.TypedArray; | 8 import android.content.res.TypedArray; |
9 import android.support.v7.widget.AppCompatEditText; | 9 import android.support.v7.widget.AppCompatEditText; |
10 import android.text.Editable; | 10 import android.text.Editable; |
(...skipping 25 matching lines...) Expand all Loading... |
36 super.onFinishInflate(); | 36 super.onFinishInflate(); |
37 addTextChangedListener(new TextWatcher() { | 37 addTextChangedListener(new TextWatcher() { |
38 @Override | 38 @Override |
39 public void beforeTextChanged(CharSequence s, int start, int count,
int after) {} | 39 public void beforeTextChanged(CharSequence s, int start, int count,
int after) {} |
40 | 40 |
41 @Override | 41 @Override |
42 public void onTextChanged(CharSequence s, int start, int before, int
count) {} | 42 public void onTextChanged(CharSequence s, int start, int before, int
count) {} |
43 | 43 |
44 @Override | 44 @Override |
45 public void afterTextChanged(Editable s) { | 45 public void afterTextChanged(Editable s) { |
46 if (isEmpty()) { | 46 if (s.toString().trim().length() != 0 && getError() != null) set
Error(null); |
47 if (getError() != null) setError(null); | |
48 } else { | |
49 setError(mAlertMessage); | |
50 } | |
51 } | 47 } |
52 }); | 48 }); |
53 } | 49 } |
54 | 50 |
55 /** | 51 /** |
56 * @return Trimmed text for validation. | 52 * @return Trimmed text for validation. |
57 */ | 53 */ |
58 public String getTrimmedText() { | 54 public String getTrimmedText() { |
59 return getText().toString().trim(); | 55 return getText().toString().trim(); |
60 } | 56 } |
61 | 57 |
62 /** | 58 /** |
63 * Sets the alert message to be shown when the text content is empty. | 59 * Sets the alert message to be shown when the text content is empty. |
64 */ | 60 */ |
65 public void setAlertMessage(String message) { | 61 public void setAlertMessage(String message) { |
66 mAlertMessage = message; | 62 mAlertMessage = message; |
67 } | 63 } |
68 | 64 |
69 /** | 65 /** |
| 66 * Checks whether the content is empty. If empty, an alert message will be s
hown. |
70 * @return Whether the content is empty. | 67 * @return Whether the content is empty. |
71 */ | 68 */ |
72 public boolean isEmpty() { | 69 public boolean validate() { |
73 return getTrimmedText().length() > 0; | 70 if (getTrimmedText().length() == 0) { |
| 71 setError(mAlertMessage); |
| 72 return false; |
| 73 } |
| 74 return true; |
74 } | 75 } |
75 } | 76 } |
OLD | NEW |