带眼睛,左侧图标的EdittextView

用组合控件做的,效果如下:
效果图.jpg
资源图标:
phone.png
icon_eye_open.png
code.png
icon_eye_close.png
使用布局:

needEye设置需不需要眼睛

 <com.latale.MainEditTextView
        android:id="@+id/et_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/x20"
        app:drawableLeft="@mipmap/phone"
        app:inputType="number"
        app:maxLength="11"
        app:hint="请输入账号" />

    <com.latale.MainEditTextView
        android:id="@+id/et_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_username"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/x20"
        app:drawableLeft="@mipmap/code"
        app:inputType="textPassword"
        app:needEye="true"
        app:maxLength="18"
        app:hint="请输入密码" />

组合View,继承LinearLayout:
public class MainEditTextView extends LinearLayout {

    private final EditText mEtData;

    public MainEditTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_main_edit_text, this, true);
        ImageView mImLeft = findViewById(R.id.im_left);
        mEtData = findViewById(R.id.et_data);
        CheckBox mImEye = findViewById(R.id.im_eye);

        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.MainEditTextView);
        if (attributes != null) {
            int drawableLeft = attributes.getResourceId(R.styleable.MainEditTextView_drawableLeft, Color.WHITE);
            mImLeft.setImageDrawable(context.getDrawable(drawableLeft));

            String hint = attributes.getString(R.styleable.MainEditTextView_hint);
            mEtData.setHint(hint);

            int maxLenth = attributes.getInt(R.styleable.MainEditTextView_maxLength, 32);
            mEtData.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLenth)});
            int inputType = attributes.getInt(R.styleable.MainEditTextView_inputType, EditorInfo.TYPE_NULL);
            mEtData.setInputType(inputType);

            boolean needEys = attributes.getBoolean(R.styleable.MainEditTextView_needEye, false);
            if (needEys) {
                mImEye.setVisibility(VISIBLE);
                mImEye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        mEtData.setTransformationMethod(isChecked ? PasswordTransformationMethod.getInstance() : HideReturnsTransformationMethod.getInstance());
                        mEtData.setSelection(mEtData.getText().length());
                    }
                });
            } else {
                mImEye.setVisibility(GONE);
                mEtData.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mEtData.setSelection(mEtData.getText().length());
            }
        }
    }

    public Editable getText() {
        return mEtData.getText();
    }
}
view_main_edit_text布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/x230"
    android:layout_height="@dimen/x40"
    android:background="@drawable/bg_edittext"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/im_left"
        android:layout_width="@dimen/x30"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/x5"
        android:padding="@dimen/x3"
        android:scaleType="center" />

    <View
        android:layout_width="@dimen/x1"
        android:layout_height="@dimen/x20"
        android:background="@color/colorGroupEdit" />

    <EditText
        android:id="@+id/et_data"
        android:layout_width="@dimen/x140"
        android:layout_height="match_parent"
        android:layout_marginLeft="@dimen/x10"
        android:background="@null"
        android:inputType="number"
        android:maxLines="1"
        android:textColor="@color/colorTvTitle"
        android:textSize="@dimen/x15" />

    <CheckBox
        android:id="@+id/im_eye"
        android:layout_width="@dimen/x20"
        android:layout_height="@dimen/x12"
        android:layout_marginLeft="@dimen/x10"
        android:background="@drawable/bg_eye"
        android:button="@null"
        android:checked="false" />

</LinearLayout>
bg_eye布局:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@mipmap/icon_eye_close"/>

    <item android:drawable="@mipmap/icon_eye_open" />
