重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Android中怎么利用EditTextLayout自定义密码输入?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联公司专注于独山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供独山营销型网站建设,独山网站制作、独山网页设计、独山网站官网定制、微信平台小程序开发服务,打造独山网络公司原创品牌,更为您提供独山网站排名全网营销落地服务。
布局
用于密码输入的自定义控件
/** * 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()); } }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。