Android 登录账户密码 输入框,可清空输入信息,显隐藏密码

这是一个 Android 登录账户 密码 输入框  ,



项目 地址 点击打开链接

效果 图:




采用 自定义 控件 形式

以下是 自定义控件 的 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="10dp"
              android:background="@drawable/background_edittext"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/drawbleIconView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/username"/>

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:background="@null"
        android:singleLine="true"/>

    <ImageView
        android:id="@+id/drawbleDeleteView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:src="@mipmap/delete"/>

    <ImageView
        android:id="@+id/drawbleVisableView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/username"/>
</LinearLayout>


然后 在 attr.xml 中 定义 要用到 的 控件 属性

    <!-- 自定义 控件 的 属性 -->
    <declare-styleable name="LoginEditTextStyle">
        <!-- 删除 按钮 图标 -->
        <attr name="deleteDrawable" format="reference"/>
        <!-- 隐藏 按钮 图标 -->
        <attr name="visibleDrawable" format="reference"/>
        <!-- 输入框 左边 显示 的 图标-->
        <attr name="iconDrawable" format="reference"/>
        <!-- 是否 是 密码 输入 框 -->
        <attr name="passwordTag" format="boolean"/>
    </declare-styleable>

然后 编写 Java 代码

/**
 * 自定义 登录 输入 框
 * Created by Administrator on 2016/7/12.
 */
public class LoginEditText extends RelativeLayout implements View.OnClickListener, TextWatcher, View.OnFocusChangeListener {
    private Context context;
    private EditText editText;
    private ImageView drawbleIconView, drawbleDeleteView, drawbleVisableView;
    // 是否 是 密码 输入 框
    private boolean passwordTag;
    // 是否 显示 密码
    private boolean passwordDisplayTag;

    public LoginEditText(Context context) {
        this(context, null);
    }

    public LoginEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
        getArrrs(attrs);
    }


    private void init() {
        View viewGroup = LayoutInflater.from(context).inflate(R.layout.view_login_edittext, this);
        editText = (EditText) viewGroup.findViewById(R.id.editText);
        drawbleIconView = (ImageView) viewGroup.findViewById(R.id.drawbleIconView);
        drawbleDeleteView = (ImageView) viewGroup.findViewById(R.id.drawbleDeleteView);
        drawbleVisableView = (ImageView) viewGroup.findViewById(R.id.drawbleVisableView);
        setListener();
    }

    private void setListener() {
        drawbleDeleteView.setOnClickListener(this);
        drawbleVisableView.setOnClickListener(this);
        editText.addTextChangedListener(this);
        editText.setOnFocusChangeListener(this);
    }

    private void getArrrs(AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoginEditTextStyle);

        passwordTag = typedArray.getBoolean(R.styleable.LoginEditTextStyle_passwordTag, false);

        Drawable deleteDrawable = typedArray.getDrawable(R.styleable.LoginEditTextStyle_deleteDrawable);
        Drawable iconDrawable = typedArray.getDrawable(R.styleable.LoginEditTextStyle_iconDrawable);
        Drawable visibleDrawable = typedArray.getDrawable(R.styleable.LoginEditTextStyle_visibleDrawable);

        showOrHideDrawableView(drawbleDeleteView, deleteDrawable);
        showOrHideDrawableView(drawbleIconView, iconDrawable);


        setIsPassword(passwordTag, visibleDrawable);
        // 先默认 隐藏
        drawbleVisableView.setVisibility(GONE);
        drawbleDeleteView.setVisibility(GONE);
    }

    public void setIsPassword(boolean passwordTag, Drawable visibleDrawable) {
        if (passwordTag) {
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
            showOrHideDrawableView(drawbleVisableView, visibleDrawable);
        } else {
            showOrHideDrawableView(drawbleVisableView, null);
        }
    }

    public void setIconView(Drawable drawable) {
        showOrHideDrawableView(drawbleIconView, drawable);
    }


    /**
     * 显示 或 隐藏 图标
     *
     * @param imageView
     * @param drawable
     */
    public void showOrHideDrawableView(ImageView imageView, Drawable drawable) {
        if (drawable == null) {
            imageView.setVisibility(GONE);
        } else {
            imageView.setVisibility(VISIBLE);
            imageView.setImageDrawable(drawable);
        }
    }

    public Editable getEditText() {
        return editText.getText();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.drawbleDeleteView:
                editText.setText("");
                break;
            case R.id.drawbleVisableView:
                if (!passwordDisplayTag) {
                    // 设置 EditText 的 input type  显示 密码
                    editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    // 隐藏 密码
                    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                passwordDisplayTag = !passwordDisplayTag;
                break;
        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (hasFocus()) {
            // 当 输入 字符 长度 不为0 时 显示 删除 按钮
            if (s.length() > 0) {
                drawbleDeleteView.setVisibility(VISIBLE);
                if (passwordTag)
                    drawbleVisableView.setVisibility(VISIBLE);
            } else {
                drawbleDeleteView.setVisibility(GONE);
                drawbleVisableView.setVisibility(GONE);
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        // 如果 EditText的 焦点 改变了  则相应的 隐藏 显示 功能 按钮
        if (!hasFocus) {
            drawbleDeleteView.setVisibility(GONE);
            drawbleVisableView.setVisibility(GONE);
        } else if (editText.getText().length() > 0) {
            drawbleDeleteView.setVisibility(VISIBLE);
            if (passwordTag)
                drawbleVisableView.setVisibility(VISIBLE);
        }
    }
}
</pre><pre name="code" class="html">


项目 地址 点击打开链接

效果 图:



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值