自定义密码输入EditTextLayout
来源:互联网 发布:qq群网络原因上传失败 编辑:程序博客网 时间:2024/06/06 09:11
<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/delete" android:layout_width="30dp" android:layout_height="48dp" android:layout_gravity="center" android:scaleType="center" android:src="@drawable/ico_delete"/> <CheckBox 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"/></merge>
/** * Created by showdy on 2017/3/15. * <p> * 一个用于密码输入的自定义控件 */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
- 自定义密码输入EditTextLayout
- 自定义密码输入框
- 自定义密码输入框
- 自定义密码 输入界面
- 自定义的密码输入判断
- 自定义密码输入框,无圆角
- Android自定义密码输入框
- 自定义查看密码输入框
- 自定义输入支付密码框
- 自定义密码输入框(passwordEditText)
- 自定义密码输入框PasswordInput 仿微信密码输入框
- Android自定义支付密码输入框
- 自定义数字密码EditText输入框
- Android实现自定义PasswordInputView密码输入
- Android自定义View之密码输入框
- wpf 自定义密码样式输入框
- 自定义view之支付密码输入框
- 自定义Android Dialog EditText 密码输入框
- ros_arduino_bridge的安装及使用
- python3.x 画图二维
- os.path模块简介
- JSP 日期处理
- java 多线程学习之多生产者多消费者产生的线程安全问题分析与解决:Lock和Condition
- 自定义密码输入EditTextLayout
- Animator 实现 FloatActionBar 点击弹出多个FloatActionBar
- Cloud云深网谈:空手套白狼的智慧
- [bzoj4293][PA2015]Siano
- 几何着色器
- uboot 通用board_init_f实现
- Wdate 小知识点
- 数据结构之链表
- iOS使用StroryBoard页面跳转及传值