Android自定义密码输入EditTextLayout

 更新时间:2018年08月21日 11:49:06   作者:showdy  
这篇文章主要为大家详细介绍了Android自定义密码输入EditTextLayout,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

布局

<?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());
  }

}

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

相关文章

  • Android 清除SharedPreferences 产生的数据(实例代码)

    Android 清除SharedPreferences 产生的数据(实例代码)

    项目是要保存上次文件播放的位置,我使用SharedPreferences来保存,键值对分别是文件路径和当时播放的位置
    2013-11-11
  • 分享安装Android Studio3.6的经验教训

    分享安装Android Studio3.6的经验教训

    这篇文章主要介绍了Android Studio3.6的安装错误问题及解决方法,非常值得大家参考,现把整个过程分享到脚本之家平台,需要的朋友参考下吧
    2020-02-02
  • Android手势ImageView三部曲 第一部

    Android手势ImageView三部曲 第一部

    这篇文章主要为大家详细介绍了Android手势ImageView三部曲的第一部,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • Android自定义实现侧滑菜单效果

    Android自定义实现侧滑菜单效果

    这篇文章主要为大家详细介绍了Android自定义实现侧滑菜单效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android实现屏幕锁定源码详解

    Android实现屏幕锁定源码详解

    本篇文章主要介绍了Android实现屏幕锁定源码详解,屏幕锁定是一个很有用的功能,有需要的可以了解一下。
    2016-10-10
  • Android多媒体教程之播放视频的四种方法

    Android多媒体教程之播放视频的四种方法

    这篇文章主要给大家介绍了关于Android多媒体教程之播放视频的四种方法,分别是通过intent的方式,调用系统自带的播放器、使用VideoView、MediaPlayer + SurfaceView及MediaPlayer + TextureView等方法,需要的朋友们可以参考学习。
    2017-06-06
  • Android adb 出错解决方法

    Android adb 出错解决方法

    本文主要介绍Android 中的adb,相信大家在开发过程中经常使用adb进行调试,这里主要说明adb 出错问题解决方法,希望能帮助有需要的同学
    2016-07-07
  • Android中Webview使用全面详解

    Android中Webview使用全面详解

    Android WebView是一个用于在应用程序中显示网页内容的组件,它可以加载网页并在应用程序内部显示,而不是调用系统浏览器,这篇文章主要给大家介绍了关于Android中Webview使用的相关资料,需要的朋友可以参考下
    2024-07-07
  • android shape的使用及渐变色、分割线、边框、半透明阴影

    android shape的使用及渐变色、分割线、边框、半透明阴影

    这篇文章主要介绍了android shape的使用及渐变色、分割线、边框、半透明阴影,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Android中的Notification机制深入理解

    Android中的Notification机制深入理解

    这篇文章主要给大家介绍了关于Android中Notification机制的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-02-02

最新评论