输入自定义密码访问android,Android自定义密码输入EditTextLayout

本文为大家分享了Android自定义密码输入的具体代码,供大家参考,具体内容如下

布局

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/delete"

android:layout_width="30dp"

android:layout_height="48dp"

android:layout_gravity="center"

android:scaleType="center"

android:src="@drawable/ico_delete"/>

android:id="@+id/ck_shift"

android:layout_gravity="center"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/pwd_selector"

android:button="@null"/>

用于密码输入的自定义控件

/**

* Created by showdy on 2017/3/15.

*

* 一个用于密码输入的自定义控件

*/

public class PwdEditLayout extends LinearLayout implements TextWatcher, View.OnFocusChangeListener,

View.OnClickListener, CompoundButton.OnCheckedChangeListener {

private ImageView mDeleteIcon;

private CheckBox mShiftIcon;

private EditText mEditText;

public PwdEditLayout(Context context) {

this(context, null);

}

public PwdEditLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public PwdEditLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

setOrientation(HORIZONTAL);

setCustomBackground();

}

private void setCustomBackground() {

GradientDrawable gd = new GradientDrawable();

gd.setCornerRadius(TypedValue.applyDimension(COMPLEX_UNIT_DIP, 4, Resources.getSystem().getDisplayMetrics()));

gd.setStroke((int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 1, Resources.getSystem().getDisplayMetrics()), Color.BLUE);

if (Build.VERSION.SDK_INT < 16) {

this.setBackgroundDrawable(gd);

} else {

this.setBackground(gd);

}

}

/**

* Called when a new child is aded to this ViewGroup. Overrides should always

* call super.onViewAdded.

*

* @param child the added child view

*/

@Override

public void onViewAdded(View child) {

super.onViewAdded(child);

if (child instanceof EditText) {

if (getChildCount() != 1) {

throw new IllegalArgumentException("Only one EditText can be added in this layout.");

}

mEditText = (EditText) child;

mEditText.setBackgroundColor(Color.TRANSPARENT);

//关键点1

LayoutInflater.from(getContext()).inflate(R.layout.layout_edittext_pwd, this, true);

mDeleteIcon = (ImageView) findViewById(R.id.delete);

mShiftIcon = (CheckBox) findViewById(R.id.ck_shift);

//关键点2

setAddStatesFromChildren(true); //使得父类获得和子控件相同的状态

mEditText.addTextChangedListener(this);

mEditText.setOnFocusChangeListener(this);

mDeleteIcon.setOnClickListener(this);

mShiftIcon.setOnCheckedChangeListener(this);

//设置默认状态---删除按钮和是否显示密码

mShiftIcon.setChecked(false);

updateDeleteIcon(mEditText.getText().toString(), mEditText.isFocused());

updateShowPassword(mShiftIcon.isChecked());

}

}

@Override

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

}

@Override

public void onTextChanged(CharSequence s, int start, int before, int count) {

updateDeleteIcon(s.toString(), mEditText.isFocused());

}

@Override

public void afterTextChanged(Editable s) {

}

@Override

public void onFocusChange(View v, boolean hasFocus) {

updateDeleteIcon(mEditText.getText().toString(), hasFocus);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.delete:

mEditText.setText("");

break;

}

}

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

updateShowPassword(isChecked);

}

/**

* 用于是否显示密码

*

* @param password

* @param focused

*/

private void updateDeleteIcon(String password, boolean focused) {

if (!TextUtils.isEmpty(password) && focused) {

mDeleteIcon.setVisibility(VISIBLE);

} else {

mDeleteIcon.setVisibility(INVISIBLE);

}

}

/**

* 用于控制是否显示密码

*

* @param checked

*/

private void updateShowPassword(boolean checked) {

if (checked) {

mEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

} else {

mEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());

}

mEditText.setSelection(mEditText.getText().toString().length());

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值