</selector>
attrs代码,inputType是源码里面直接复制的
<declare-styleable name="MainEditTextView">
        <attr name="drawableLeft" format="reference|integer" />
        <attr name="needEye" format="boolean" />
        <attr name="hint" format="string" />
        <attr name="maxLength" format="integer" />
        <attr name="inputType">
            <!-- There is no content type.  The text is not editable. -->
            <flag name="none" value="0x00000000" />
            <!-- Just plain old text.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_NORMAL}. -->
            <flag name="text" value="0x00000001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 request capitalization of all characters.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}. -->
            <flag name="textCapCharacters" value="0x00001001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 request capitalization of the first character of every word.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_WORDS}. -->
            <flag name="textCapWords" value="0x00002001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 request capitalization of the first character of every sentence.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_SENTENCES}. -->
            <flag name="textCapSentences" value="0x00004001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 request auto-correction of text being input.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_AUTO_CORRECT}. -->
            <flag name="textAutoCorrect" value="0x00008001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 specify that this field will be doing its own auto-completion and
                 talking with the input method appropriately.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_AUTO_COMPLETE}. -->
            <flag name="textAutoComplete" value="0x00010001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 allow multiple lines of text in the field.  If this flag is not set,
                 the text field will be constrained to a single line.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_MULTI_LINE}. -->
            <flag name="textMultiLine" value="0x00020001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 indicate that though the regular text view should not be multiple
                 lines, the IME should provide multiple lines if it can.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_IME_MULTI_LINE}. -->
            <flag name="textImeMultiLine" value="0x00040001" />
            <!-- Can be combined with <var>text</var> and its variations to
                 indicate that the IME should not show any
                 dictionary-based word suggestions.  Corresponds to
                 {@link android.text.InputType#TYPE_TEXT_FLAG_NO_SUGGESTIONS}. -->
            <flag name="textNoSuggestions" value="0x00080001" />
            <!-- Text that will be used as a URI.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_URI}. -->
            <flag name="textUri" value="0x00000011" />
            <!-- Text that will be used as an e-mail address.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_EMAIL_ADDRESS}. -->
            <flag name="textEmailAddress" value="0x00000021" />
            <!-- Text that is being supplied as the subject of an e-mail.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_EMAIL_SUBJECT}. -->
            <flag name="textEmailSubject" value="0x00000031" />
            <!-- Text that is the content of a short message.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_SHORT_MESSAGE}. -->
            <flag name="textShortMessage" value="0x00000041" />
            <!-- Text that is the content of a long message.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_LONG_MESSAGE}. -->
            <flag name="textLongMessage" value="0x00000051" />
            <!-- Text that is the name of a person.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_PERSON_NAME}. -->
            <flag name="textPersonName" value="0x00000061" />
            <!-- Text that is being supplied as a postal mailing address.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_POSTAL_ADDRESS}. -->
            <flag name="textPostalAddress" value="0x00000071" />
            <!-- Text that is a password.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_PASSWORD}. -->
            <flag name="textPassword" value="0x00000081" />
            <!-- Text that is a password that should be visible.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_VISIBLE_PASSWORD}. -->
            <flag name="textVisiblePassword" value="0x00000091" />
            <!-- Text that is being supplied as text in a web form.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_WEB_EDIT_TEXT}. -->
            <flag name="textWebEditText" value="0x000000a1" />
            <!-- Text that is filtering some other data.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_FILTER}. -->
            <flag name="textFilter" value="0x000000b1" />
            <!-- Text that is for phonetic pronunciation, such as a phonetic name
                 field in a contact entry.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_PHONETIC}. -->
            <flag name="textPhonetic" value="0x000000c1" />
            <!-- Text that will be used as an e-mail address on a web form.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS}. -->
            <flag name="textWebEmailAddress" value="0x000000d1" />
            <!-- Text that will be used as a password on a web form.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_TEXT} |
                 {@link android.text.InputType#TYPE_TEXT_VARIATION_WEB_PASSWORD}. -->
            <flag name="textWebPassword" value="0x000000e1" />
            <!-- A numeric only field.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_NUMBER} |
                 {@link android.text.InputType#TYPE_NUMBER_VARIATION_NORMAL}. -->
            <flag name="number" value="0x00000002" />
            <!-- Can be combined with <var>number</var> and its other options to
                 allow a signed number.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_NUMBER} |
                 {@link android.text.InputType#TYPE_NUMBER_FLAG_SIGNED}. -->
            <flag name="numberSigned" value="0x00001002" />
            <!-- Can be combined with <var>number</var> and its other options to
                 allow a decimal (fractional) number.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_NUMBER} |
                 {@link android.text.InputType#TYPE_NUMBER_FLAG_DECIMAL}. -->
            <flag name="numberDecimal" value="0x00002002" />
            <!-- A numeric password field.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_NUMBER} |
                 {@link android.text.InputType#TYPE_NUMBER_VARIATION_PASSWORD}. -->
            <flag name="numberPassword" value="0x00000012" />
            <!-- For entering a phone number.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_PHONE}. -->
            <flag name="phone" value="0x00000003" />
            <!-- For entering a date and time.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_DATETIME} |
                 {@link android.text.InputType#TYPE_DATETIME_VARIATION_NORMAL}. -->
            <flag name="datetime" value="0x00000004" />
            <!-- For entering a date.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_DATETIME} |
                 {@link android.text.InputType#TYPE_DATETIME_VARIATION_DATE}. -->
            <flag name="date" value="0x00000014" />
            <!-- For entering a time.  Corresponds to
                 {@link android.text.InputType#TYPE_CLASS_DATETIME} |
                 {@link android.text.InputType#TYPE_DATETIME_VARIATION_TIME}. -->
            <flag name="time" value="0x00000024" />
        </attr>
    </declare-styleable>

THE END

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,752评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,100评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,244评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,099评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,210评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,307评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,346评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,133评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,546评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,849评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,019评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,702评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,331评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,030评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,260评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,871评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,898评论 2 351

推荐阅读更多精彩内容