Last active
August 29, 2015 14:19
-
-
Save SyllaJay/102f960eff63d299b3f0 to your computer and use it in GitHub Desktop.
An EditText for input type "textPassword", with a switch button which can control the visibility of password.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.com.you.packagename.activity.SignUpActivity"> | |
<com.you.packagename.widget.PasswordEditText | |
android:id="@+id/edit_password" | |
android:layout_width="0dp" | |
android:layout_height="wrap_content" | |
android:layout_weight="3" | |
android:hint="@string/account_password_hint" | |
android:imeOptions="actionDone" | |
android:inputType="textPassword" | |
android:singleLine="true" | |
app:passwordHiddenDrawable="@drawable/password_hidden" | |
app:passwordShownDrawable="@drawable/password_show"/> | |
</LinearLayout> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="PasswordEditText"> | |
<attr name="passwordHiddenDrawable" format="reference" /> | |
<attr name="passwordShownDrawable" format="reference" /> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
�PNG | |
IHDRQQ��J6�IDATx^�\{xTյ��}&�y@`�Z�Z{��ސ��! b��Qk��z�Z[�*��T媥�Yl��*j�XTԂ��AHB*W�^|Py��#�������:�I����{g_����^�w�^�B~��L!Oy]�<�y]@�yĨ�.��KbDp�D^� ���$��\ ����+ ����Mu*1�p�F��h�S�x�� ��0�+M�M-ﻀCN$>I�[�:�U�6� �1����(`���� F�@oh�j(�O��:��"Y,>� r������D| "u�[C�L�^��=�q�p::�D���K=���jp>���bJ$���c | |
3�~Ŵ���b�nR��4� | |
����B��!4�A���� ��Nm�w8%���x��r�����7 �������H͇�d��@z3��g{��c>�%K�>�|���0�`�I�$��x#S����/�*h�|��tR��ﮃ��FE��Me� | |
��l | |
x��62h�Ҵ�0̭E�v�"%jws�(�eh�c�7�Pb���Z&��?Q�55�Num� | |
b���$�y�z�P6�]~�� ��:(�u8tWW�t��F@�34�6T����������g�PtD��)�x��j�� 8ֶ�B[�mP3� ��f<�%@���!R��� | |
��'2Z'j���8Z?I�����k.��}������WÎ�`RN rC�����s�t^�����;���h�2Z��;�\S� �S�w5�����liN���v�xE�* ���h��a�ׇX�K(>d:1�0�v���'=�o,2j�@z8�Y����!Fq�d��V�`o� | |
&^��Ă��Wv�c ��|���7hV�>��4XI��A��^Z�k��,CG|)3��/�H�ߦA+[�8�7u^V v�W|!��� | |
�j�&lڐ���VM�B�w|�,� | |
b<�5�V����I�L��������c=��������(:*�f|���5�c<�G"o��Nf��l�8S&���9���(\ | |
@�z&����Q(���\��i_N����J�+�z&��/՜��΄QtN��i��QvX&'m6 | |
��ஷi�6qar����I�A�ь,��O%��(mn �wpyyAhH�hb�5�"2��4�̷��ON�DQ�#r�يY�&މ�3�\]���[�:TW���VT�� ��Wb68q��p]�.Pd߀(3f�;�g��]<�GZ�+��.�\[ዋ���E�t��n���]�G��{�]��>�}�5NM�|��-6=j��7�MG� | |
���c���� | |
$�!~嵦�x0�A<PS>��K�I衖[@���}Q�ڰA�πC�p�y9n��/�����/����Bѻ���iN:�i~�c��g�s$�����q=W�ꪃ ��>�����Ľ���s���bl�l���D�eS���`�}U"/c2n4n|-ɘ�h�1N� & �*rQT�N�j�o��eNh����k2nOѓ�u,�����\�~A�U^��5I�DL��H����m��7TW�����^��]��ˋ���3�jPqL�h���bTҪ��DXJ ���䭨����Ǚ�؊tdt2�|��М�tz��v�W\JL��0�6nƾ=�i�~��bK�v����Mq36��(Z�ٙ��`�^��w�֧3X�����W(9��[��M��J�K��=l�[q�֘ | |
?�O� | |
�X���X��2���[$z55[-�%%v��"�c�Jg z���V�$s�l˭ ��[~4U"�h�1�1��q�3��^��ƻzc�3^���9唣`��L���aX�#5�a��`�_2ݰ�����j�#&�̟(y���$���� �UT�+�li�Ơ���-MNu`:�W�R�WMeV)Vg�X�tKs�]3��@���L ����U���|�=�y�&5sPcK�AC5#`�8�߳ю���c_�$�Ie�����f����у�R�_��.���)�'���`�e�2�ҏ�w�@�|QIq0�6��Eɪ�w�5aϻ�ہO��t���-�9�D�c*TW��D+&�h�f��zں�Ӥ��W� | |
+����`��NY | |
�_C��"�Jq_�/�,$Q�����4�P �&�aN#TW�.���""��T�$�5�W]9��,��l�)�����O���6�)���& wd������%��Y��R$P���(#���̏m��P-#�}������1���E�K�$p�J��Rb�#uU����`k���d~�sl/�B$ | |
+c7�j+��p���D�M,�1S����Bu�rm����vqa�?ؚ3?<�+��Y|3�+�+�u�V6A��H�~]�<�+����_��\Zϻ��6)��4µ㦱R��1DL0�D�n����S®O'{Wm� ��Bu���?Đ�/ؚSzM�8n�7�jD���3fw�,���5���U | |
#0��!>P���*~��l�����K���R�9Ӥ'm��`z]+\$�bJ�����Pf���U�oe�qx��o����̳ٿ� | |
�y���dz��=�8S%~ ��I�χDt��`��R>h=%X���2AJ��( �'S�)iڸ#�ͭ�)�:��]��8��.zz`妭Η�̌�TÆ�� iƒ�\� ��]�w'#�C\����eӼ�?�~X�����fn�� ���]��� | |
ez��?���gZW�>��E)���4pM*����$��0 Enqb,��夓!� ���Ӥ�Ǝ7]�$R�}��c��֧2M����p��g�^����>�aё���?0�4I]DƢ��*Ҳf c�s��#f�D�S��C�%���:"���p..�H)���B����-��<d�_�<a$�ĝ���r�m�[+�_w�UÓ�N�����#�H��>]hl���$�u`r�hC� | |
vC������-����x�����{ȸ���m %����<�Y�.���A�f�7�\�`�6�99�Ksb`�'�Kn?��)����sQ�*7 | |
�:b�ڊ�G��6�Z��?��H+�q�f1YB�ѥ�������{։���6�Z�Hw�~'�f+��o�3��{��5���H�G2�Σin:�$-�BL,mIq\�����IrA�����ֻ�\ | |
�����^�'K���/��9Nk*���+�q-�J�Z�#�-]�j~_:бN��Pm��@V | |
�4�D�R��2_M��钸r��㷀崊T;��Q� �3 ø$��3���2��� �k�!Ny1f���9�7�CXbO�)P|���B�W �9fƚ�Yo�$�L��*I���?���Γ�!�����{�Mm���wXu�.�a��,OAD��A����!��3If, v�����$%�Ξ�r�C�b��V�߶bI'E���R��=��lGM`<����H礊�������M6�K[�R��Mɩ�-�nTN�9uqx�ڌ�Id��%�]l;N��e��!c� O�3L�%�ɇ��hLM�7�y9]��n�����=�#�[���U��8�p | |
D�zI?�Y:��c�UI&�S�U��n��鮆\qE�Hh}2�H��Y~�H��Rߕ���@c-�^�l~hj�3����xp | |
X>6IOR���d�M��=[�x� ������Y#��뒹6{Q�����l���AOn(�/��=��H��\5���6����C~��F�<��_/��r������K�Y�©/��`y�D | |
Vo��=n?ȱnF���ȁ�:�Ɛ��Ԑ�i!:z/��%�6�e�Q�$�����3�%���������Ah3X�Y���^.�����Vm��"�l=Db�#&+{�'Ph��%>��/'���������qOW�ey�$�Xޤ$��3 �m�۔<�e��+;��to�B/�2̈��k�/B�Xf|INd�"%{�E�:d�� | |
��r-y1I<D*�ȁ��bS�%Mg2�>�bb4$��P��@�L�#̊�IS���4��Y/M��X$Qơ�L{��RbZB�g��t�Gٶ�8���&���)8mta�A��p��|�ЋQ� q��/��^���__|�����ʣ�xU�^,��EU8P�{3c���@�S�yyߓ5�ܥ\G�,�dR�3�4��˭H^��� ^ +|C���ᔸO�:;����lc�������y��u�g�)�y�"5��<�y]@�yĨ�.��KbDp�D^� ���$��\ ��D@�_�ʠP>�IEND�B`� |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.you.packagename.widget; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.drawable.Drawable; | |
import android.text.InputType; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.EditText; | |
import com.you.packagename..R; | |
/** | |
* <p/> | |
* Created by JAY on 2015/4/18. | |
*/ | |
public class PasswordEditText extends EditText implements View.OnTouchListener { | |
// the drawable for switch button, give it "eye" drawable usually. | |
private Drawable mShownDrawable; | |
private Drawable mHiddenDrawable; | |
public PasswordEditText(Context context) { | |
super(context); | |
init(context, null); | |
} | |
public PasswordEditText(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public PasswordEditText(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs); | |
} | |
@Override | |
public void setOnTouchListener(OnTouchListener l) { | |
this.l = l; | |
} | |
private OnTouchListener l; | |
@Override | |
public boolean onTouch(View v, MotionEvent event) { | |
if (getCompoundDrawables()[2] != null) { | |
boolean tappedX = event.getX() > (getWidth() - getPaddingRight() - mShownDrawable | |
.getIntrinsicWidth()); | |
if (tappedX) { | |
if (event.getAction() == MotionEvent.ACTION_UP) { | |
boolean wasHidden = getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); | |
setPasswordVisible(wasHidden); | |
} | |
return true; | |
} | |
} | |
if (l != null) { | |
return l.onTouch(v, event); | |
} | |
return false; | |
} | |
private void init(Context context, AttributeSet attrs) { | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PasswordEditText); | |
Drawable hiddenDrawable = a.getDrawable(R.styleable.PasswordEditText_passwordHiddenDrawable); | |
Drawable shownDrawable = a.getDrawable(R.styleable.PasswordEditText_passwordShownDrawable); | |
a.recycle(); | |
mShownDrawable = shownDrawable; | |
mHiddenDrawable = hiddenDrawable; | |
if (mShownDrawable == null) { | |
// just give it a default drawable, it's up to you . | |
mShownDrawable = getResources().getDrawable(android.R.drawable.radiobutton_on_background); | |
} | |
if (mHiddenDrawable == null) { | |
// just give it a default drawable, it's up to you . | |
mHiddenDrawable = getResources().getDrawable(android.R.drawable.radiobutton_off_background); | |
} | |
mShownDrawable.setBounds(0, 0, mShownDrawable.getIntrinsicWidth(), mShownDrawable.getIntrinsicHeight()); | |
mHiddenDrawable.setBounds(0, 0, mHiddenDrawable.getIntrinsicWidth(), mHiddenDrawable.getIntrinsicHeight()); | |
boolean textPassword = (getInputType() == (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)); | |
if (textPassword) { | |
updateIcon(false); | |
} else { | |
throw new IllegalArgumentException(PasswordEditText.class.getSimpleName() + " needs attribute android:inputType=\"textPassword\"! Please set it in the xml file."); | |
} | |
super.setOnTouchListener(this); | |
} | |
protected void updateIcon(boolean showPassword) { | |
Drawable drawable = showPassword ? mShownDrawable : mHiddenDrawable; | |
setCompoundDrawables(getCompoundDrawables()[0], | |
getCompoundDrawables()[1], drawable, getCompoundDrawables()[3]); | |
} | |
/** | |
* @param showPassword show password if true, otherwise hide password | |
*/ | |
protected void setPasswordVisible(boolean showPassword) { | |
int lastSelection = getSelectionEnd(); | |
if (showPassword) { | |
// show password | |
setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD); | |
} else { | |
// hide password, value is 129 | |
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); | |
} | |
// retain position of the caret | |
setSelection(lastSelection); | |
updateIcon(showPassword); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment